Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is new Task always executed on ThreadPool thread?

This is probably an easy and dumb question. I create a task like this:

Task<bool> myTask = new Task<bool>(() => { Debug.WriteLine("Task fired"); return true; });
// I know I can create it with Task.Run, but this is for purpose of the sample
myTask.Start(); 

and I've few questions about this:

  • Does it always run on ThreadPool thread?
  • If it runs on ThreadPool is there a chance that it will be run by UI thread? And in case of havey job - can block it?
  • In case of large number of tasks, can few be assigned to one thread (queued) and then run one after another? Or every task has its own thread?

I've read some documentation, but I failed to find concrete explanation. For example Task documentation says generally about Tasks:

Because the work performed by a Task object typically executes asynchronously on a thread pool thread rather than synchronously on the main application thread...

like image 410
Romasz Avatar asked Dec 08 '15 11:12

Romasz


People also ask

Does task run Use the ThreadPool?

By default, TPL types like Task and Task<TResult> use thread pool threads to run tasks. You can also use the thread pool by calling ThreadPool.

Does task run use new thread?

NET code does not mean there are separate new threads involved. Generally when using Task. Run() or similar constructs, a task runs on a separate thread (mostly a managed thread-pool one), managed by the . NET CLR.

What is the difference between thread and ThreadPool?

A thread pool is - as the name suggests - a pool of worker threads which are always running. Those threads then normally take tasks from a list, execute them, then try to take the next task. If there's no task, the thread will wait.

How does a ThreadPool work?

How a Thread Pool Works. Instead of starting a new thread for every task to execute concurrently, the task can be passed to a thread pool. As soon as the pool has any idle threads the task is assigned to one of them and executed.


2 Answers

does it always run on ThreadPool thread?

Not necessarily. If you take a look at the Task constructor overload taking a TaskCreationOptions, you can pass the value TaskCreationOptions.LongRunning. If internally it uses TaskScheduler.Default, this will create a new thread which isn't one of the threadpool's.

Generally, it is recommended that you use Task.Run for queuing threads on the thread-pool. If you want to pass a custom TaskScheduler, you can use the more advanced Task.Factory.StartNew, but I'd recommend only using it when really needed.

if it runs on ThreadPool is there a chance that it will be run by UI thread? And in case of havey job - can block it?

No. The UI thread isn't part of the pool used by the ThreadPool.

in case of large number of tasks, can few be assigned to one thread (queued) and then run one after another? Or every task has its own thread?

That is implementation specific to the TaskScheduler being used. If we look at the ThreadPoolTaskScheduler (which is the default in case no custom one is passed) the threadpool starts with a constant amount of threads, and scales as it needs. It is not guaranteed that each delegate will execute on a different thread. You can, however, create a custom TaskScheduler, where you control the mechanism of scheduling tasks for exectuion.

like image 136
Yuval Itzchakov Avatar answered Oct 07 '22 11:10

Yuval Itzchakov


To answer the first point: "Beginning with the .NET Framework 4, the easiest way to use the thread pool is to use the Task Parallel Library (TPL). By default [emphasise added], parallel library types like Task and Task use thread pool threads to run tasks."

https://msdn.microsoft.com/en-us/library/0ka9477y(v=vs.110).aspx

like image 22
C. Knight Avatar answered Oct 07 '22 13:10

C. Knight