http://msdn.microsoft.com/zh-cn/library/dd267265(v=vs.110).aspx

static void Main(string[] args)
{
// Construct a ConcurrentQueue.
ConcurrentQueue<int> cq = new ConcurrentQueue<int>(); // Populate the queue.
for (int i = 0; i < 10000; 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 != 0)
{
Console.WriteLine("CQ: Expected TryPeek result of 0, got {0}", result);
} int outerSum = 0;
// An action to consume the ConcurrentQueue.
Action action = () =>
{
int localSum = 0;
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 (表示线程安全的先进先出 (FIFO) 集合)的更多相关文章

  1. C# 同步/并发队列ConcurrentQueue

    如下所示,ConcurrentQueue做到了代码的简化,在并发模型中充当同步对象 private ConcurrentQueue<string> inQueue = new Concur ...

  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. ConcurrentQueue并发队列

    表示线程安全的先进先出 (FIFO) 集合 System.Collections.Concurrent 命名空间提供多个线程安全集合类.当有多个线程并发访问集合时,应使用这些类代替 System.Co ...

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

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

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

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

  8. 【Java并发】并发队列与线程池

    并发队列 阻塞队列与非阻塞队 ConcurrentLinkedQueue BlockingQueue ArrayBlockingQueue LinkedBlockingQueue PriorityBl ...

  9. c# 高效的线程安全队列ConcurrentQueue

    c#高效的线程安全队列ConcurrentQueue<T>(上) c# 高效的线程安全队列ConcurrentQueue(下) Segment类 c#高效的线程安全队列Concurrent ...

随机推荐

  1. android listview综合使用演示样例_结合数据库操作和listitem单击长按等事件处理

    本演示样例说明: 1.自己定义listview条目样式,自己定义listview显示列数的多少,灵活与数据库中字段绑定. 2.实现对DB的增删改查,而且操作后listview自己主动刷新. 3.响应用 ...

  2. 关于飞控下载时候出现avrdude: stk500_getsync(): not in sync: resp=0x00错误的解决方法

    avrdude: stk500_getsync(): not in sync: resp=0x00该问题表述为串口通信失败 经过分析,出现这种情况的原因大致有:1.arduino在IDE下载过程中没有 ...

  3. sql Server 常用存储过程的优化

    优化存储过程有很多种方法,下面介绍最常用的7种. 1.使用SET NOCOUNT ON选项 我们使用SELECT语句时,除了返回对应的结果集外,还会返回相应的影响行数.使用SET NOCOUNT ON ...

  4. iptables阻止服务器被攻击

    下列规则将会阻止来自某一特定IP范围内的数据包,因为该IP地址范围被管理员怀疑有大量恶意攻击者在活动:  # iptables -t filter -A INPUT -s 123.456.789.0/ ...

  5. iOS9中请求出现App Transport Security has blocked a cleartext HTTP (http://)

    错误描述: App Transport Security has blocked a cleartext HTTP (http://) resource load since it is insecu ...

  6. php视图操作

    一.视图的基本介绍          视图是虚拟的表.与包含数据的表不一样,视图只包含使用时动态检索数据的查询.         使用视图需要MySQL5及以后的版本支持.         下面是视图 ...

  7. Mysql 视图笔记

    1.       视图的定义 视图就是从一个或多个表中,导出来的表,是一个虚拟存在的表.视图就像一个窗口(数据展示的窗口),通过这个窗口,可以看到系统专门提供的数据(也可以查看到数据表的全部数据),使 ...

  8. 跟我学android-搭建Android开发环境(一)

    Android官网地址:http://developer.android.com/,下载和安装 AndroidSDK请按如下步骤进行: 下载ADT 和SDK:http://developer.andr ...

  9. linux入门。删除不用到内核,为boot分区释放空间

    在终端中输入如下命令: dpkg --get-selections|grep linux-image 这时会列出系统中所有到内核. 你可以使用 uname -a 查看你当前使用到内核. 然后用 sud ...

  10. intent.setFlags方法中参数值的含义

    intent.setFlags()方法中参数的含义 1.FLAG_ACTIVITY_NEW_TASK: 例如现在栈一的情况是:A    B   C(C位于栈顶),C通过intent跳转到D,并且这个I ...