Concurrency API in Java
This is how the first thread-example looks like using executors:
ExecutorService executor = Executors.newSingleThreadExecutor();
executor.submit(() -> {
String threadName = Thread.currentThread().getName();
System.out.println(“Hello ” + threadName);
});
// => Hello pool-1-thread-1
The class Executors provides convenient factory methods for creating different kinds of executor services. In this sample we use an executor with a thread pool of size one.
The result looks similar to the above sample but when running the code you’ll notice an important difference: the java process never stops! Executors have to be stopped explicitly – otherwise they keep listening for new tasks.
An ExecutorService provides two methods for that purpose: shutdown() waits for currently running tasks to finish while shutdownNow() interrupts all running tasks and shut the executor down immediately.
This is the preferred way how I typically shutdown executors:
try {
System.out.println(“attempt to shutdown executor”);
executor.shutdown();
executor.awaitTermination(5, TimeUnit.SECONDS);
}
catch (InterruptedException e) {
System.err.println(“tasks interrupted”);
}
finally {
if (!executor.isTerminated()) {
System.err.println(“cancel non-finished tasks”);
}
executor.shutdownNow();
System.out.println(“shutdown finished”);
}