Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When should I not use the ThreadPool in .Net? [closed]

When should I not use the ThreadPool in .Net?

It looks like the best option is to use a ThreadPool, in which case, why is it not the only option?

What are your experiences around this?

like image 925
Vaibhav Avatar asked Aug 13 '08 19:08

Vaibhav


People also ask

When should you not use ThreadPool?

Thread pools do not make sense when you need thread which perform entirely dissimilar and unrelated actions, which cannot be considered "jobs"; e.g., One thread for GUI event handling, another for backend processing. Thread pools also don't make sense when processing forms a pipeline.

Why is ThreadPool needed?

If you have short tasks that require background processing, the managed thread pool is an easy way to take advantage of multiple threads. Use of the thread pool is significantly easier in Framework 4 and later, since you can create Task and Task<TResult> objects that perform asynchronous tasks on thread pool threads. .

How do you close a ThreadPool?

shutdownNow() should be used to shutdown the thread pool to gracefully exiting the application.


1 Answers

@Eric, I'm going to have to agree with Dean. Threads are expensive. You can't assume that your program is the only one running. When everyone is greedy with resources, the problem multiplies.

I prefer to create my threads manually and control them myself. It keeps the code very easy to understand.

That's fine when it's appropriate. If you need a bunch of worker threads, though, all you've done is make your code more complicated. Now you have to write code to manage them. If you just used a thread pool, you'd get all the thread management for free. And the thread pool provided by the language is very likely to be more robust, more efficient, and less buggy than whatever you roll for yourself.

Thread t = new Thread(new ThreadStart(DoSomething));   t.Start();   t.Join();   

I hope that you would normally have some additional code in between Start() and Join(). Otherwise, the extra thread is useless, and you're wasting resources for no reason.

People are way too afraid of the resources used by threads. I've never seen creating and starting a thread to take more than a millisecond. There is no hard limit on the number of threads you can create. RAM usage is minimal. Once you have a few hundred threads, CPU becomes an issue because of context switches, so at that point you might want to get fancy with your design.

A millisecond is a long time on modern hardware. That's 3 million cycles on a 3GHz machine. And again, you aren't the only one creating threads. Your threads compete for the CPU along with every other program's threads. If you use not-quite-too-many threads, and so does another program, then together you've used too many threads.

Seriously, don't make life more complex than it needs to be. Don't use the thread pool unless you need something very specific that it offers.

Indeed. Don't make life more complex. If your program needs multiple worker threads, don't reinvent the wheel. Use the thread pool. That's why it's there. Would you roll your own string class?

like image 56
Derek Park Avatar answered Sep 22 '22 09:09

Derek Park