DEV Community

Cover image for Threadpool, Thread

Posted on • Updated on • Originally published at shayankamalzadeh.Medium

Threadpool, Thread

Hello hello
Today I want to talk about Thread Threadpool and the type of Threads in .Net.
What is a thread pool?
Before talking about Threadpool, I have a question, Do you know about Thread??
So first let me talk about Thread.
A thread controls the flow of an executable program. By default, a program has one thread called Main
Thread. Main Thread starts when control enters in the Main method and it terminates when the Main method returns.
If the execution of a program is controlled by more than one thread, it’s called a Multithreaded

Application. Such a program increases the performance and response time of an application.
A thread can be created by using System.Threading.Thread class. A thread can only be manipulated on a method. For example, MainThread needs the Main method to control the flow of a program.
Thread Pool
The cost of instantiating a managed thread is higher than reusing a free thread. In .NET, a thread pool is helpful to reuse the free threads. A thread pool is a collection of background threads created by a system and are available to perform any task when required.
When a program requires an extra thread, it is more efficient to use available free threads from a thread pool because it can save the cost of creating a thread. And when a thread completes its execution, it can go back to the thread pool so other programs can reuse the same thread again.
Limitation of Thread Pool
• It is hard to tell when a thread of a thread pool has finished its execution.
• There is no “Start” method, so we cannot tell when a thread of a thread pool has started its execution because it is being managed by the system.
• It can’t manage a thread that returns a value.

As you see, I talked about the background thread.

We have two types of Thread, Background and Foreground Thread
There are two kinds of threads in C#, i.e., Foreground thread and Background thread. By default, in C# all threads are initialized as foreground thread. An application cannot terminate its execution until all its foreground threads are completed.
A background thread is almost identical to a foreground thread. The one difference is that, if the Main Thread has completed its execution and the background thread is the only thread remaining in the application, the Main Thread will terminate the application and not wait for the background thread to be completed.

Top comments (0)