ConcurrentDictionary<int, BlockingCollection<string>> mailBoxes = new ConcurrentDictionary<int, BlockingCollection<string>>();
int maxBoxes = ; CancellationTokenSource cancelationTokenSource = new CancellationTokenSource();
CancellationToken cancelationToken = cancelationTokenSource.Token; Random rnd = new Random();
// Producer
Task.Factory.StartNew(() =>
{
while (true)
{
int index = rnd.Next(, maxBoxes);
// put the letter in the mailbox 'index'
var box = mailBoxes.GetOrAdd(index, new BlockingCollection<string>());
box.Add("some message " + index, cancelationToken);
Console.WriteLine("Produced a letter to put in box " + index); // Wait simulating a heavy production item.
Thread.Sleep();
}
}); // Consumer 1
Task.Factory.StartNew(() =>
{
while (true)
{
int index = rnd.Next(, maxBoxes);
// get the letter in the mailbox 'index'
var box = mailBoxes.GetOrAdd(index, new BlockingCollection<string>());
var message = box.Take(cancelationToken);
Console.WriteLine("Consumed 1: " + message); // consume a item cost less than produce it:
Thread.Sleep();
}
}); // Consumer 2
Task.Factory.StartNew(() =>
{
while (true)
{
int index = rnd.Next(, maxBoxes);
// get the letter in the mailbox 'index'
var box = mailBoxes.GetOrAdd(index, new BlockingCollection<string>());
var message = box.Take(cancelationToken);
Console.WriteLine("Consumed 2: " + message); // consume a item cost less than produce it:
Thread.Sleep();
}
}); Console.ReadLine();
cancelationTokenSource.Cancel();

wrap ConcurrentDictionary in BlockingCollection的更多相关文章

  1. Java进击C#——语法之基础

    本章简言 上一章讲到关于项目工程开发常用的知识点,有了前面俩章的介绍之后.本章正式开始介绍关于C#的基础语法.我们都很清楚C#也是面向对象的计算机语言.而且他跟JAVA的相似度高达80%.所以很多语法 ...

  2. ConcurrentDictionary并发字典知多少?

    背景 在上一篇文章你真的了解字典吗?一文中我介绍了Hash Function和字典的工作的基本原理. 有网友在文章底部评论,说我的Remove和Add方法没有考虑线程安全问题. https://doc ...

  3. 【C#】58. .Net中的并发集合——BlockingCollection

    https://blog.csdn.net/huiwuhuiwu/article/details/53608269 这篇是并发集合中的最后一篇,介绍一下BlockingCollection.在工作中我 ...

  4. BlockingCollection使用

    BlockingCollection是一个线程安全的生产者-消费者集合. 代码 public class BlockingTest { BlockingCollection<int> bc ...

  5. jQuery之常用且重要方法梳理(siblings,nextAll,end,wrap,apply,call,each)-(二)

    1.siblings() siblings() 获得匹配集合中每个元素的同胞,通过选择器进行筛选是可选的. <body> <div><span>Hello</ ...

  6. ConcurrentDictionary线程不安全么,你难道没疑惑,你难道弄懂了么?

    前言 事情不太多时,会时不时去看项目中同事写的代码可以作个参考或者学习,个人觉得只有这样才能走的更远,抱着一副老子天下第一的态度最终只能是井底之蛙.前两篇写到关于断点传续的文章,还有一篇还未写出,后续 ...

  7. .net源码分析 - ConcurrentDictionary<TKey, TValue>

    List源码分析 Dictionary源码分析 ConcurrentDictionary源码分析 继上篇Dictionary源码分析,上篇讲过的在这里不会再重复 ConcurrentDictionar ...

  8. [转载]jQuery中wrap、wrapAll和wrapInner用法以及区别

    原文地址:jQuery中wrap.wrapAll和wrapInner用法以及区别作者:伊少君 原文: <ul>   <li title='苹果'>苹果</li>   ...

  9. 【数据类型】Dictionary 与 ConcurrentDictionary 待续

    Dictionary<TKey, TValue> 泛型类提供了从一组键到一组值的映射.通过键来检索值的速度是非常快的,接近于 O(1),这是因为 Dictionary<TKey, T ...

随机推荐

  1. 面试题:输入两个整数 n 和 m,从数列1,2,3…….n 中 随意取几个数, 使其和等于 m

    问题: 2010年中兴面试题 编程求解: 输入两个整数 n 和 m,从数列1,2,3…….n 中 随意取几个数, 使其和等于 m ,要求将其中所有的可能组合列出来. 思路: 类似这种组合问题一般都是使 ...

  2. CF475C. Kamal-ol-molk's Painting

    C. Kamal-ol-molk's Painting time limit per test 2 seconds memory limit per test 256 megabytes input ...

  3. USACO 6.3 Cowcycles

    CowcyclesOriginally by Don Gillies [International readers should note that some words are puns on co ...

  4. pomelo 安装

    1. 安装nodejs ,python ,C++运行环境(VS2012以上版本) 2.npm install -g node-gyp --registry=https://registry.npm.t ...

  5. Windows下安装mysql cluster

    0.mysql集群介绍 浅谈mysql集群——http://blog.csdn.net/chenxingzhen001/article/details/7708663: 官网——http://dev. ...

  6. git 设置bitbucket 邮箱、用户

    1. git config --global user.name "youname" 2 .git config --global user.email "youeami ...

  7. Android四大组件-服务

    Android服务 android 的服务有点类似windows的服务,没有界面,在后台长时间运行,如果我们这种需求的话我们就可以使用服务来实现. 服务的典型应用场景: 播放音乐,下载等,如果要在一个 ...

  8. 机器学习之路:python 网格搜索 并行搜索 GridSearchCV 模型检验方法

    git:https://github.com/linyi0604/MachineLearning 如何确定一个模型应该使用哪种参数? k折交叉验证: 将样本分成k份 每次取其中一份做测试数据 其他做训 ...

  9. 排序算法之直接插入排序Java实现

    排序算法之直接插入排序 舞蹈演示排序: 冒泡排序: http://t.cn/hrf58M 希尔排序:http://t.cn/hrosvb  选择排序:http://t.cn/hros6e  插入排序: ...

  10. BZOJ 4066 简单题(KD树)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=4066 [题目大意] 要求维护矩阵内格子加点和矩阵查询 [题解] 往KD树上加权值点,支 ...