Repaso
Español
Java CoreIntermediateJV-27

Why use an ExecutorService (thread pool) instead of new Thread() for every task?

Options

  1. Threads cannot be created with the new keyword at all
  2. Threads are costly; a pool reuses a few and queues the rest (Correct option)
  3. Executors make the code run on the GPU instead of the CPU
  4. A pool guarantees that tasks never fail or throw exceptions

Answer

Creating threads is expensive and unlimited threads can exhaust memory; a pool reuses a fixed number of threads and queues extra tasks

Why

Each thread uses memory (around 1 MB of stack) and the OS needs time to create it. If a server started a new thread per request, a traffic spike could create thousands and crash.

An ExecutorService keeps a limited set of worker threads that take tasks from a queue and are reused. You submit tasks and get a Future for the result. Always shut it down (shutdown()) when finished.

ExecutorService pool = Executors.newFixedThreadPool(4);
Future<Integer> f = pool.submit(() -> 21 * 2);
System.out.println(f.get());   // 42
pool.shutdown();

Remember

Thread pool = reuse a bounded set of threads.

Key terms

Practice this question in Repaso