Skip to content

Instantly share code, notes, and snippets.

@tomatophobia
Last active July 29, 2023 18:59
Show Gist options
  • Save tomatophobia/d9ecee3560a9e942e3a162e21c083c3e to your computer and use it in GitHub Desktop.
Save tomatophobia/d9ecee3560a9e942e3a162e21c083c3e to your computer and use it in GitHub Desktop.
void shutdownAndAwaitTermination(ExecutorService pool) {
pool.shutdown(); // (1)
try {
if (!pool.awaitTermination(60, TimeUnit.SECONDS)) { // (2)
pool.shutdownNow(); // (3)
if (!pool.awaitTermination(60, TimeUnit.SECONDS)) // (4)
System.err.println("Pool did not terminate"); // (5)
}
} catch (InterruptedException ex) { // (6)
pool.shutdownNow(); // (7)
Thread.currentThread().interrupt(); // (8)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment