日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢(xún)
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問(wèn)題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷(xiāo)解決方案
.NET多線程:Task使用方法

Task為.NET提供了基于任務(wù)的異步模式,它不是線程,它運(yùn)行在線程池的線程上,下面為大家詳細(xì)講解一下Task的使用方法。

成都創(chuàng)新互聯(lián)是一家專(zhuān)業(yè)提供克東企業(yè)網(wǎng)站建設(shè),專(zhuān)注與網(wǎng)站設(shè)計(jì)、成都網(wǎng)站建設(shè)、H5網(wǎng)站設(shè)計(jì)、小程序制作等業(yè)務(wù)。10年已為克東眾多企業(yè)、政府機(jī)構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專(zhuān)業(yè)網(wǎng)絡(luò)公司優(yōu)惠進(jìn)行中。

創(chuàng)建并且初始化Task

使用lambda表達(dá)式創(chuàng)建Task

Task.Factory.StartNew(() => Console.WriteLine("Hello from a task!"));

var task = new Task(() => Console.Write("Hello"));

task.Start();

用默認(rèn)參數(shù)的委托創(chuàng)建Task

using System;

using System.Threading.Tasks;

namespace MultiThread

{

 class ThreadTest

 {

   static void Main()

   {

     var task = Task.Factory.StartNew(state => Greet("Hello"), "Greeting");

    Console.WriteLine(task.AsyncState); // Greeting

     task.Wait();
   }

   static void Greet(string message) { Console.Write(message); }

 }

}

這種方式的一個(gè)優(yōu)點(diǎn)是,task.AsyncState作為一個(gè)內(nèi)置的屬性,可以在不同線程中獲取參數(shù)的狀態(tài)。

System.Threading.Tasks.TaskCreateOptions

創(chuàng)建Task的時(shí)候,我們可以指定創(chuàng)建Task的一些相關(guān)選項(xiàng)。在.Net 4.0中,有如下選項(xiàng):

LongRunning

用來(lái)表示這個(gè)Task是長(zhǎng)期運(yùn)行的,這個(gè)參數(shù)更適合block線程。LongRunning線程一般回收的周期會(huì)比較長(zhǎng),因此CLR可能不會(huì)把它放到線程池中進(jìn)行管理。

PreferFairness

表示讓Task盡量以公平的方式運(yùn)行,避免出現(xiàn)某些線程運(yùn)行過(guò)快或者過(guò)慢的情況。

AttachedToParent

表示創(chuàng)建的Task是當(dāng)前線程所在Task的子任務(wù)。這一個(gè)用途也很常見(jiàn)。

下面的代碼是創(chuàng)建子任務(wù)的示例:

using System;

using System.Threading;

using System.Threading.Tasks;

namespace MultiThread

{

 class ThreadTest

 {

   public static void Main(string[] args)

   {

     Task parent = Task.Factory.StartNew(() =>

     {

       Console.WriteLine("I am a parent");

       Task.Factory.StartNew(() =>    // Detached task

       {

         Console.WriteLine("I am detached");

       });

       Task.Factory.StartNew(() =>    // Child task

       {

         Console.WriteLine("I am a child");

       }, TaskCreationOptions.AttachedToParent);

     });

     parent.Wait();

     Console.ReadLine();

   }

 }

}

如果你等待你一個(gè)任務(wù)結(jié)束,你必須同時(shí)等待任務(wù)里面的子任務(wù)結(jié)束。這一點(diǎn)很重要,尤其是你在使用Continue的時(shí)候。(后面會(huì)介紹)

等待Task

在ThreadPool內(nèi)置的方法中無(wú)法實(shí)現(xiàn)的等待,在Task中可以很簡(jiǎn)單的實(shí)現(xiàn)了:

using System;

using System.Threading;

using System.Threading.Tasks;

namespace MultiThread

{

 class ThreadTest

 {

   static void Main()

   {

     var t1 = Task.Run(() => Go(null));

     var t2 = Task.Run(() => Go(123));

     Task.WaitAll(t1, t2);//等待所有Task結(jié)束

     //Task.WaitAny(t1, t2);//等待任意Task結(jié)束

   }

   static void Go(object data) // data will be null with the first call.

   {

     Thread.Sleep(5000);

     Console.WriteLine("Hello from the thread pool! " + data);

   }

 }

}

注意:

