Course contentsShow
← Java
Lesson 2720 of 2,87050. Executor FrameworkFree lesson

newFixedThreadPool: Creating a Fixed-Size Thread Pool

How newFixedThreadPool(int) creates a pool with a fixed number of threads that are reused for submitted tasks.

newFixedThreadPool: Creating a Fixed-Size Thread Pool

What you'll learn: How to create a thread pool with a fixed number of reusable threads using Executors.newFixedThreadPool(int).

The Core Idea

Executors.newFixedThreadPool(int n) creates a thread pool containing exactly n threads. These threads persist for the lifetime of the pool, handling submitted tasks one after another. When all threads are busy, new tasks wait in a queue until a thread becomes available.

Think of it like a restaurant kitchen with a fixed number of chefs. If you have 5 chefs, they can cook 5 dishes simultaneously. When orders pile up, they're queued and prepared as chefs finish their current dishes. The kitchen never hires more chefs or sends any home—it's always those same 5 workers.

How It Works

ExecutorService executor = Executors.newFixedThreadPool(4);

// Submit tasks (assuming Runnable tasks exist)
executor.execute(task1);
executor.execute(task2);
executor.execute(task3);
executor.execute(task4);
executor.execute(task5); // Waits in queue
executor.execute(task6); // Waits in queue

executor.shutdown();

In this example, 4 threads run task1 through task4 concurrently. task5 and task6 wait in the queue. As soon as a thread finishes its task, it immediately picks up the next queued task—no thread is destroyed and recreated.

Key Benefits

  • Predictable resource usage: You control exactly how many threads exist
  • Thread reuse: Avoids the overhead of creating and destroying threads
  • Automatic queuing: Tasks wait their turn without additional code
  • Bounded concurrency: Limits parallel execution to prevent system overload

Key Takeaway: newFixedThreadPool(int) creates a specified number of worker threads that continuously process submitted tasks from a queue, providing predictable, efficient thread reuse without creating new threads for each task.