Multithreading: shutdown lets queued work finish, shutdownNow does not

shutdown() stops the pool from accepting new tasks but still runs everything already queued to completion. Only after awaitTermination confirms that is it safe to read state the tasks wrote.

Code
ExecutorService pool = Executors.newFixedThreadPool(1);
List<Integer> completed = new ArrayList<>();
pool.submit(() -> completed.add(1));
pool.submit(() -> completed.add(2));
pool.shutdown();
pool.awaitTermination(1, TimeUnit.SECONDS);
System.out.println("Completed after shutdown(): " + completed);
Output
Completed after shutdown(): [1, 2]
Advertisement
More in JAVA

Run this yourself in the Online Java Compiler, spin up a live REST API in the API Sandbox, or practise with Java interview questions.

Published 2026-09-27

© Java Coding Hub · About · Contact · Privacy · Terms