Java - Quick Tips & Tutorials #1

ExecutorService executorService = Executors.newCachedThreadPool();
// use your executor
// ...

// shutdown if you are done with it
executorService.shutdown(); // --> let executor know that it will no longer accept new tasks

try {
if (!executorService.awaitTermination(1, TimeUnit.SECONDS)) {
executorService.shutdownNow(); // --> sends interrupt signal to all threads
}
} catch (InterruptedException e) {
// current thread may get interrupted waiting termination,
// in this case, we should send interrupt to running threads
executorService.shutdownNow();
Thread.currentThread().interrupt();
}

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Ömer Kurular

Ömer Kurular

221 Followers

I am Ömer and currently working as a full-time software engineer. I will share my knowledge with you I gained through years of professional and self working.