Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java's ThreadPoolExecutor equivalent for C#?

Tags:

c#

threadpool

I used to make good use of Java's ThreadPoolExecutor class and have yet to find a good equivalent in C#. I know of ThreadPool.QueueUserWorkItem which is useful in many cases but no good if you want to control the number of threads assigned to a task or have multiple individual queues for different task types.

For example I liked to use a ThreadPoolExecutor with a single thread to guarantee sequential execution of asynchronous calls.. Is there an easy way to do this in C#? Is there a non-static thread pool implementation?

like image 454
chillitom Avatar asked May 03 '10 15:05

chillitom


People also ask

What is Corepoolsize and max pool size in ThreadPoolExecutor?

The default configuration of core pool size is 1, max pool size and queue capacity as 2147483647. This is roughly equivalent to Executors. newSingleThreadExecutor(), sharing a single thread for all tasks.

What is ThreadPoolExecutor?

ThreadPoolExecutor is an ExecutorService to execute each submitted task using one of possibly several pooled threads, normally configured using Executors factory methods. It also provides various utility methods to check current threads statistics and control them.

What is C# thread pool?

Thread pool in C# is a collection of threads. It is used to perform tasks in the background. When a thread completes a task, it is sent to the queue wherein all the waiting threads are present. This is done so that it can be reused.

What is Keepalivetime in ThreadPoolExecutor?

Returns the thread keep-alive time, which is the amount of time which threads in excess of the core pool size may remain idle before being terminated.


1 Answers

Until .Net 4.0 and the TPL, there is no such feature built-in.

However, see this artcle

like image 187
SLaks Avatar answered Oct 16 '22 10:10

SLaks