Reputation: 81781
ThreadPool keeps thread idle instead of creating and tearing them down, not all the time though based on how many threads it has a given time.
Even though I don't know the whole process during a thread creation and destruction, why is more expensive than having threads idle.
Upvotes: 2
Views: 347
Reputation: 116471
When creating a managed thread a lot of stuff happens. Obviously both the native and the managed data structures for the thread are created. Furthermore, managed threads commits their entire 1 MB stack space and additional per thread data structures are allocated. In short this is a very expensive allocation.
By keeping threads around and reusing them (e.g. via the thread pool) this cost is amortized. That being said you still don't want to have an excessive number of threads just hanging around, cause they do take up resources even when idle. Consequently the thread pool will terminate idle threads periodically to reclaim resources.
Upvotes: 2
Reputation: 8818
Well, because put simply, an idle thread isn't doing anything. It's just waiting around for work to do. Creating a thread involves allocating memory, load balancing...all kinds of stuff behind the scenes.
Upvotes: 3
Reputation: 340366
Simply because idle threads don't do anything.
Creating a thread performs a bunch of work (allocating memory, creating the data structures necessary to manage the thread, etc.). All of that work is done once for a pooled thread, instead of performing it once each time there's work for a thread.
Upvotes: 1