The isShutdown
method of ExecutorService
in Java
is used to check if the executor service is shut down or not.
boolean isShutdown();
This method has no parameters.
This method returns true
if the executor service is shut down. Otherwise, returns false.
Let’s look at the code below:
import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;import java.util.concurrent.TimeUnit;public class Main {public static void main(String[] args) throws InterruptedException {ExecutorService singleThreadExecutor = Executors.newSingleThreadExecutor();singleThreadExecutor.submit(() -> System.out.println("Hello Educative"));if(!singleThreadExecutor.isShutdown()){singleThreadExecutor.shutdown();singleThreadExecutor.awaitTermination(1, TimeUnit.SECONDS);}System.out.println("Executor service shut down - " + singleThreadExecutor.isShutdown());}}
singleThreadExecutor
.singleThreadExecutor
that prints a string.singleThreadExecutor
shuts down. If it’s not shut down yet, the shutdown
method is invoked on the service. Finally, we wait for the termination of the service for one second using the awaitTermination
method.