← Lessons

quiz vs the machine

Gold1380

Concurrency

The Thread Pool Executor

Reusing threads instead of spawning them per task.

5 min read · core · beat Gold to climb

Why pool threads

Creating a thread per task is expensive: stacks, scheduler entries, and context switches add up. A thread pool executor keeps a fixed set of worker threads alive and feeds them tasks from a shared queue.

Core parameters

  • Core pool size is the number of always warm threads.
  • Maximum pool size caps how many threads may exist under load.
  • Keep alive time lets extra threads above the core size retire when idle.

When all core threads are busy, new tasks go onto the work queue. Only when that queue is full does the pool grow toward its maximum.

The lifecycle of a task

Sizing the pool

For CPU bound work, size near the core count so threads are not fighting for cycles. For IO bound work you can run many more threads because most sit waiting. Measuring throughput beats guessing.

Key idea

A thread pool reuses a bounded set of workers, queues overflow tasks, and only grows toward its maximum when the queue fills.

Check yourself

Answer to earn rating on the learn ladder.

1. What happens when all core threads are busy but the queue has room?

2. How should you size a pool for CPU bound work?