C#并发集合的简单方法

上传人:宝路 文档编号:20902198 上传时间:2017-11-22 格式:DOC 页数:11 大小:37.85KB
返回 下载 相关 举报
C#并发集合的简单方法_第1页
第1页 / 共11页
C#并发集合的简单方法_第2页
第2页 / 共11页
C#并发集合的简单方法_第3页
第3页 / 共11页
C#并发集合的简单方法_第4页
第4页 / 共11页
C#并发集合的简单方法_第5页
第5页 / 共11页
点击查看更多>>
资源描述

《C#并发集合的简单方法》由会员分享,可在线阅读,更多相关《C#并发集合的简单方法(11页珍藏版)》请在金锄头文库上搜索。

1、计算机交流平台:计算机故障_计算机论坛 wyC#并发集合的简单方法微软对 C#(4.0)的框架添加了全新的并发 编程框架,现在我们也能用 C#开发支持并发概念的程序的。在并发编程中最让人烦恼的应该就是如何数据同步:避免脏读和脏写,当然我们可以通过 Lock 技术来实现,也可以使用微软提供给我们的并发集合,这些集合都提供了TryDo 方法。用它们对数据的读/写操作能在 TryDo 返回 True 的情况下执行。我们来看看它们吧:IProducerConsumerCollection所有的并发集合都实现了这个接口,TryAdd 和 TryTake 分别在读和写的时候判断是否能正常进行,不行则返回

2、 false。csharppublic interface IProducerConsumerCollection : IEnumerable, ICollection, IEnumerablevoid CopyTo(T array, int index);T ToArray();bool TryAdd(T item);bool TryTake(out T item);public interface IProducerConsumerCollection : IEnumerable, ICollection, IEnumerablevoid CopyTo(T array, int index

3、);T ToArray();bool TryAdd(T item);bool TryTake(out T item);ConcurrentQueue并发队列,队列类型的数据结构。csharppublic static void ConcurrentQueueTest()Parallel.For(1, 10000,() =while (strNornalQueue.Count != 26)if (strNornalQueue.Count = 0)for (int i = 65; i string tmp = string.Empty;tmp = head.Dequeue();Console.Wr

4、iteLine(string.Format(The element 0 was set by thread 1, tmp, System.Threading.Thread.CurrentThread.ManagedThreadId);head.Enqueue(tmp);return strNornalQueue;,(result) =Console.WriteLine(-end-););Console.WriteLine(string.Format(current collection has 0 elements, Has duplicate data:1, strNornalQueue.C

5、ount, IsDuplicate(strNornalQueue.GetEnumerator();foreach (string item in strNornalQueue)Console.WriteLine(item);public static void ConcurrentQueueTest()Parallel.For(1, 10000,() =while (strNornalQueue.Count != 26)if (strNornalQueue.Count = 0)for (int i = 65; i string tmp = string.Empty;tmp = head.Deq

6、ueue();Console.WriteLine(string.Format(The element 0 was set by thread 1, tmp, System.Threading.Thread.CurrentThread.ManagedThreadId);head.Enqueue(tmp);return strNornalQueue;,(result) =Console.WriteLine(-end-););Console.WriteLine(string.Format(current collection has 0 elements, Has duplicate data:1,

7、 strNornalQueue.Count, IsDuplicate(strNornalQueue.GetEnumerator();foreach (string item in strNornalQueue)Console.WriteLine(item);ConcurrentStack并发栈,栈类型的数据结构。csharppublic static void ConcurrentStackTest()Parallel.For(1, 10000,() =while (strStack.Count != 26)if (strStack.Count = 0)for (int i = 65; i s

8、tring tmp = string.Empty;if (head.TryPop(out tmp)Console.WriteLine(string.Format(The element 0 was set by thread 1, tmp, System.Threading.Thread.CurrentThread.ManagedThreadId);head.Push(tmp);elseConsole.WriteLine(queue is buzy now);return strStack;,(result) =Console.WriteLine(-end-););Console.WriteL

9、ine(string.Format(current collection has 0 elements, Has duplicate data:1, strStack.Count, IsDuplicate(strStack.GetEnumerator();foreach (string item in strStack)Console.WriteLine(item);public static void ConcurrentStackTest()Parallel.For(1, 10000,() =while (strStack.Count != 26)if (strStack.Count =

10、0)for (int i = 65; i string tmp = string.Empty;if (head.TryPop(out tmp)Console.WriteLine(string.Format(The element 0 was set by thread 1, tmp, System.Threading.Thread.CurrentThread.ManagedThreadId);head.Push(tmp);elseConsole.WriteLine(queue is buzy now);return strStack;,(result) =Console.WriteLine(-

11、end-););Console.WriteLine(string.Format(current collection has 0 elements, Has duplicate data:1, strStack.Count, IsDuplicate(strStack.GetEnumerator();foreach (string item in strStack)Console.WriteLine(item);ConcurrentDictionary并发字典,字典类型的数据结构。csharppublic static void ConcurrentDictionary()for (int i

12、= 65; i (1, 10000,() =Console.WriteLine(-start-);return strDictionary;计算机交流平台:计算机故障_计算机论坛 wy,(index, state, head) =string tmp = string.Empty;if (head.TryRemove(Convert.ToChar(new Random().Next(65,90).ToString(), out tmp)Console.WriteLine(string.Format(The element 0 was set by thread 1, tmp, System.

13、Threading.Thread.CurrentThread.ManagedThreadId);head.TryAdd(tmp, tmp);elseConsole.WriteLine(queue is buzy now);return strDictionary;,(result) =Console.WriteLine(-end-););public static void ConcurrentDictionary()for (int i = 65; i (1, 10000,() =Console.WriteLine(-start-);return strDictionary;,(index,

14、 state, head) =string tmp = string.Empty;if (head.TryRemove(Convert.ToChar(new Random().Next(65,90).ToString(), out tmp)Console.WriteLine(string.Format(The element 0 was set by thread 1, tmp, System.Threading.Thread.CurrentThread.ManagedThreadId);head.TryAdd(tmp, tmp);计算机交流平台:计算机故障_计算机论坛 wyelseConsole.WriteLine(queue is buzy now);return strDictionary;,(result) =Console.WriteLine(-end-););ConcurrentBag类似堆栈的数据结构。csharppublic static void ConcurrentBag()for (int i = 65; i (1, 10000,() =return strBag;,(index, state, head) =string tmp = string.Empty;if (strBag.TryTake(

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 行业资料 > 其它行业文档

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号