Lesson 28: ExecutorService & Thread Pools

29 Jul 2026 1 min Swarnil Singhai

ExecutorService manages a pool of reusable threads instead of creating a new OS thread per task — far cheaper at scale.

Real-time example

An image-processing service submits 1000 resize jobs to a fixed pool of 8 threads instead of spawning 1000 raw threads.

ExecutorService pool = Executors.newFixedThreadPool(4);
for (int i = 0; i < 8; i++) {
    int jobId = i;
    pool.submit(() -> System.out.println("Resizing image " + jobId));
}
pool.shutdown();

What's happening

Only 4 threads ever exist; the other 4 jobs wait in a queue until a thread frees up — bounded resource usage under load.

Always call shutdown() or the JVM won't exit cleanly.

Advertisement