资源预览内容
第1页 / 共11页
第2页 / 共11页
第3页 / 共11页
第4页 / 共11页
第5页 / 共11页
第6页 / 共11页
第7页 / 共11页
第8页 / 共11页
第9页 / 共11页
第10页 / 共11页
亲,该文档总共11页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述
C#并发集合的简单方法微软对C#(4.0)的框架添加了全新的并发编程框架,现在我们也能用C#开发支持并发概念的程序的。在并发编程中最让人烦恼的应该就是如何数据同步:避免脏读和脏写,当然我们可以通过Lock技术来实现,也可以使用微软提供给我们的并发集合,这些集合都提供了TryDo方法。用它们对数据的读/写操作能在TryDo返回True的情况下执行。我们来看看它们吧:IProducerConsumerCollection 所有的并发集合都实现了这个接口,TryAdd和TryTake分别在读和写的时候判断是否能正常进行,不行则返回false。csharppublic interface IProducerConsumerCollection : IEnumerable, ICollection, IEnumerable void CopyTo(T array, int index); T ToArray(); bool TryAdd(T item); bool TryTake(out T item);public interface IProducerConsumerCollection : IEnumerable, ICollection, IEnumerable void CopyTo(T array, int index); T ToArray(); bool TryAdd(T item); bool TryTake(out T item); ConcurrentQueue 并发队列,队列类型的数据结构。csharppublic static void ConcurrentQueueTest() Parallel.ForQueue(1, 10000, () = while (strNornalQueue.Count != 26) if (strNornalQueue.Count = 0) for (int i = 65; i string tmp = string.Empty; tmp = head.Dequeue(); 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, strNornalQueue.Count, IsDuplicate(strNornalQueue.GetEnumerator(); foreach (string item in strNornalQueue) Console.WriteLine(item); public static void ConcurrentQueueTest() Parallel.ForQueue(1, 10000, () = while (strNornalQueue.Count != 26) if (strNornalQueue.Count = 0) for (int i = 65; i string tmp = string.Empty; tmp = head.Dequeue(); 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, strNornalQueue.Count, IsDuplicate(strNornalQueue.GetEnumerator();foreach (string item in strNornalQueue) Console.WriteLine(item); ConcurrentStack并发栈,栈类型的数据结构。csharppublic static void ConcurrentStackTest() Parallel.ForConcurrentStack(1, 10000, () = while (strStack.Count != 26) if (strStack.Count = 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); else Console.WriteLine(queue is buzy now); return strStack; , (result) = Console.WriteLine(-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); public static void ConcurrentStackTest() Parallel.ForConcurrentStack(1, 10000, () = while (strStack.Count != 26) if (strStack.Count = 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); else Console.WriteLine(queue is buzy now); return strStack; , (result) = Console.WriteLine(-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 = 65; i = 90; i+) strDictionary.TryAdd(Convert.ToChar(i).ToString(), Convert.ToChar(i).ToString(); Parallel.ForConcurr
收藏 下载该资源
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号