多線程(5)async&await

  .net 4.0的Task已經讓咱們能夠很是簡單地使用多線程,而且能夠有返回值,也能夠支持線程的取消等操做,可謂已經很強大了。但.net 4.5爲咱們帶來了async&await,使得實現多線程的寫法更簡單,更優美,更符合線性思惟。多線程

下面經過一個例子來演示經過Task和async&await分別如何實現,而且最後還附上代碼執行順序圖。async

使用Task實現

以下代碼:ide

 1 #region 使用Task實現
 2 static void TestByTask()
 3 {
 4     Console.WriteLine("main thread start,current thread id:" + Thread.CurrentThread.ManagedThreadId);
 5     var task = Task.Factory.StartNew<string>(() =>
 6     {
 7         return GetNameByTask();
 8     });
 9     Console.WriteLine("get another thread result,result:" + task.Result);
10     Console.WriteLine("main thread completed!");
11 }
12 
13 static string GetNameByTask()
14 {
15     Console.WriteLine("another thread start,current thread id:" + Thread.CurrentThread.ManagedThreadId);
16     return "mcgrady";
17 } 
18 #endregion

 

輸出結果:spa

 

使用async&await實現

 假如使用async&await如何實現呢,以下代碼:.net

 1 #region 使用async&await實現
 2 static async void TestByAsyncAwait()
 3 {
 4     Console.WriteLine("main thread start,current thread id:" + Thread.CurrentThread.ManagedThreadId);
 5     var name = GetNameByAsyncAwait();
 6 
 7     Console.WriteLine(string.Format("get another thread result,result:{0}", await name));
 8     Console.WriteLine("main thread completed!");
 9 }
10 
11 static async Task<string> GetNameByAsyncAwait()
12 {
13     return await Task.Factory.StartNew<string>(() =>
14     {
15         Console.WriteLine("another thread start,current thread id:" + Thread.CurrentThread.ManagedThreadId);
16         return "mcgrady";
17     });
18 } 
19 #endregion

 

輸出結果:線程

輸出結果跟使用Task相同。code

 

代碼執行流程以下圖:orm

 

完整代碼:blog

 1 namespace ConsoleApplication25
 2 {
 3     class Program
 4     {
 5         static void Main(string[] args)
 6         {
 7             //1,使用task實現
 8             //TestByTask();
 9 
10             //2,使用async&await實現
11             TestByAsyncAwait();
12 
13             Console.ReadKey();
14         }
15 
16         #region 使用Task實現
17         static void TestByTask()
18         {
19             Console.WriteLine("main thread start,current thread id:" + Thread.CurrentThread.ManagedThreadId);
20             var task = Task.Factory.StartNew<string>(() =>
21             {
22                 return GetNameByTask();
23             });
24             Console.WriteLine("get another thread result,result:" + task.Result);
25             Console.WriteLine("main thread completed!");
26         }
27 
28         static string GetNameByTask()
29         {
30             Console.WriteLine("another thread start,current thread id:" + Thread.CurrentThread.ManagedThreadId);
31             return "mcgrady";
32         } 
33         #endregion
34 
35         #region 使用async&await實現
36         static async void TestByAsyncAwait()
37         {
38             Console.WriteLine("main thread start,current thread id:" + Thread.CurrentThread.ManagedThreadId);
39             var name = GetNameByAsyncAwait();
40 
41             Console.WriteLine(string.Format("get another thread result,result:{0}", await name));
42             Console.WriteLine("main thread completed!");
43         }
44 
45         static async Task<string> GetNameByAsyncAwait()
46         {
47             return await Task.Factory.StartNew<string>(() =>
48             {
49                 Console.WriteLine("another thread start,current thread id:" + Thread.CurrentThread.ManagedThreadId);
50                 return "mcgrady";
51             });
52         } 
53         #endregion
54     }
55 }
View Code
相關文章
相關標籤/搜索