ExecutorService

For Java Certification Exam Practice Questions, refer http://talks.skilltoz.com/java-11-certification-exam-questions/

In this article, let us discuss the ExecutorService in Java, from the Java certification perspective.

ExecutorService defines thread executors that can be used for running tasks in asynchronous mode. It provides a pool of threads and an API for assigning tasks to it.

How do we get an instance of the ExecutorService?

The Executors factory class can be used to create instances of the ExecutorsService object.

For example, the following line of code will create a thread pool with 5 threads:

ExecutorService executor = Executors.newFixedThreadPool(5);

Executors class defines other factory methods also to create a predefined ExecutorService.

How do we assign tasks to the ExecutorService?

We can assign Runnable and Callable tasks to the ExecutorService.

Runnable printData = () -> System.out.println("Printing data");
ExecutorService service = Executors.newSingleThreadExecutor();
service.execute(printData);

How do we shutdown the ExecutorService?

As the thread executor is not automatically terminated, it is recommended to call the shutdown() method after using an ExecutorService instance. The ExecutorService will not be destroyed as soon as the shutdown() method is called. However, it will cause the ExecutorService to stop accepting new tasks and shut down after all running threads finish their current tasks.

What will happen if a new task is given to the executor after calling the shutdown() method?

If a new task is submitted to the thread executor while it is shutting down, a RejectedExecutionException will be thrown.

How do we know if a thread executor has been shutdown?

If the shutdown() method is called, the isShutdown() method will return true even if any of the tasks are still running. However, the isTerminated() method will return true only if the isShutdown() method is called and all the tasks have ended.

How do we wait for all the tasks to finish after an ExecutorService is shut down?

Once we shut down the thread executor using the shutdown() method, we can call the awaitTermination() method that waits for a given time to complete all tasks. This method waits until all the tasks have completed execution after a shutdown request, or the timeout occurs, or the current thread is interrupted, whichever happens first.

Runnable printData = () -> System.out.println("Printing data");
    ExecutorService service = null;
    try {
        service = Executors.newSingleThreadExecutor();
        service.execute(printData);

    } finally {
    // Shut down the service 
        service.shutdown();
    }
    service.awaitTermination(1, TimeUnit.MINUTES);

    // Check whether all tasks are finished
    if (service.isTerminated())
        System.out.println("Terminated");
    else
        System.out.println("Not Terminated");

Refer Oracle’s official documentation to learn more about thread executors.

Leave a Reply

Your email address will not be published. Required fields are marked *