Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How is a thread pool able to re-use threads?

Our current course assignment specifies that we are supposed to create a manager for a thread pool using the "Object Pool Manager" design pattern which spawns a set amount of threads. The ownership of these threads shall be transferred to the client and then back to the pool after the client has finished using it. If no thread exists in the pool then the client has to wait.

My confusion comes from the fact that a thread is supposedly not reusable, which defeats the purpose of pooling them. Have I understood the assignment incorrectly?

like image 695
fabbe680 Avatar asked Mar 10 '26 10:03

fabbe680


1 Answers

Threads are reusable as long as they have not ended. A pool of threads generally involves threads that do work as it is given to them, and then wait for more work. Thus, they never end until explicitly told to do so. The trick is designing them in a way such that the work they are given ends, but the thread itself does not. Thread pools are useful because it is often relatively expensive to create/destroy threads.

like image 184
kaliatech Avatar answered Mar 12 '26 00:03

kaliatech