• 并发字典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.time测试代码块执行时间
    label表单的关联性
    attr返回被选元素的属性值
    2018 885程序设计编程题
    输出斐波拉数列的前n个数(n>=2)
    简单的光照贴图
    复杂纹理复制及纹理叠加效果
    简单纹理复制
    UV旋转shader
    shader实现积雪效果
  • 原文地址:https://www.cnblogs.com/1175429393wljblog/p/14042786.html
Copyright © 2020-2023  润新知