The thread pool is a term that basically refers to the pool that has been created for the threads. The thread pool is useful since it reuses the threads and does not create new threads unless needed. These thread pools are also responsible for the timely execution of tasks. The tasks are kept in the queue and executed when a thread becomes available. Therefore, they are effective in the execution of tasks with time.
In this article, we will discuss the isTerminated() method of the Thread Pool Executor class in Java.
Thread Pool Executor isTerminated() Method in Java
The isTerminated() method of the ThreadPool Executor Class in Java is responsible for returning the value of all the tasks as true if the shutdown() method has been executed. If the shutdown() method has not been executed the isTerminated() method will not return the value as true.
Syntax of isTerminated() Method
Let us have a look at the syntax of the isTerminated method() of the ThreadPool Executor-class in Java.
Public boolean isTerminated()
Example 1: Implementation of isTerminated() Method
The code implementation for the isTerminated() method is depicted below,
//Import concurrent that helps in Multithreading of Java
import java.util.concurrent.*;
import java.lang.Thread;
import java.util.*;
//Create a class
class Terminationprogram {
public static void main(String[] args) {
//cancel tasks occurring initially
cancel firsttask = new cancel("t1");
System.out.println("The threads are starting..");
//newCached reuses previously created threads but creates new ones as needed.
ThreadPoolExecutor texec = (ThreadPoolExecutor) Executors.newCachedThreadPool();
//To get the maximum pool size
System.out.println("The Maximum pool size is "+ texec.getMaximumPoolSize());
//the count of tasks using the get() method
System.out.println("Before Execution the task count is:"+ texec.getTaskCount());
texec.submit(firsttask);
//Start task 1
texec.execute(firsttask);
//get the results after execution of task 1.
System.out.println("After Execution the task count is :"+ texec.getTaskCount());
//shut down the thread
texec.shutdown();
System.out.println("Has the executor ended ? "+ texec.isTerminated());
System.out.println("The threads have started.\nThe main has ended.");
}
}
//the runnable instance of Java for Threads.
class cancel implements Runnable {
private final String threadName;
public cancel(String threadName) {
this.threadName = threadName;
}
//The only method used by Runnable.
@Override
public void run() {
for(int i=5 ; i<=9;i++)
{
if(i==6)
{
System.out.println("i=" +i);
}
}
}
}
In the above code,
- Declare a class and cancel all the tasks running currently since new threads will start to run.
- The newCachedThreadPool() will create a new thread when needed otherwise it reuses the previously created threads.
- The getTaskcount() method gets the count of tasks before and after execution.
- The shutdown() method is executed before the isTerminated() method since termination occurs after the shutdown() method has been executed.
- The run() method has been used by the runnable interface.
- A block of code has been declared in the run() method.
Output

In the above output,
- The start of the thread has been indicated.
- The tasks count before and after execution has been declared.
- The code block under the run() method has given the output as an incremented value that is 6.
- If the executor has not ended and the shutdown() method has still not completed, the isTerminated() method will give the boolean value as false.
- Now, the threads in the queue have started.
This sums up the implementation of the isTerminated() method of the ThreadPool executor class in Java.
Conclusion
The ThreadPoolExecutor in Java is an executor service that has the responsibility to run the tasks from the pool of threads or create new threads to run the tasks. These tasks are also terminated when completed. Therefore, there arises a need to implement the isTerminated method() of the ThreadPollExecutor class. This termination method works only if the shutdown() method has been declared before. This article has described the working and code implementation of the isTerminated() method in detail.