What is the difference between using a new thread and using a thread from the thread pool? What performance benefits are there and why should I consider using a thread from
I was curios about the relative resource usage for these and and ran a benchmark on my 2012 dual-core Intel i5 laptop using .net 4.0 release build on windows 8. Thread Pools took on average 0.035ms to start where Threads took an average of 5.06ms. In other words Thread in the pool started about 300x faster for large numbers of short lived threads. At least in the tested range (100-2000) threads, the total time per thread seemed pretty constant.
This is the code that was benchmarked:
for (int i = 0; i < ThreadCount; i++) {
Task.Run(() => { });
}
for (int i = 0; i < ThreadCount; i++) {
var t = new Thread(() => { });
t.Start();
}
If you need a lot of threads, you probably want to use a ThreadPool. They re-use threads saving you the overhead of thread creation.
If you just need one thread to get something done, Thread is probably easiest.
Thread local storage is not a good idea with thread pools. It gives threads an "identity"; not all threads are equal anymore. Now thread pools are especially useful if you just need a bunch of identical threads, ready to do your work without creation overhead.
Check here for an earlier thread:
When should I not use the ThreadPool in .Net?
Summary is that Threadpool is good if you need to spawn many shortlived threads, whereas using Threads gives you a bit more control.
Thread:
Thread-Pool:
also
new Thread().Start()
spawns Foreground thread that will not die if you close your program. ThreadPool threads are background threads that die when you close the app.