Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

How would one implement a threadpool? I've been reading on wikipedia for "threadpool" but I still can't figure out what one should do to solve this question (possibly because I didn't quite understand what a threadpool is in simple terms).

Can someone explain me in plain english what a threadpool is and how would one answer this question?

share|improve this question
One way to visualize (and get an idea of what goes on in a thread pool) of a real system - the apache mod_status page on – user40980 Nov 5 '12 at 17:59
up vote 50 down vote accepted

A thread pool is a group of pre-instantiated, idle threads which stand ready to be given work. These are preferred over instantiating new threads for each task when there is a large number of short tasks to be done rather than a small number of long ones. This prevents having to incur the overhead of creating a thread a large number of times.

Implementation will vary by environment, but in simplified terms, you need the following:

  • A way to create threads and hold them in an idle state. This can be accomplished by having each thread wait at a barrier until the pool hands it work. (This could be done with mutexes as well.)
  • A container to store the created threads, such as a queue or any other structure that has a way to add a thread to the pool and pull one out.
  • A standard interface or abstract class for the threads to use in doing work. This might be an abstract class called Task with an execute() method that does the work and then returns.

When the thread pool is created, it will either instantiate a certain number of threads to make available or create new ones as needed depending on the needs of the implementation.

When the pool is handed a Task, it takes a thread from the container (or waits for one to become available if the container is empty), hands it a Task, and meets the barrier. This causes the idle thread to resume execution, invoking the execute() method of the Task it was given. Once execution is complete, the thread hands itself back to the pool to be put into the container for re-use and then meets its barrier, putting itself to sleep until the cycle repeats.

share|improve this answer
A thread pool is a group of pre-instantiated, idle threads which stand ready to be given work. [...] This prevents having to incur the overhead of creating a thread a large number of times. - this should be spit by Google whenever someone searches for "thread pool" – Rafael Eyng May 30 '15 at 14:56

Thread pool is a collection of managed threads usually organized in a queue, which execute the tasks in the task queue.

Creating a new thread object every time you need something to be executed asynchronously is expensive. In a thread pool you would just add the tasks you wish to be executed asynchronously to the task queue and the thread pool takes care of assigning an available thread, if any, for the corresponding task. As soon as the task is completed, the the now available thread requests another task (assuming there is any left).

Thread pool helps you avoid creating or destroying more threads, than would really be necessary.

I would start by creating a class with a queue of threads and a queue of tasks. Then implement a method which adds a task to the task queue and move on from there. Obviously, you should also make it possible to set the maximum allowed threads in a thread pool.

share|improve this answer

In multithreaded application, thread pool is a "pool of available threads" that can be used by your application. Usually, e.g. .NET, it is all managed so you just assign tasks and once a thread is free, it is going to perform it. So to implement a threadpool, I would expect to create a concept where tasks are automatically taken by free threads without explicit thread creation for each task.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.