.NET framework provided two types for complication: TASK and Thread. Although both can be used for asynchronous execution code, there are fundamental differences between them, which will affect their applicability of specific scenes.
The difference between task and thread
In contrast, Task is a higher -level concept, which means that asynchronous operations and future results promises. TASK is lightweight. It is managed by a thread pool. The thread pool optimizes resources and reduces the overhead related to thread creation. Unlike threads, TASK does not have a dedicated stack, and shared resources in other TASK in the same thread in the thread pool.
When to use thread instead of task (vice versa)
In the following cases, use Thread:It is necessary to explicitly control the creation and execution of threads.
It requires long -term operation, which will take up special threads for a long time.
You need to manage threads local storage or communicate directly with the operating system.There is an asynchronous operation that does not require exclusive thread access, such as web service calls or database query. Want to use the Async/Await mode for asynchronous programming.
The above is the detailed content of Task vs. Thread in .NET: When Should I Use Which?. For more information, please follow other related articles on the PHP Chinese website!