如下所示,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的更多相关文章

  1. C# 同步/并发队列ConcurrentQueue (表示线程安全的先进先出 (FIFO) 集合)

    http://msdn.microsoft.com/zh-cn/library/dd267265(v=vs.110).aspx static void Main(string[] args) { // ...

  2. [一起读源码]走进C#并发队列ConcurrentQueue的内部世界

    决定从这篇文章开始,开一个读源码系列,不限制平台语言或工具,任何自己感兴趣的都会写.前几天碰到一个小问题又读了一遍ConcurrentQueue的源码,那就拿C#中比较常用的并发队列Concurren ...

  3. [一起读源码]走进C#并发队列ConcurrentQueue的内部世界 — .NET Core篇

    在上一篇<走进C#并发队列ConcurrentQueue的内部世界>中解析了Framework下的ConcurrentQueue实现原理,经过抛砖引玉,得到了一众大佬的指点,找到了.NET ...

  4. C# 并发队列ConcurrentQueue

    测试函数 static async Task RunProgram() { var taskQueue = new ConcurrentQueue<CustomTask>(); var c ...

  5. OC 线程操作 - GCD使用 -同步函数,异步函数,串行队列,并发队列

    - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ // GCD 开几条线程并不是我们 ...

  6. 阻塞队列LinkedBlockingQueue和并发队列ConcurrentLinkedQueue

    LinkedBlockingQueue: public class LinkedBlockingQueue<E> extends AbstractQueue<E> implem ...

  7. 刀哥多线程之并发队列gcd-05-dispatch_queue_concurrent

    并发队列 特点 以先进先出的方式,并发调度队列中的任务执行 如果当前调度的任务是同步执行的,会等待任务执行完成后,再调度后续的任务 如果当前调度的任务是异步执行的,同时底层线程池有可用的线程资源,会再 ...

  8. ios多线程操作(五)—— GCD串行队列与并发队列

          GCD的队列能够分为2大类型,分别为串行队列和并发队列      串行队列(Serial Dispatch Queue):      一次仅仅调度一个任务,队列中的任务一个接着一个地运行( ...

  9. iOS 之GCD串行和并发队列的理解

    dispatch_queue_t serialQueue = dispatch_queue_create("com.lai.www", DISPATCH_QUEUE_SERIAL) ...

随机推荐

  1. SSDT Hook实现简单的进程隐藏和保护【转载】

    原文链接:http://www.blogfshare.com/ssdthook-hide-protect.html 原文作者:AloneMonkey SSDT Hook实现简单的进程隐藏和保护 Alo ...

  2. Win10走红背后,最开心的人却是谷歌

    导读 微软在不惜余力推进Windows10普及的同时,也有一些让自己小小郁闷的事儿发生,在Win10系统当中,微软用新的Edge浏览器取代了用户熟悉的IE浏览器,以求改写在浏览器市场上的被动局面,不过 ...

  3. [HDU5015]233 Matrix

    [HDU5015]233 Matrix 试题描述 In our daily life we often use 233 to express our feelings. Actually, we ma ...

  4. Sqli-LABS通关笔录-1

    在第一个关卡当中还是学到些知识. 1.注释语句多试试其他的几个 2.不报错可能是前面的语句没有错误,union没有得到执行. http://127.0.0.1/sql/Less-1/index.php ...

  5. Leetcode 之Convert Sorted Array to Binary Search Tree(54)

    思路很简单,用二分法,每次选中间的点作为根结点,用左.右结点递归. TreeNode* sortedArrayToBST(vector<int> &num) { return so ...

  6. 2014-08-07 SSDB 使用 rocksdb 引擎

    http://www.ideawu.net/blog/archives/824.html 为了满足各位对 Facebook 出品的 rocksdb 的爱好, SSDB 数据库也可以使用 rocksdb ...

  7. 《ASP.NET1200例》当前上下文中不存在名称configurationmanager

    当前上下文中不存在名称ConfigurationManager的解决方法 今晚做项目在DBHelper.cs类中的数据库连接要改到web.config里面调用,结果在编译的时候却发现提示错误: 当前上 ...

  8. July 25th, Week 31st Monday, 2016

    We will not go quietly into the night. 今夜,我们将奋战到底. We will be the champion. We will not stop fightin ...

  9. windows8输入法终极完美修复

    现在WIN8正式版出现以来,win8的用户越来越多,毕竟是新系统,BUG肯定是有的,现在小编就为大家解决一个大BUG. 输入法BUG: 现象:1.删除系统输入法,重启后无法调出输入法; 2.卸载用户安 ...

  10. Linux设置IP

    进入 vi /etc/sysconfig/network-scripts/ifcfg-eth0  root # ifconfig eth0 192.168.22.232 root # route ad ...