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. [AngularJS] Design Pattern: Simple Mediator

    We're going to use rootScope emit here to send out events and then we're going to listen for them in ...

  2. [ES6] ... spread operator

    var parts = ['shoulders', 'knees']; var lyrics = ['head', ...parts, 'and', 'toes']; // ["head&q ...

  3. Windows API一日一练(55)FlushFileBuffers和SetFilePointer函数

    在PC硬件体系结构里,速度最快的存储器是CPU里面的寄存器,接着到二级缓存,再到系统RAM内存,最后才到硬盘.因为这种体系结构,就决定了操作系统对文件的操作方式,或者说是最优化的算法.比方操作系统接收 ...

  4. 10 Powerful Apache Modules--reference

    Apache is the most popular web server in the world,because it is more efficient than others.Thrust o ...

  5. LOAD DATA INFILE Syntax--官方

    LOAD DATA [LOW_PRIORITY | CONCURRENT] [LOCAL] INFILE 'file_name' [REPLACE | IGNORE] INTO TABLE tbl_n ...

  6. Gamit解算脚本

    这是一个解算单天的shell脚本,对于初学者很有帮助. 首先就是需要在项目(四个字符)建立rinex brdc igs 还有以年纪日命名的目录,然后提前准备好station.info和lfile.文件 ...

  7. 在ASP.NET中使用一般处理程序生成验证码

    如果期望一般处理程序(ashx)处理Session,必须实现[System.Web.SessionState]命名空间下的[IRequiresSessionState]接口. asp.net中的验证码 ...

  8. Linux samba服务器设置简单匿名共享

    linux下面的samba非常的好用,很多人拿它来作共享文件服务器, 缺省配置下,samba必须提供用户名密码来访问,如果是所有人都可以访问的内容,那么是比较麻烦的,其实通过一个设置,即可实现不用输入 ...

  9. PL/SQL中的变量

    1.标量: ag1: declare v_ename emp.ename%type;--自己称为单变量 begin select ename into v_ename from emp where e ...

  10. centos的常用命令

    公司服务器主要是centos,第一篇就从centos的常用命令开始吧. 转载自:http://www.cnblogs.com/zitsing/archive/2012/05/02/2479009.ht ...