1
votes

I have read in so many places, that Thread pools reduce the thread creation overhead which results in improved performance. But once a thread completes executing its run method, it goes to Dead/Terminated state which means it can be restarted again.

So, how does thread pool handles releasing of threads? Does it really holds the thread some way for serving next task or internally it creates new thread every time a task is submitted?

2
It reuses the threads it creates. That's how it reduces thread creation. It's documented, and the source code is available.JB Nizet
It does not release them. It keeps them on an available list when not in use.Kevin
I got it. It never allows the thread to reach the terminated state unless until its required to terminate the thread.Kumar V

2 Answers

1
votes

Thread pool internally has list which is a place holder for threads which is being used as and when there are requirements and adds them to this list when it completes the execution or creates a new one when one terminates and maintains them by growing in number or reducing based on whether you asked for cached or fixed thread pool.

0
votes

Fixed size thread pools keep the threads until shutdown.

The Cached thread pool keeps the thread for 60 seconds by default.