ArticleslgStudy

computer science

Thread pool

Thread pool is a computer science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Thread pool rather than just read about it. In short: In computer programming, a thread pool is a software design pattern for achieving concurrency of execution in a computer program. Often also called a replicated workers or worker-crew model, a thread pool maintains multiple threads waiting for tasks to be allocated for concurrent execution by the supervising program.

Thread pool — main illustration
Thread pool — illustration

Key takeaways

  • Thread pool belongs to computer science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Thread pool to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Thread pool from memory before moving on to harder problems.

Reference excerpt

In computer programming, a thread pool is a software design pattern for achieving concurrency of execution in a computer program. Often also called a replicated workers or worker-crew model, a thread pool maintains multiple threads waiting for tasks to be allocated for concurrent execution by the supervising program. By maintaining a pool of threads, the model increases performance and avoids latency in execution due to frequent creation and destruction of threads for short-lived tasks. Another good property - the ability to limit system load, when we use fewer threads than available. The number of available threads is tuned to the computing resources available to the program, such as a parallel task queue after completion of execution.

Performance The size of a thread pool is the number of threads kept in reserve for executing tasks. It is usually a tuneable parameter of the application, adjusted to optimize program performance. Deciding the optimal thread pool size is crucial to optimize performance. One benefit of a thread pool over creating a new thread for each task is that thread creation and destruction overhead is restricted to the initial creation of the pool, which may result in better performance and better system stability. Creating and destroying a thread and its associated resources can be an expensive process in terms of time. An excessive number of threads in reserve, however, wastes memory, and context-switching between the runnable threads invokes performance penalties. A socket connection to another network host, which might take many CPU cycles to drop and re-establish, can be maintained more efficiently by associating it with a thread that lives over the course of more than one network transaction. Using a thread pool may be useful even putting aside thread startup time. There are implementations of thread pools that make it trivial to queue up work, control concurrency and sync threads at a higher level than can be done easily when manually managing threads. In these cases the performance benefits of use may be secondary. Typically, a thread pool executes on a single computer. However, thread pools are conceptually related to server farms in which a master process, which might be a thread pool itself, distributes tasks to worker processes on different computers, in order to increase the overall throughput. Embarrassingly parallel problems are highly amenable to this approach. The number of threads may be dynamically adjusted during the lifetime of an application based on the number of waiting tasks. For example, a web server can add threads if numerous web page requests come in and can remove threads when those requests taper down. The cost of having a larger thread pool is increased resource usage. The algorithm used to determine when to create or destroy threads affects the overall performance:

Creating too many threads wastes resources and costs time creating the unused threads. Destroying too many threads requires more time later when creating them again. Creating threads too slowly might result in poor client performance (long wait times). Destroying threads too slowly may starve other processes of resources.

In languages In bash implemented by --max-procs / -P in xargs, for example:

In Go, called worker pool, with buffered channels:

It will print:

With unbuffered channels:

See also

Asynchrony (computer programming) Object pool pattern Concurrency pattern Grand Central Dispatch Parallel Extensions (.NET) Parallelization Server farm Staged event-driven architecture

References

External links "Query by Slice, Parallel Execute, and Join: A Thread Pool Pattern in Java" by Binildas C. A. "Thread pools and work queues" by Brian Goetz "A Method of Worker Thread Pooling" by Pradeep Kumar Sahu "Work Queue" by Uri Twig: C++ code demonstration of pooled threads executing a work queue. "Windows Thread Pooling and Execution Chaining" "Smart Thread Pool" by Ami Bar "Programming the Thread Pool in the .NET Framework" by David Carmona "Creating a Notifying Blocking Thread Pool in Java" by Amir Kirsh "Practical Threaded Programming with Python: Thread Pools and Queues" by Noah Gift "Optimizing Thread-Pool Strategies for Real-Time CORBA" by Irfan Pyarali, Marina Spivak, Douglas C. Schmidt and Ron Cytron "Deferred cancellation. A behavioral pattern" by Philipp Bachmann "A C++17 Thread Pool for High-Performance Scientific Computing" by Barak Shoshany

Illustrations

Thread pool: A sample thread pool (green boxes) with waiting tasks (blue) and completed tasks (yellow)
A sample thread pool (green boxes) with waiting tasks (blue) and completed tasks (yellow)

Worked examples

Example 1 — a first encounter with Thread pool

Start with the simplest possible case. Write down what Thread pool claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In computer science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Thread pool before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Thread pool ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Thread pool

In research
Thread pool appears in computer science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Thread pool in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Thread pool is common in secondary-school and first-year university syllabi. It links to neighbouring topics Concurrent computing, Parallel computing, Software design patterns, so understanding it makes those chapters shorter.
In everyday life
Look for Thread pool outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Thread pool” →

How to study Thread pool in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Thread pool means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Thread pool out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Thread pool in simple terms?

In computer programming, a thread pool is a software design pattern for achieving concurrency of execution in a computer program. Often also called a replicated workers or worker-crew model, a thread pool maintains multiple threads waiting for tasks to be allocated for concurrent execution by the s…

Why does Thread pool matter?

Because it connects several computer science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Thread pool?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Thread pool.

Tags

  • Concurrent computing
  • Parallel computing
  • Software design patterns
  • Threads (computing)

Keep exploring