ExecutorService for C#?

Y

Yexiong Feng

Hi All,

I am looking for a Java ExecutorService equivalent in C#. Basically what I
need is a library that can manage a queue of tasks, execute them with a fixed
size thread pool, and return the result to the external caller that queues
the tasks. Anyone know about anything similar?

Thanks,
Feng
 
A

Arne Vajhøj

Yexiong said:
I am looking for a Java ExecutorService equivalent in C#. Basically what I
need is a library that can manage a queue of tasks, execute them with a fixed
size thread pool, and return the result to the external caller that queues
the tasks. Anyone know about anything similar?

..NET has a ThreadPool class.

Arne
 
Y

Yexiong Feng

ThreadPool is a static class. If I want let the ThreadPool to perform
different type of tasks, they will interfere with each other. Is there a
better solution?

Thanks,
Feng
 
A

Arne Vajhøj

ThreadPool is a static class. If I want let the ThreadPool to perform
different type of tasks, they will interfere with each other. Is there a
better solution?

A single ThreadPool can execute different types of tasks.

But if you want multiple thread pools, then I think you are
out of luck and will have to create your own.

It is not that difficult. I have some code somewhere, if
you are interested, but it is literally not many lines
of code.

Arne
 
P

Peter Duniho

ThreadPool is a static class. If I want let the ThreadPool to perform
different type of tasks, they will interfere with each other. Is there a
better solution?

As Arne says, the threads in the .NET ThreadPool class will, for the most
part, interfere with each to no greater degree than threads from multiple
thread pool classes. Theoretically, the issues using ThreadPool would be:

-- Reaching the maximum number of active threads for the pool. In the
current versions of .NET, this maximum number is very high (if I recall
correctly, 250 threads per CPU core). You likely run into lots of other
problems related only to the number of current threads long before you max
out the thread pool.

-- Throttled activations of threads. The ThreadPool class will only
add one new thread after a certain delay of having a non-empty task queue
(half a second, if I recall correctly). Over a period of time, the
ThreadPool will eventually stabilize and this won't be an issue, but it
might delay throughput initially.

In practice, neither of these issues are usually an actual problem,
assuming your workload is designed sensibly and fits the hardware
configuration well.

All that said, if you still feel a need to have a thread pool class with
more flexibility than the built-in one, you might check out Jon Skeet's
custom thread pool class, found in his "MiscUtil" library:
http://www.yoda.arachsys.com/csharp/miscutil/

Pete
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top