網(wǎng)站首頁 編程語言 正文
Task.Wait()與awaiat?Task異常處理區(qū)別
Task異常處理
下面有兩個(gè)例子代碼,可以直接復(fù)制粘貼到.net core中運(yùn)行。兩個(gè)代碼要實(shí)現(xiàn)的功能完全一樣,但是內(nèi)核卻又很大差異。
先看下面用await的例子與輸出:
using System;
using System.Threading;
using System.Threading.Tasks;
class Program
{
static async Task Main()
{
System.Console.WriteLine($"Main Task ID:{Thread.CurrentThread.ManagedThreadId}");
var task = Task.Run(() =>
{
System.Console.WriteLine($"In Task.Run(), Task ID:{Thread.CurrentThread.ManagedThreadId}");
int[] vary=new int[5];
while (true)
{
Thread.Sleep(3000);
int d = vary[6];
}
});
// Just continue on this thread, or await with try-catch:
try
{
await task;
}
catch (IndexOutOfRangeException ex)
{
System.Console.WriteLine(ex.Message);
System.Console.WriteLine($"After Wait(), Task ID:{Thread.CurrentThread.ManagedThreadId}");
}
catch(AggregateException ex)
{
System.Console.WriteLine(ex.Message);
System.Console.WriteLine($"After Wait(), Task ID:{Thread.CurrentThread.ManagedThreadId}");
}
finally
{
//...
}
System.Console.WriteLine("Reach end.");
Console.ReadKey();
}
}
/*
Main Task ID:1
In Task.Run(), Task ID:4
Index was outside the bounds of the array.
Catch System.IndexOutOfRangeException
After Wait(), Task ID:4
Reach end.
*/
再看Task.Wait()方法下的異常處理與輸出:
using System;
using System.Threading;
using System.Threading.Tasks;
class Program
{
static void Main()
{
System.Console.WriteLine($"Main Task ID:{Thread.CurrentThread.ManagedThreadId}");
var task = Task.Run(() =>
{
System.Console.WriteLine($"In Task.Run(), Task ID:{Thread.CurrentThread.ManagedThreadId}");
int[] vary=new int[5];
while (true)
{
Thread.Sleep(3000);
int d = vary[6];
}
});
// Just continue on this thread, or await with try-catch:
try
{
task.Wait();
}
catch (IndexOutOfRangeException ex)
{
System.Console.WriteLine($"Catch {ex.GetType()}");
System.Console.WriteLine($"After Wait(), Task ID:{Thread.CurrentThread.ManagedThreadId}");
}
catch(AggregateException ex)
{
System.Console.WriteLine($"Catch {ex.GetType()}");
System.Console.WriteLine($"After Wait(), Task ID:{Thread.CurrentThread.ManagedThreadId}");
}
finally
{
//...
}
System.Console.WriteLine("Reach end.");
Console.ReadKey();
}
}
/*
Main Task ID:1
In Task.Run(), Task ID:4
Catch System.AggregateException
One or more errors occurred. (Index was outside the bounds of the array.)
After Wait(), Task ID:1
Reach end.
*/
從例子中可以看出,await之后的代碼其實(shí)都是在新的線程(4線程)中執(zhí)行,而Task.Wait()方法后的線程則是在主線程中執(zhí)行。
因此,await之后的代碼完全以傳統(tǒng)方式處理異常;而Task.Wait()拋出的異常則由于是從新線程往外部線程拋出,所以它是被重新封裝為AggregateException異常拋出。
Task.WaitAll()注意事項(xiàng)
使用Task.WaitAll() 等待多任務(wù)執(zhí)行完畢的時(shí)候發(fā)現(xiàn),等待的任務(wù)還沒結(jié)束,Task.WaitAll() 就先結(jié)束了,于是就寫了一段測(cè)試代碼進(jìn)行驗(yàn)證。
先上代碼
static void Main(string[] args)
{
//建立兩個(gè)任務(wù)
Task t1 = new Task(async () => await T1());
Task t2 = new Task(async () => await T2());
//啟動(dòng)任務(wù)
t1.Start();
t2.Start();
//等待任務(wù)完成
Task.WaitAll(t1, t2);
Print("WaitAll Done");
Console.ReadLine();
}
static async Task T1()
{
Print("T1 Start");
Thread.Sleep(1000);
Print("T1 await");
await Task.Delay(1000);
Print("T1 Done");
}
static async Task T2()
{
Print("T2 Start");
Thread.Sleep(1000);
Print("T2 await");
await Task.Delay(1000);
Print("T2 Done");
}
static void Print(string msg)
{
Console.WriteLine($"{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.ffffff")}: {msg}");
}
再上結(jié)果,注意看T1、T2 Done 和 WaitAll Done的打印時(shí)間:?
果然,坑!
Task.WaitAll() 盡然比等待的Task先結(jié)束。
總結(jié):(不推薦,請(qǐng)看補(bǔ)充內(nèi)容)
new Task().Start()?中一旦使用 await ,會(huì)立馬返回結(jié)束狀態(tài)。
所以,在使用 Task.WaitAll()? 或其接續(xù)任務(wù)的時(shí)候,可以考慮使用 Thead.sleep() 替代 await? Task.Delay() 。
2022-04-25 補(bǔ)充:
經(jīng)過【32號(hào)就放假】提醒,測(cè)試了Task.Run() 和 Task.Factory.StartNew()兩個(gè)方法,得出結(jié)論:
1、 在Task.Run()啟動(dòng)任務(wù)中,await會(huì)正常運(yùn)行;(推薦使用)
static void Main(string[] args)
{
//建立兩個(gè)任務(wù)
Task t1 = Task.Run(T1);
Task t2 = Task.Run(T2);
//等待任務(wù)完成
Task.WaitAll(t1, t2);
Print("WaitAll Done");
Console.ReadLine();
}
2、在Task.Factory.StartNew() 啟動(dòng)任務(wù)中,會(huì)立馬返回結(jié)束狀態(tài)。
static void Main(string[] args)
{
//建立兩個(gè)任務(wù)
Task t1 = Task.Factory.StartNew(T1);
Task t2 = Task.Factory.StartNew(T2);
//等待任務(wù)完成
Task.WaitAll(t1, t2);
Print("WaitAll Done");
Console.ReadLine();
}
原文鏈接:https://blog.csdn.net/qq_16587307/article/details/103954279
相關(guān)推薦
- 2022-07-17 oracle數(shù)據(jù)庫去除重復(fù)數(shù)據(jù)常用的方法總結(jié)_oracle
- 2022-12-13 Redis?Hash序列化存儲(chǔ)的問題及解決方案_Redis
- 2023-06-03 一文帶你吃透Python中的os和sys模塊_python
- 2022-06-09 Entity?Framework?Core基于數(shù)據(jù)模型創(chuàng)建數(shù)據(jù)庫_實(shí)用技巧
- 2022-11-27 Python?OpenCV實(shí)現(xiàn)圖像增強(qiáng)操作詳解_python
- 2022-07-09 Android廣播實(shí)現(xiàn)App開機(jī)自啟動(dòng)_Android
- 2022-04-27 一篇文章了解正則表達(dá)式的替換技巧_正則表達(dá)式
- 2022-09-12 C++數(shù)據(jù)結(jié)構(gòu)哈希表詳解_C 語言
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支