Below code is from https://blog.stephencleary.com/2012/02/async-and-await.html I just added some descriptive methods
class Program
{
async static Task Main(string[] args)
{
Console.WriteLine(Thread.CurrentThread.ManagedThreadId); //thread id is 1 here
await DoSomethingAsync();
Console.WriteLine("do other workzzz");
Console.WriteLine(Thread.CurrentThread.ManagedThreadId); //thread id is 4 here
Console.ReadLine();
}
public static async Task DoSomethingAsync()
{
await Task.Delay(2000);
}
}
and the author says:
I like to think of “await” as an “asynchronous wait”. That is to say, the async method pauses until the awaitable is complete (so it waits), but the actual thread is not blocked (so it’s asynchronous).
so my questions is:
why the actual thread(thread id 1 in my case) is not blocked?
From my perspective, a thread is blocked because further statements will not be executed until current method has finished. We can see that Console.WriteLine("do other workzzz");
won't be executed until DoSomethingAsync() finishes, isn't it kind of blocking?
Another important thing to notice is, after DoSomethingAsync();
finishes, thread id changes from 1 to 4, there is no "actual thread" anymore. Why thread 1 disappear? shouldn't it be the thread 4 to disappear?