The collection of threads that helps in running the tasks together is called a Thread Pool. The ThreadPoolExecutor in Java is responsible for executing the worker threads. It makes use of the Runnable() Interface that contains the run() method which runs the working threads from the queue. It is useful for controlling the threads. There are various methods present to perform various functionalities in the ThreadPoolExecutor class.
In this article, we will implement the remove() method of the thread pool executor class in Java.
How to Implement the remove() Method of ThreadPoolExeceutor in Java?
The remove() method of the ThreadPoolExecutor class deletes a task from the executor’s waiting list, which stops the task from running if it hasn’t started yet.
To implement the remove() method of the Thread pool executor class in Java we need to understand its syntax, how it works, and whether it throws any exceptions or not.
remove() Method Syntax
The syntax of the remove() method is depicted below:
Public boolean remove(Runnable job);
Here the job is a parameter that will be removed.
This method does not throw any exception since it will just remove the task.
Example: remove() Method Implementation
The following coding example depicts the use of remove() method in Java:
import java.util.concurrent.*;
import java.lang.Thread;
//The class to implement the Remove() method.
class removeexample {
public static void main(final String[] arguments) throws InterruptedException {
// The statement from the threads in the pool.
ThreadPoolExecutor remexe = (ThreadPoolExecutor) Executors.newCachedThreadPool();
System.out.println("THE REMOVE METHOD OF THREAD POOL EXECUTOR");
remexe.submit(new remTask());
remexe.submit(new remTask());// The remove() method will remove the task.
remexe.remove(new remTask());// This will shutdown the worker thread.
remexe.shutdown();
}
// The Runnable Interface that used one method run()
static class remTask implements Runnable {
@Override
public void run() {
try {
Long timespan = (long) (Math.random() * 5);
// The thread that is currently running
System.out.println("The thread that is currently running " + Thread.currentThread().getName());
TimeUnit.SECONDS.sleep(timespan);
// The thread that has completed running
System.out.println("The threads that are completed " + Thread.currentThread().getName());
} catch (InterruptedException f) {
}
}
}
}
In the code above:
- An executor with the tasks has been assigned inside the removeexample class.
- Two tasks have been submitted.
- The remove() method will remove any tasks that have not yet started to run.
- The shutdown() is important to execute in order to shut the workers’ thread.
- The thread that is currently running and the threads that have completed running have been printed using the println() function.
Output

In the output above:
- The remove() method has stopped the tasks that would have been currently running.
- The currently running threads and the threads that have completed running have been printed.
This sums up the implementation of the remove() method in ThreadPoolExecutor in Java.
Conclusion
The remove() method of the ThreadPoolExecutor class deletes a task from the executor’s waiting list, which stops the task from running if it hasn’t started yet. The thread pool executor class consists of various methods that are applied according to the need. The thread pools are beneficial in reusing the threads instead of creating the threads again. In this article, we have discussed the thread pool executor class remove() method in detail.