Knowledge Guide
HomeConcurrencyConcurrency Foundations

Thread Pools, Executors & Futures

Stop creating threads by hand

Threads are expensive to create (an OS call, ~1 MB stack — see Go vs Java), and unbounded new Thread() under load leads straight to OutOfMemoryError. A thread pool creates a fixed set of worker threads once and feeds them tasks from a queue.

Caller submits tasks to a queue; a fixed set of worker threads pull and run them, returning Futures
Caller submits tasks to a queue; a fixed set of worker threads pull and run them, returning Futures

Executors & Futures (Java)

ExecutorService pool = Executors.newFixedThreadPool(8);
Future<Integer> f = pool.submit(() -> expensive());  // queued, runs on a worker
// ... do other work ...
int result = f.get();        // blocks until that task completes
pool.shutdown();             // stop accepting; let running tasks finish

submit returns a Future — a handle to a result that isn't ready yet. CompletableFuture chains them without blocking (thenApply, thenCompose).

How big should the pool be?

WorkloadSizeWhy
CPU-bound≈ number of coresmore threads than cores just adds context-switch overhead
IO-boundcores × (1 + wait/compute)threads sit blocked on IO, so you need more to keep cores busy

The dangerous pitfalls

In Go: no pool needed

Goroutines are cheap, so Go's idiom is the worker pool from the Go Concurrency lesson — N goroutines ranging over a jobs channel. To bound concurrency (the reason Java needs a fixed pool), use a counting semaphore channel:

sem := make(chan struct{}, 8)              // at most 8 in flight
for _, t := range tasks {
    sem <- struct{}{}                       // acquire (blocks if 8 busy)
    go func(t Task){ defer func(){ <-sem }(); handle(t) }(t)
}

Takeaways


Re-authored for this guide; pool diagram hand-authored as SVG. Follows Java Concurrency in Practice ch. 6–8. See also: Go Concurrency (worker pool), Go vs Java (thread cost), Deadlock.

🤖 Don't fully get this? Learn it with Claude

Stuck on Thread Pools, Executors & Futures? Open Claude, copy a block below, and it'll teach you this exact concept — visually and interactively.

🎨 Explain it visually

Build the mental picture, not memorization.

I just read a lesson on **Thread Pools, Executors & Futures** (Concurrency) and want to truly understand it. Explain Thread Pools, Executors & Futures from first principles using ONE vivid real-world analogy and a visual mental model — draw it as ASCII art or a clear step-by-step diagram — with a concrete example using real numbers. Then ask me one question to check I got the mental picture, and wait for my reply. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🤔 Walk me through it (interactive)

Socratic — adapts to where you're stuck.

Teach me **Thread Pools, Executors & Futures** interactively. Ask me ONE guiding question at a time, wait for my answer, and adapt to my confusion — build the idea with me step by step instead of explaining it all at once. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🧪 Quiz me & fix my gaps

Active recall exposes what you missed.

Quiz me on **Thread Pools, Executors & Futures** with 5 questions, easy to tricky, ONE at a time. Tell me if each answer is right; at the end, explain clearly what I got wrong and why. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🧠 Make it stick

Intuition + hook + flashcards for long-term memory.

Help me remember **Thread Pools, Executors & Futures** for the long term: give the one-sentence intuition, a memorable hook/mnemonic, a tiny worked example, and 3 active-recall flashcards (Q -> A). If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.

📝 My notes