The ThreadPoolExecutor class in Java is responsible for executing and managing a task in an effective manner. The already-created threads execute the tasks otherwise new threads are created to execute the task. In Java, the ThreadPoolExecutor class provides several methods for managing tasks scheduled for execution, and one of these methods is the shutdown() method.
In this write-up, we will demonstrate one of the most important methods of the ThreadPoolExecutor class, which is the shutdown() method.
What is the Need for ThreadPoolExecutor shutdown() Method in Java?
The thread pool executor belongs to the executor service that is responsible for running the tasks and completing the execution using the threads. Certain methods are available to execute these tasks. When the tasks have been submitted for execution there arises the need to shut down the tasks to avoid any mishap for the execution of other tasks, therefore the shutdown() method helps to finish the already accepted.
Implementation of ThreadPoolExecutor shutdown() Method in Java
The shutdown() method is responsible for initiating the termination of the tasks that were previously submitted for execution. This method basically deals with the tasks that were already submitted for the execution and no fresh tasks are being accepted.
The syntax of the shutdown() method is depicted below:
Public void shutdown()
Note: ThreadPoolExecutor class offers another handy method named the termination() method that checks if all tasks have been terminated.
Example: Implementation of shutdown() Method of ThreadPoolExecutor
The shutdown() method is implemented in the code below. In this example, the number of tasks provided for execution is 3. If the threads are currently running the tasks, the shutdown() method will return “false”. The threads that have been completed running will be printed on the screen:
import java.util.concurrent.*;
class shutdownmethod{
public static void main(final String[] arguments) throws InterruptedException {
//The cached thread pool for the creation of new threads or the reuse of previous ones.
ThreadPoolExecutor ex =(ThreadPoolExecutor)Executors.newCachedThreadPool();
//Three tasks have been submitted.
ex.submit(new newtask());
ex.submit(new newtask());
ex.submit(new newtask());
//The task count will be printed.
System.out.println("The Task count is :"+ ex.getTaskCount());
//The shutdown() method of thread pool executor.
System.out.println("Has the executor shutdown :"+ ex.isShutdown());
ex.shutdown();
ex.shutdown();
ex.shutdown();
}
// The Runnable Interface which implements the run() method.
static class newtask implements Runnable { // The override in Java
@Override
public void run() {
try { // The timespan for the threads to run
Long timesp = (long) (Math.random() * 7);
System.out.println("The thread that is currently running: " + Thread.currentThread().getName());
TimeUnit.SECONDS.sleep(timesp);
System.out.println("The thread that has completed running: " +Thread.currentThread().getName());
} catch (InterruptedException f) {
}
}
}
}
In the above code:
- A class for the shutdown() method is declared.
- The Cachedthreadpool either makes use of threads created previously or makes new ones if needed.
- Three new tasks are submitted for execution using the submit() method.
- The getTaskCount() prints the total number of tasks for execution.
- The shutdown() method finishes the tasks provided to it for execution.
- The Runnable interface is called when the threads have started.
- The println() method prints the currently running threads and the completed threads.
Output
In the below-mentioned output the threads currently running to execute the tasks have been shown:
This brings us to the end of the implementation of the shutdown() method of the thread pool executor in Java.
Conclusion
The shutdown() method is responsible for initiating the termination of the tasks that were previously submitted for execution. This method actually prevents the waiting tasks from starting and shuts the currently executing tasks. In this blog post, we have demonstrated in detail the implementation of the shutdown() method.