C# 同步/并发队列ConcurrentQueue
如下所示,ConcurrentQueue做到了代码的简化,在并发模型中充当同步对象
private ConcurrentQueue<string> inQueue = new ConcurrentQueue<string>(); private object lockObject = new object();
private Queue<string> queue = new Queue<string>();
MSDN例子(还是并行库强大):
static void Main(string[] args)
{
// Construct a ConcurrentQueue.
ConcurrentQueue<int> cq = new ConcurrentQueue<int>(); // Populate the queue.
for (int i = ; i < ; i++) cq.Enqueue(i); // Peek at the first element.
int result;
if (!cq.TryPeek(out result))
{
Console.WriteLine("CQ: TryPeek failed when it should have succeeded");
}
else if (result != )
{
Console.WriteLine("CQ: Expected TryPeek result of 0, got {0}", result);
} int outerSum = ;
// An action to consume the ConcurrentQueue.
Action action = () =>
{
int localSum = ;
int localValue;
while (cq.TryDequeue(out localValue)) localSum += localValue;
Interlocked.Add(ref outerSum, localSum);
}; // Start 4 concurrent consuming actions.
Parallel.Invoke(action, action, action, action); Console.WriteLine("outerSum = {0}, should be 49995000", outerSum);
}
C# 同步/并发队列ConcurrentQueue的更多相关文章
- C# 同步/并发队列ConcurrentQueue (表示线程安全的先进先出 (FIFO) 集合)
http://msdn.microsoft.com/zh-cn/library/dd267265(v=vs.110).aspx static void Main(string[] args) { // ...
- [一起读源码]走进C#并发队列ConcurrentQueue的内部世界
决定从这篇文章开始,开一个读源码系列,不限制平台语言或工具,任何自己感兴趣的都会写.前几天碰到一个小问题又读了一遍ConcurrentQueue的源码,那就拿C#中比较常用的并发队列Concurren ...
- [一起读源码]走进C#并发队列ConcurrentQueue的内部世界 — .NET Core篇
在上一篇<走进C#并发队列ConcurrentQueue的内部世界>中解析了Framework下的ConcurrentQueue实现原理,经过抛砖引玉,得到了一众大佬的指点,找到了.NET ...
- C# 并发队列ConcurrentQueue
测试函数 static async Task RunProgram() { var taskQueue = new ConcurrentQueue<CustomTask>(); var c ...
- OC 线程操作 - GCD使用 -同步函数,异步函数,串行队列,并发队列
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ // GCD 开几条线程并不是我们 ...
- 阻塞队列LinkedBlockingQueue和并发队列ConcurrentLinkedQueue
LinkedBlockingQueue: public class LinkedBlockingQueue<E> extends AbstractQueue<E> implem ...
- 刀哥多线程之并发队列gcd-05-dispatch_queue_concurrent
并发队列 特点 以先进先出的方式,并发调度队列中的任务执行 如果当前调度的任务是同步执行的,会等待任务执行完成后,再调度后续的任务 如果当前调度的任务是异步执行的,同时底层线程池有可用的线程资源,会再 ...
- ios多线程操作(五)—— GCD串行队列与并发队列
GCD的队列能够分为2大类型,分别为串行队列和并发队列 串行队列(Serial Dispatch Queue): 一次仅仅调度一个任务,队列中的任务一个接着一个地运行( ...
- iOS 之GCD串行和并发队列的理解
dispatch_queue_t serialQueue = dispatch_queue_create("com.lai.www", DISPATCH_QUEUE_SERIAL) ...
随机推荐
- Windows主机里利用VMware安装Linux(CentOS)虚拟机,Host-only连接上网方式详解
关于Host-only指的是主机与虚拟机之间的互联,因此虚拟机是不能连网的,若需要连网则需要使用NAT模式: Host-only模式实现联网得考虑如下配置过程: 附:VMware虚拟机三种网络模式(B ...
- String的内存分配
1.String类是final类不能被继承 2.String str="abc"的内部工作 (1)先在栈中定 一个名为str的String类的引用变量 String str: (2 ...
- Android--UI之EditText
前言 上一篇博客介绍了Android的TextView控件,这篇博客来说一下EditText控件.EditText为一个文本控件,提供了文本输入的功能,而且继承自TextView,可以理解为可以输入的 ...
- 第15章 使用Postfix与Dovecot收发电子邮件
章节概述: 本章节从电子邮局系统的组成角色开始讲起,了解MUA.MTA与MDA的作用,熟悉熟悉SMTP.POP3与IMAP4邮局协议. 学习postfix与dovecot服务程序的使用方法并逐条讲解配 ...
- NGUI的部分控件无法更改layer?
http://momowing.diandian.com/post/2012-09-17/40038835795 今天狗日的遇到这样的问题,这是一个imagebutton:,它的层定义为:,NGUI里 ...
- Android 云服务器的搭建和友盟APP自动更新功能的实现
setContentView(R.layout.activity_splash); //Bmob SDK初始化--只需要这一段代码即可完成初始化 //请到Bmob官网(http://www.bmob. ...
- BZOJ 3540 realtime-update 解题
分析一下题意,大约是给定一串牛,然后找到一个跨越距离最长的牛子串使得在这个范围内白牛和花牛一样多. 白牛可以任意涂成花牛. 既然"白牛可以任意涂成花牛",那么我们需要找到一个最长的 ...
- nginx lua处理图片
user apache apache; worker_processes 4; worker_rlimit_nofile 100000; #error_log logs/error.log; #err ...
- N-Queens | & N-Queens II
The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens ...
- CheckBoxList1复选框
循环绑定数据的两个方法: List<string> LIColl = new List<string>(); protected void Page_Load(object s ...