<output id="qn6qe"></output>

    1. <output id="qn6qe"><tt id="qn6qe"></tt></output>
    2. <strike id="qn6qe"></strike>

      亚洲 日本 欧洲 欧美 视频,日韩中文字幕有码av,一本一道av中文字幕无码,国产线播放免费人成视频播放,人妻少妇偷人无码视频,日夜啪啪一区二区三区,国产尤物精品自在拍视频首页,久热这里只有精品12

      CPU 100% 問(wèn)題分析,我們把博客園踩過(guò)的坑又踩了一遍《二》(補(bǔ)充)

      問(wèn)題如下:

      針對(duì).net core 異常奇怪問(wèn)題分析

      1、StackExchange.Redis.RedisConnectionException: It was not possible to connect to the redis server(s); to create a disconnected multiplexer, disable AbortOnConnectFail. Timeout
      at StackExchange.Redis.ConnectionMultiplexer.ConnectImpl(Func`1 multiplexerFactory, TextWriter log)

      2、There is already an open DataReader associated with this Command which must be closed first

      3、A second operation started on this context before a previous operation completed. This is usually caused by different threads using the same instance of DbContext, however instance members are not guaranteed to be

      static void Main(string[] args)
              {
                //按鍵后開始執(zhí)行
                  Console.ReadKey();
                  //模擬并發(fā)
                  while (true)
                  {
                      Task.Run(Producer);
                      Thread.Sleep(200);
                  }
                 
              }
              /// <summary>
              /// 如果處理程序耗時(shí)>請(qǐng)求耗時(shí),也就是說(shuō)引起并發(fā),就會(huì)導(dǎo)致死鎖
              /// </summary>
              static void Producer()
              {
                  var result = Process().Result;
                  //或者
                  //Process().Wait();
              }
      
      
              static async Task<bool> Process()
              {
                  Console.WriteLine("Start - " + DateTime.Now.ToLongTimeString());
                  await Task.Run(() =>
                  {
                      //模擬任務(wù)執(zhí)行耗時(shí)
                      Thread.Sleep(1000);
                  });
      
                  Console.WriteLine("Ended - " + DateTime.Now.ToLongTimeString());
                  return true;
              }        

       

      執(zhí)行效果:

      以上可看出:

      1、控制臺(tái)執(zhí)行5-10個(gè)任務(wù)后,不再執(zhí)行Ended 語(yǔ)句的任何內(nèi)容(Ended語(yǔ)句全部被阻塞)

      2、內(nèi)存占用穩(wěn)步上升

      3、AsyncTest.exe 線程每秒增加一個(gè)

       

      解決方案:

      var result = Process().ConfigureAwait(false);

      或修改為異步方法(強(qiáng)烈推薦

           static async Task Producer()
              {
                  await Process();
              }

       ---補(bǔ)充 2021.2.26-------------------

      優(yōu)化代碼后進(jìn)一步測(cè)試

      測(cè)試代碼如下:

        1 using System;
        2 using System.Threading;
        3 using System.Threading.Tasks;
        4 
        5 namespace AsyncTest
        6 {
        7     class Program
        8     {
        9         static void Main(string[] args)
       10         {
       11             Console.ReadKey();
       12             //模擬并發(fā)
       13             for (int i = 0; i < 300; i++)
       14             {
       15                
       16                 Task.Run(() => 
       17                      Producer(i)
       18                 );
       19                 // Task.Run(Producer3);
       20                 // Task.Run(ProducerQueue);
       21                 Thread.Sleep(300);
       22             }
       23         }
       24         /// <summary>
       25         /// 如果處理程序耗時(shí)>請(qǐng)求耗時(shí),也就是說(shuō)引起并發(fā),就會(huì)導(dǎo)致死鎖
       26         /// </summary>
       27         static void Producer(int i)
       28         {
       29             var result = Process(i).Result;
       30             //或者
       31             //Process().Wait();
       32         }
       33         /// <summary>
       34         /// 正常
       35         /// </summary>
       36         static void Producer2(int i)
       37         {
       38             var result = Process(i).ConfigureAwait(false);
       39         }
       40         //cpu 內(nèi)存均正常
       41         static async Task Producer3(int i)
       42         {
       43             await Process(i);
       44         }
       45 
       46         /// <summary>
       47         /// 
       48         /// </summary>
       49         static void ProducerQueue(int i)
       50         {
       51             ProcessQueue(i).Wait();
       52             //或者
       53             //Process().Wait();
       54         }
       55         /// <summary>
       56         /// 正常
       57         /// </summary>
       58         static void ProducerQueue2(int i)
       59         {
       60             var result = ProcessQueue(i).ConfigureAwait(false);
       61         }
       62         //cpu 內(nèi)存均正常
       63         static async Task ProducerQueue3(int i)
       64         {
       65             await ProcessQueue(i);
       66         }
       67 
       68         static async Task<bool> Process(int i)
       69         {
       70             ConsoleWrite($"Start{i}");
       71             await Task.Run(() =>
       72             {
       73                 //模擬任務(wù)執(zhí)行耗時(shí)
       74                 Thread.Sleep(2000);
       75             });
       76 
       77             ConsoleWrite($"End{i}");
       78             return true;
       79         }
       80 
       81 
       82 
       83         static async Task ProcessQueue(int i)
       84         {
       85             ConsoleWrite($"Start{i}");
       86             ThreadPool.QueueUserWorkItem(state =>
       87             {
       88                 ConsoleWrite("Hello" + (string)state);
       89             }, await GetName(i));
       90             ConsoleWrite($"End{i}");
       91         }
       92 
       93         private static async Task<string> GetName(int i)
       94         {
       95             Thread.Sleep(1000);
       96             Random r = new Random();
       97             return $"ZhiXin[{r.Next(100, 999)}]-[{i}]";
       98         }
       99         private static string GetCurrentThreadID()
      100         {
      101             return $" {DateTime.Now.ToLongTimeString()} --ThreadId[{Thread.CurrentThread.ManagedThreadId.ToString("0000")}]";
      102         }
      103         private static void ConsoleWrite(string type)
      104         {
      105             if (type.Contains("Start"))
      106             {
      107                 //Console.BackgroundColor = ConsoleColor.Blue; //設(shè)置背景色
      108                 Console.ForegroundColor = ConsoleColor.Green; //設(shè)置前景色,即字體顏色
      109             }
      110             else if (type.Contains("End"))
      111             {
      112                 // Console.BackgroundColor = ConsoleColor.Red; //設(shè)置背景色
      113                 Console.ForegroundColor = ConsoleColor.Red; //設(shè)置前景色,即字體顏色
      114             }
      115             Console.WriteLine($"{type} - " + GetCurrentThreadID());
      116         }
      117     }
      118 }
      使用.Result異步調(diào)用

      結(jié)果如下:

      測(cè)試代碼如下:

        1 using System;
        2 using System.Threading;
        3 using System.Threading.Tasks;
        4 
        5 namespace AsyncTest
        6 {
        7     class Program
        8     {
        9         static void Main(string[] args)
       10         {
       11             Console.ReadKey();
       12             //模擬并發(fā)
       13             for (int i = 0; i < 300; i++)
       14             {
       15                
       16                 Task.Run(() => 
       17                      Producer3(i)
       18                 );
       19                 // Task.Run(Producer3);
       20                 // Task.Run(ProducerQueue);
       21                 Thread.Sleep(300);
       22             }
       23         }
       24         /// <summary>
       25         /// 如果處理程序耗時(shí)>請(qǐng)求耗時(shí),也就是說(shuō)引起并發(fā),就會(huì)導(dǎo)致死鎖
       26         /// </summary>
       27         static void Producer(int i)
       28         {
       29             var result = Process(i).Result;
       30             //或者
       31             //Process().Wait();
       32         }
       33         /// <summary>
       34         /// 正常
       35         /// </summary>
       36         static void Producer2(int i)
       37         {
       38             var result = Process(i).ConfigureAwait(false);
       39         }
       40         //cpu 內(nèi)存均正常
       41         static async Task Producer3(int i)
       42         {
       43             await Process(i);
       44         }
       45 
       46         /// <summary>
       47         /// 
       48         /// </summary>
       49         static void ProducerQueue(int i)
       50         {
       51             ProcessQueue(i).Wait();
       52             //或者
       53             //Process().Wait();
       54         }
       55         /// <summary>
       56         /// 正常
       57         /// </summary>
       58         static void ProducerQueue2(int i)
       59         {
       60             var result = ProcessQueue(i).ConfigureAwait(false);
       61         }
       62         //cpu 內(nèi)存均正常
       63         static async Task ProducerQueue3(int i)
       64         {
       65             await ProcessQueue(i);
       66         }
       67 
       68         static async Task<bool> Process(int i)
       69         {
       70             ConsoleWrite($"Start{i}");
       71             await Task.Run(() =>
       72             {
       73                 //模擬任務(wù)執(zhí)行耗時(shí)
       74                 Thread.Sleep(2000);
       75             });
       76 
       77             ConsoleWrite($"End{i}");
       78             return true;
       79         }
       80 
       81 
       82 
       83         static async Task ProcessQueue(int i)
       84         {
       85             ConsoleWrite($"Start{i}");
       86             ThreadPool.QueueUserWorkItem(state =>
       87             {
       88                 ConsoleWrite("Hello" + (string)state);
       89             }, await GetName(i));
       90             ConsoleWrite($"End{i}");
       91         }
       92 
       93         private static async Task<string> GetName(int i)
       94         {
       95             Thread.Sleep(1000);
       96             Random r = new Random();
       97             return $"ZhiXin[{r.Next(100, 999)}]-[{i}]";
       98         }
       99         private static string GetCurrentThreadID()
      100         {
      101             return $" {DateTime.Now.ToLongTimeString()} --ThreadId[{Thread.CurrentThread.ManagedThreadId.ToString("0000")}]";
      102         }
      103         private static void ConsoleWrite(string type)
      104         {
      105             if (type.Contains("Start"))
      106             {
      107                 //Console.BackgroundColor = ConsoleColor.Blue; //設(shè)置背景色
      108                 Console.ForegroundColor = ConsoleColor.Green; //設(shè)置前景色,即字體顏色
      109             }
      110             else if (type.Contains("End"))
      111             {
      112                 // Console.BackgroundColor = ConsoleColor.Red; //設(shè)置背景色
      113                 Console.ForegroundColor = ConsoleColor.Red; //設(shè)置前景色,即字體顏色
      114             }
      115             Console.WriteLine($"{type} - " + GetCurrentThreadID());
      116         }
      117     }
      118 }
      使用await關(guān)鍵字異步調(diào)用

       

      結(jié)果如下:

       

       

       

       ps:截屏工具 ScreenToGif

      參考:

      又踩.NET Core的坑:在同步方法中調(diào)用異步方法Wait時(shí)發(fā)生死鎖(deadlock)

      一碼阻塞,萬(wàn)碼等待:ASP.NET Core 同步方法調(diào)用異步方法“死鎖”的真相

      .NET Core中遇到奇怪的線程死鎖問(wèn)題:內(nèi)存與線程數(shù)不停地增長(zhǎng)

       C#同步方法中如何調(diào)用異步方法?值得一看

       理解C#中的ConfigureAwait

       bindot:http://www.rzrgm.cn/bindot/p/cpu100.html

      posted @ 2021-02-02 16:22  FengLu-1  閱讀(1264)  評(píng)論(4)    收藏  舉報(bào)
      主站蜘蛛池模板: 人妻人人澡人人添人人爽人人玩| 亚洲精品无码久久一线| 国产视频一区二区三区视频| 国产午夜伦鲁鲁| 99久久精品国产亚洲精品| 日本精品不卡一二三区| 亚洲欧洲日韩精品在线| 人妻另类 专区 欧美 制服| 好爽好紧好大的免费视频| 亚洲精品一区二区三区婷婷月| 国产精品露脸视频观看| 少妇伦子伦情品无吗| 免费无码又黄又爽又刺激| 成人综合人人爽一区二区| 宣威市| 亚洲人午夜射精精品日韩| 国产精品SM捆绑调教视频| 国产在线亚州精品内射 | 国产卡一卡二卡三免费入口| 青青草无码免费一二三区| 国产成人亚洲综合app网站| 精品人妻午夜一区二区三区四区| 豆国产97在线 | 亚洲| 国产精品亚洲一区二区在| 午夜福利国产精品视频| 中文字幕精品亚洲二区| 国产精品伦人视频免费看| 国产成人无码免费视频麻豆| 老色99久久九九爱精品| 中文在线最新版天堂| 日韩深夜福利视频在线观看| 国产卡一卡二卡三免费入口| 国产欧美综合在线观看第十页| 综合激情网一区二区三区| 中文一区二区视频| 精品无码国产自产拍在线观看蜜 | 亚洲另类激情专区小说图片| 国产在线精品一区二区三区| 国产精品成人无码久久久| 欧美人与禽2o2o性论交| 亚洲人成色77777|