[英]Does await create another thread internally before shifting to the same thread as the caller (for UI application)
[英]Does await create another thread?
我閱讀了有關異步並等待的Microsoft文檔 。 它說:
async和await關鍵字不會導致創建其他線程。
但是我運行下面的代碼
using System;
using System.Collections.Concurrent;
using System.Threading;
using System.Threading.Tasks;
namespace Examples.AdvancedProgramming.AsynchronousOperations
{
public class AsyncMain
{
static void Main()
{
Console.WriteLine(Thread.CurrentThread.ManagedThreadId);
Task task = Test();
task.Wait();
}
public async static Task Test()
{
Console.WriteLine(Thread.CurrentThread.ManagedThreadId);
await Task.Delay(1000);
Console.WriteLine(Thread.CurrentThread.ManagedThreadId);
}
}
}
結果是
1個
1個
4
很明顯,
await Task.Delay(1000);
Console.WriteLine(Thread.CurrentThread.ManagedThreadId);
在另一個線程中運行。 為什么文檔說沒有創建線程?
我已經閱讀了上一個問題,使用async / await是否會創建一個新線程? ,但它不能回答我的問題,那里的答案只是從Microsoft來源復制粘貼。 為什么在測試中看到不同的線程ID?
您在測試中看到了不同的線程ID,因為使用了不同的線程來執行代碼。 這並不意味着異步/等待導致了其他線程的創建。 在這種情況下,使用的線程已經存在於稱為線程池的線程池中。
有兩個不同的想法:
Async / await與前者有關,但是您的測試代碼告訴您有關后者的信息:處理這些任務的特定機制。
該機制由上下文決定。 一些上下文使用多個線程(但是請注意,上下文取決於它如何管理這些線程,包括是否以及何時創建新線程),但某些上下文僅使用1個線程。
使用了多個線程是與上下文有關的,而不是異步/等待。
我改變了你的例子:
public async static Task Test()
{
Console.WriteLine(Thread.CurrentThread.ManagedThreadId);
await Task.CompletedTask; // <--- instead of Task.Delay()
Console.WriteLine(Thread.CurrentThread.ManagedThreadId);
}
現在輸出為1、1、1。
await
未創建新線程。 Task.Delay
是元凶!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.