Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Async await and threads [duplicate]

I am working with async-await and tasks, but I can't understand the one thing:

Is async task executes in separate thread?

As msdn says (Asynchronous programming):

The async and await keywords don't cause additional threads to be created. Async methods don't require multithreading because an async method doesn't run on its own thread.

But in the remarks in description of ThreadPool class (ThreadPool Class):

Examples of operations that use thread pool threads include the following:

When you create a Task or Task object to perform some task asynchronously, by default the task is scheduled to run on a thread pool thread.

So, now I don't understand if async task uses separate thread. Explain me please. Thanks.

like image 272
Roman Doskoch Avatar asked May 17 '16 13:05

Roman Doskoch


People also ask

Does async await run on different threads?

Instead, the expression signs up the rest of the method as a continuation and returns control to the caller of the async method. The async and await keywords don't cause additional threads to be created. Async methods don't require multithreading because an async method doesn't run on its own thread.

Does async use multiple threads?

Async programming is about non-blocking execution between functions, and we can apply async with single-threaded or multithreaded programming. So, multithreading is one form of asynchronous programming.

Is async better than threading?

Asyncio vs threading: Async runs one block of code at a time while threading just one line of code at a time. With async, we have better control of when the execution is given to other block of code but we have to release the execution ourselves.

Does async run on a separate thread?

No, it does not. It MAY start another thread internally and return that task, but the general idea is that it does not run on any thread.


2 Answers

To put it in a simple term Task can run on a different Thread as well as it can run on the caller Thread. Task will take a Thread from a ThreadPool ONLY when caller Thread does not have resources to run another Task. Task will be ran on caller Thread when it has enough resources to run another Task (idle mode).

Compared to a thread, a Task is a higher-level abstraction—it represents a concurrent operation that may or may not be backed by a thread. Tasks are compositional (you can chain them together through the use of continuations). They can use the thread pool to lessen startup latency, and with a TaskCompletionSource, they can leverage a callback approach that avoid threads altogether while waiting on I/O-bound operations.

Source: page 565 "C# 5.0 in a Nutshell" by Joseph Albahari, Ben Albahari.

like image 62
Karolis Kajenas Avatar answered Sep 28 '22 07:09

Karolis Kajenas


A Task does not necessarily represent an extra thread.

If you await a Task, you return the control flow to the caller of your method until "someone" sets the Task as completed.

If you start a Task via Task.Run() or Task.Factory.StartNew(), then the action you pass to these calls is executed on another thread (not a new one, but one from the ThreadPool).

like image 29
René Vogt Avatar answered Sep 28 '22 09:09

René Vogt