當(dāng)你調(diào)用一個(gè)Wait方法時(shí),當(dāng)前的線程會(huì)被阻塞,直到Task返回。但是如果Task還沒(méi)有被執(zhí)行,這個(gè)時(shí)候系統(tǒng)可能會(huì)用當(dāng)前的線程來(lái)執(zhí)行調(diào)用Task,而不是新建一個(gè),這樣就不需要重新創(chuàng)建一個(gè)線程,并且阻塞當(dāng)前線程。這種做法節(jié)省了創(chuàng)建新線程的開(kāi)銷(xiāo),也避免了一些線程的切換。但是也有缺點(diǎn),當(dāng)前線程如果和被調(diào)用的Task同時(shí)想要獲得一個(gè)lock,就會(huì)導(dǎo)致死鎖。

Task異常處理

當(dāng)?shù)却粋€(gè)Task完成的時(shí)候(調(diào)用Wait或者或者訪問(wèn)Result屬性的時(shí)候),Task任務(wù)中沒(méi)有處理的異常會(huì)被封裝成AggregateException重新拋出,InnerExceptions屬性封裝了各個(gè)Task沒(méi)有處理的異常。

using System;

using System.Threading.Tasks;

namespace MultiThreadTest

{

 class Program

 {

  static void Main(string[] args)
   {
     int x = 0;

    Task
  
    calc = Task.Factory.StartNew(() => 7 / x);     try     {       Console.WriteLine(calc.Result);   }     catch (AggregateException aex)      {       Console.Write(aex.InnerException.Message); // Attempted to divide by 0      }   }  } } 
  

對(duì)于有父子關(guān)系的Task,子任務(wù)未處理的異常會(huì)逐層傳遞到父Task,并且最后包裝在AggregateException中。

using System;

using System.Threading.Tasks;

namespace MultiThreadTest

{

 class Program

 {

  static void Main(string[] args)

  {

    TaskCreationOptions atp = TaskCreationOptions.AttachedToParent;
    var parent = Task.Factory.StartNew(() =>
     {

      Task.Factory.StartNew(() => // Child

      {

       Task.Factory.StartNew(() => { throw null; }, atp); // Grandchild
       }, atp);

     });

     // The following call throws a NullReferenceException (wrapped

     // in nested AggregateExceptions):

     parent.Wait();

   }

 }

}

取消Task

如果想要支持取消任務(wù),那么在創(chuàng)建Task的時(shí)候,需要傳入一個(gè)CancellationTokenSouce

示例代碼:

using System;

using System.Threading;

using System.Threading.Tasks;

namespace MultiThreadTest

{

 class Program

 {

  static void Main(string[] args)

   {

     var cancelSource = new CancellationTokenSource();

     CancellationToken token = cancelSource.Token;

     Task task = Task.Factory.StartNew(() =>

     {

       // Do some stuff...

       token.ThrowIfCancellationRequested(); // Check for cancellation request

       // Do some stuff...

    }, token);

    cancelSource.Cancel();

    try

     {

       task.Wait();

     }

     catch (AggregateException ex)
     {

      if (ex.InnerException is OperationCanceledException)

         Console.Write("Task canceled!");

     }

     Console.ReadLine();

 }

 }

}

任務(wù)的連續(xù)執(zhí)行

Continuations

任務(wù)調(diào)度也是常見(jiàn)的需求,Task支持一個(gè)任務(wù)結(jié)束之后執(zhí)行另一個(gè)任務(wù)。

Task task1 = Task.Factory.StartNew(() => Console.Write(“antecedant..”));

Task task2 = task1.ContinueWith(task =>Console.Write(“..continuation”));

Continuations 和Task

Task也有帶返回值的重載,示例代碼如下:

Task.Factory.StartNew(() => 8)

.ContinueWith(ant => ant.Result * 2)

.ContinueWith(ant => Math.Sqrt(ant.Result))

.ContinueWith(ant => Console.WriteLine(ant.Result)); // output 4

子任務(wù)

前面提到了,當(dāng)你等待一個(gè)任務(wù)的時(shí)候,同時(shí)需要等待它的子任務(wù)???成。

下面代碼演示了帶子任務(wù)的Task:

using System;

using System.Threading.Tasks;

using System.Threading;

namespace MultiThreadTest

{

 class Program

 {

   public static void Main(string[] args)

   {

     Task
  
    parentTask = Task.Factory.StartNew(() =>      {        int[] results = new int[3];        Task t1 = new Task(() => { Thread.Sleep(3000); results[0] = 0; }, TaskCreationOptions.AttachedToParent);        Task t2 = new Task(() => { Thread.Sleep(3000); results[1] = 1; }, TaskCreationOptions.AttachedToParent);        Task t3 = new Task(() => { Thread.Sleep(3000); results[2] = 2; }, TaskCreationOptions.AttachedToParent);        t1.Start();        t2.Start();        t3.Start();        
   return results;      });      Task finalTask = parentTask.ContinueWith(parent =>      {        foreach (int result 
   in parent.Result)        {          Console.WriteLine(result);        }      });      finalTask.Wait();      Console.ReadLine();    }  } } 
  

這段代碼的輸出結(jié)果是: 1,2,3

FinalTask會(huì)等待所有子Task結(jié)束后再執(zhí)行。

TaskFactory

關(guān)于TaskFactory,上面的例子中我們使用了System.Threading.Tasks .Task.Factory屬性來(lái)快速的創(chuàng)建Task。當(dāng)然你也可以自己創(chuàng)建TaskFactory,你可以指定自己的TaskCreationOptions,TaskContinuationOptions來(lái)使得通過(guò)你的Factory創(chuàng)建的Task默認(rèn)行為不同。

.Net中有一些默認(rèn)的創(chuàng)建Task的方式,由于TaskFactory創(chuàng)建Task的默認(rèn)行為不同可能會(huì)導(dǎo)致一些不容易發(fā)現(xiàn)的問(wèn)題。

如在.NET 4.5中,Task加入了一個(gè)Run的靜態(tài)方法:

Task.Run(someAction);

如果你用這個(gè)方法代替上面例子中的Task.Factory.StartNew,就無(wú)法得到正確的結(jié)果。原因是Task.Run創(chuàng)建Task的行為默認(rèn)是默認(rèn)是拒絕添加子任務(wù)的。上面的代碼等價(jià)于:

Task.Factory.StartNew(someAction, CancellationToken.None, TaskCreationOptions.DenyChildAttach, TaskScheduler.Default);

你也可以創(chuàng)建具有自己默認(rèn)行為的TaskFactory。

無(wú)論ThreadPool也好,或者Task,微軟都是在想進(jìn)辦法來(lái)實(shí)現(xiàn)線程的重用,來(lái)節(jié)省不停的創(chuàng)建銷(xiāo)毀線程帶來(lái)的開(kāi)銷(xiāo)。線程池內(nèi)部的實(shí)現(xiàn)可能在不同版本中有不同的機(jī)制。如果可能的話,使用線程池來(lái)管理線程仍然是建議的選擇。

我們主要介紹了一下Task的基本用法,在我們編程過(guò)程中,有一些使用Task來(lái)提升程序性能的場(chǎng)景往往是很相似的,微軟為了簡(jiǎn)化編程,在System.Threading.Tasks.Parallel中封裝了一系列的并行類(lèi),內(nèi)部也是通過(guò)Task來(lái)實(shí)現(xiàn)的。

Parallel的For,F(xiàn)oreach,Invoke 方法

在編程過(guò)程中,我們經(jīng)常會(huì)用到循環(huán)語(yǔ)句:

for (int i = 0; i

{

DoSomeWork(i);

}

如果循環(huán)過(guò)程中的工作可以是并行的話,那么我們可以用如下語(yǔ)句:

Parallel.For(0, 10, i => DoSomeWork(i));

我們也經(jīng)常會(huì)使用Foreach來(lái)遍歷某個(gè)集合:

foreach (var item in collection)

{

DoSomeWork(item);

}

如果我們用一個(gè)線程池來(lái)執(zhí)行里面的任務(wù),那么我們可以寫(xiě)成:

Parallel.ForEach(collection, item => DoSomeWork(item));

最后,如果你想并行的執(zhí)行幾個(gè)不同的方法,你可以:

Parallel.Invoke(Method1, Method2, Method3);

如果你看下后臺(tái)的實(shí)現(xiàn),你會(huì)發(fā)現(xiàn)基本都是基于Task的線程池,當(dāng)然你也可以通過(guò)手動(dòng)創(chuàng)建一個(gè)Task集合,然后等待所有的任務(wù)結(jié)束來(lái)實(shí)現(xiàn)同樣的功能。上面的Parallel.For和Parallel.Forach方法并不以為這你可以尋找你代碼里面所有用到For和Foreach方法,并且替代他們,因?yàn)槊恳粋€(gè)任務(wù)都會(huì)分配一個(gè)委托,并且在線程池里執(zhí)行,如果委托里面的任務(wù)是線程不安全的,你可能還需要lock來(lái)保證線程安全,使用lock本身就會(huì)造成性能上的損耗。如果每一個(gè)任務(wù)都是需要長(zhǎng)時(shí)間執(zhí)行并且線程安全的,Parallel會(huì)給你帶來(lái)不錯(cuò)的性能提升。對(duì)于短任務(wù),或者線程不安全的任務(wù),你需要權(quán)衡下,你是否真的需要使用Parallel。


新聞名稱(chēng):.NET多線程:Task使用方法
分享地址:http://www.5511xx.com/article/cdpiisp.html