• Task---常用的多线程(基于多线程线程)


    Thread 内容多,不易控制。 Task 好用(必须掌握)。

     1 #region Private Method
     2         /// <summary>
     3         /// 一个比较耗时耗资源的私有方法
     4         /// </summary>
     5         /// <param name="name"></param>
     6         private void DoSomethingLong(string name)
     7         {
     8             Console.WriteLine($"****************DoSomethingLong Start {name} {Thread.CurrentThread.ManagedThreadId} {DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss fff")}***************");
     9             long lResult = 0;
    10             for (int i = 0; i < 2000000000; i++)
    11             {
    12                 lResult += i;
    13             }
    14             //Thread.Sleep(2000);
    15 
    16             Console.WriteLine($"****************DoSomethingLong   End  {name} {Thread.CurrentThread.ManagedThreadId} {DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss fff")}***************");
    17         }
    18         #endregion
    DoSomethingLong

    声明:

    1 Task task = Task.Factory.StartNew(() => this.DoSomethingLong(name));

    WaitAny() && WaitAll()---卡主线程,卡界面

    1             Console.WriteLine("before WaitAny");
    2             Task.WaitAny(taskList.ToArray());//当前线程等待某个任务的完成  主线程
    3             Console.WriteLine("after WaitAny");
    4 
    5 
    6             Console.WriteLine("before WaitAll");
    7             Task.WaitAll(taskList.ToArray());//当前线程等待全部任务的完成  主线程
    8             Console.WriteLine("after WaitAll");

    ContinueWhenAny() && ContinueWhenAll() ---不卡主线程,不卡界面

     1           taskList.Add(Task.Factory.ContinueWhenAny(taskList.ToArray(), t =>
     2                 {
     3                     Console.WriteLine(t.IsCompleted);
     4                     Console.WriteLine($"ContinueWhenAny {Thread.CurrentThread.ManagedThreadId}");
     5                 }));
     6 
     7             taskList.Add(Task.Factory.ContinueWhenAll(taskList.ToArray(), tList =>
     8             {
     9                 Console.WriteLine(tList[0].IsCompleted);
    10                 Console.WriteLine($"ContinueWhenAll {Thread.CurrentThread.ManagedThreadId}");
    11             }));
    12             //回调形式的,全部任务完成后执行的后续动作

    ContinueWith() 建议少用(嵌套太多,容易晕)

    1           //Task taskContinue = task.ContinueWith(t =>
    2                 // {
    3                 //     Console.WriteLine(t.IsCompleted);
    4                 //     Console.WriteLine($"ContinueWhenAny {Thread.CurrentThread.ManagedThreadId}");
    5                 // }).ContinueWith(t =>
    6                 // {
    7                 //     Console.WriteLine(t.IsCompleted);
    8                 //     Console.WriteLine($"ContinueWhenAny {Thread.CurrentThread.ManagedThreadId}");
    9                 // });
  • 相关阅读:
    MVC WebApi的两种访问方法
    MVC CRUD 的两种方法
    MVC EF 导航属性
    MVC EF两种查询方法
    MVC WebApi
    POJ 1511 Invitation Cards ( 双向单源最短路 || 最小来回花费 )
    POJ 2502 Subway ( 最短路 && 最短路建图 )
    POJ 3660 Cow Contest ( 最短路松弛思想应用 && Floyd求传递闭包 )
    POJ 1502 MPI MaeIstrom ( 裸最短路 || atoi系统函数 )
    POJ 3259 Wormholes ( SPFA判断负环 && 思维 )
  • 原文地址:https://www.cnblogs.com/anwser-jungle/p/8878097.html
Copyright © 2020-2023  润新知