并发字典ConcurrentDictionary+迭代器--异步
作者:互联网
并发字典ConcurrentDictionary+迭代器--异步
代码如下:
using Microsoft.EntityFrameworkCore; using System; using System.Collections; using System.Collections.Concurrent; using System.Collections.Generic; using System.Linq; using System.Threading; using System.Threading.Tasks; namespace EFCOREDB { class Program { static void Main(string[] args) { Console.WriteLine("Hello World!"); #region //GetAsync(); //var iterator = GetEnumerator(); //while (iterator.MoveNext()) //{ // Console.WriteLine($"输出{iterator.Current}"); //} #region 测试 //TestConcurrentDictionary(); #endregion Console.Read(); } #region 测试并发字典ConcurrentDictionary+迭代器--异步 /// <summary> /// 迭代器--异步 /// </summary> /// <returns></returns> static async void GetAsync() { var iterator = GetEnumeratorAsyncFor(); while (await iterator.MoveNextAsync()) { Console.WriteLine($"输出{iterator.Current}"); } #region MyRegion //var iterator = GetEnumeratorAsync(); // var result = await iterator.MoveNextAsync(); // while (result) // { // Console.WriteLine($"输出{iterator.Current}"); // result = await iterator.MoveNextAsync(); // } // //while (await iterator.MoveNextAsync()) // //{ // // Console.WriteLine($"输出{iterator.Current}"); // //} #endregion Console.WriteLine("GetAsync---结束。。。。"); } /// <summary> /// 迭代器--异步 /// </summary> /// <returns></returns> static async IAsyncEnumerator<string> GetEnumeratorAsyncFor() { for (int i = 0; i < 10; i++) { //await Task.Delay(1000); //yield return i.ToString(); await Task.Delay(1000).ContinueWith((_) => { Console.WriteLine($"迭代器--异步--{i}"); }); yield return i.ToString(); } Console.WriteLine("GetEnumeratorAsync---结束。。。。"); } /// <summary> /// 迭代器--异步 /// </summary> /// <returns></returns> static async IAsyncEnumerator<string> GetEnumeratorAsync() { await Task.Delay(1000); //Console.WriteLine("输出A"); yield return "B"; await Task.Delay(1000); //Console.WriteLine("输出A"); yield return "C"; await Task.Delay(1000); //Console.WriteLine("输出A"); yield return "D"; Console.WriteLine("GetEnumeratorAsync---结束。。。。"); } /// <summary> /// 迭代器 /// </summary> /// <returns></returns> static IEnumerator<string> GetEnumerator() { yield return "A"; //Console.WriteLine("输出A"); yield return "B"; //Console.WriteLine("输出A"); yield return "C"; //Console.WriteLine("输出A"); yield return "D"; Console.WriteLine("结束。。。。"); } /// <summary> /// 测试并发字典ConcurrentDictionary /// </summary> static void TestConcurrentDictionary() { #region MyRegion ConcurrentDictionary<int, int> cd = new ConcurrentDictionary<int, int>(); Parallel.For(1, 100, i => { cd.AddOrUpdate(1, 1, (key, oldValue) => oldValue + 1); }); Console.WriteLine("After 10000 AddOrUpdates, cd[1] = {0}, should be 10000", cd[1]); // Should return 100, as key 2 is not yet in the dictionary int value = cd.GetOrAdd(2, (key) => 100); Console.WriteLine("After initial GetOrAdd, cd[2] = {0} (should be 100)", value); // Should return 100, as key 2 is already set to that value value = cd.GetOrAdd(2, 10000); Console.WriteLine("After second GetOrAdd, cd[2] = {0} (should be 100)", value); //Console.WriteLine("After initial GetOrAdd, cd[2] = {0} (should be 100)", value); #endregion } #endregion } }
标签:异步,Console,iterator,--,await,ConcurrentDictionary,WriteLine,return,cd 来源: https://www.cnblogs.com/1175429393wljblog/p/14042786.html