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) ...
随机推荐
- PHP error_log() 函数
定义和用法 error_log() 函数向服务器错误记录.文件或远程目标发送一个错误. 若成功,返回 true,否则返回 false. 语法 error_log(error,type,destinat ...
- loadrunner跑场景的时候出现:Abnormal termination, caused by mdrv process termination
1.问题 loadrunner跑场景的时候出现:Abnormal termination, caused by mdrv process termination. 备注:我使用的是RTE协议录制的脚本 ...
- Android EditText 文本框实现搜索和清空效果
前言 本文实现的效果:文本框输入为空时显示输入的图标:不为空时显示清空的图标,此时点击清空图标能清空文本框内输入文字. 声明 欢迎转载,但请保留文章原始出处:) 博客园:http://www.cnbl ...
- NSArray和NSMutableArray
//1. NSArray EOItems *eOItems = [[EOItems alloc] init]; eOItems.ID = [NSNumber numberWithInt:]; NSAr ...
- ruby实时查看日志
(文章是从我的个人主页上粘贴过来的, 大家也可以访问我的主页 www.iwangzheng.com) 在调试代码的时候,把日志文件打开,边操作边调试能很快帮助我们发现系统中存在的问题. $tail r ...
- (转)女生应该找一个玩ACM的男生
1.强烈的事业心 将来,他也一定会有自己热爱的事业.而且,男人最性感的时刻之一,就是他专心致志做事的时候.所以,找一个机会在他全神贯注玩ACM的时候,从侧面好好观察他,你就会发现我说的话没错. 2.永 ...
- 【分布式存储】GlusterFS failing to mount at boot with Ubuntu 14.04
GlusterFS failing to mount at boot with Ubuntu 14.04 Previously I asked about mounting GlusterFS a ...
- 【云计算】Docker云平台—Docker进阶
Docker云平台系列共三讲,此为第二讲:Docker进阶 参考资料: 五个Docker监控工具的对比:http://www.open-open.com/lib/view/open1433897177 ...
- 利用cocostudio库函数 实现左右滑动的背包栏UI (cocos2d-x 2.2.0)
.h #ifndef __COMMON_COMPONENTS__ #define __COMMON_COMPONENTS__ #include "cocos2d.h" #inclu ...
- springJDBC实现查询方法二
无废话,看代码: @Override public List<Sites> queryAllSites(Pager pager) { String sql = "select * ...