6 · Advanced · lesson 35 of 41

ExecutorService

Thread pools — the right way to run many tasks.

Java
ExecutorService pool = Executors.newFixedThreadPool(4);

Future<Integer> f = pool.submit(() -> heavyWork());
Integer result = f.get();     // blocks until done

pool.shutdown();
Key takeaways
  • ✓Don't hand-roll threads — submit tasks to a pool.
  • ✓Always shutdown() the executor.