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

@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?

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)