and : The trap of the deadlock await
Task.Wait
In asynchronous programming, understanding the difference between
is very important. This article will analyze a case that uses to cause dead locks. await
Task.Wait
Task.WaitAll
: Synchronous blocking
will block the current thread simultaneously until the task is completed. In the example code, Waiting for the 10 tasks returned by the Task.Wait
method to complete. This will block threads and prevent them from performing follow -up operations.
: Waiting asynchronous Task.Wait
Task.WaitAll
Ros()
The allowable method will return the unfinished tasks to the caller while waiting for the task asynchronous. When the task is completed, the remaining code in the method is scheduled to be scheduled to be followed.
Dead lock scene await
In the example code, the
, and the await
method calls the asynchronous method
to create a series of asynchronous operations. This effectively prevented the task from completing and release of threads. As a result, the method will never exit, resulting in dead locks. The obstruction operation in the asynchronous code
Get
It is usually not recommended to use obstruction in asynchronous code. When the blocking occurs, the thread cannot handle the inlet request, resulting in a decline in performance and might be a deadlocked lock. Task.WaitAll
Ros
Conclusion Foo
Bar
The difference between Get
understanding and
It should only be used in specific scenes that need to be synchronized. In most cases, it is recommended to use a "complete asynchronous" method to use to maintain asynchronous and prevent blocking problems in the entire code.
The above is the detailed content of Await vs. Task.Wait: When Does Synchronous Blocking Create a Deadlock?. For more information, please follow other related articles on the PHP Chinese website!