The thread pool in Java works in such a way that it keeps the threads and uses them from the pool when required. Whenever there is a need for a thread by any task, the thread pool provides it to the relevant task and returns it to the pool after usage. It reuses the threads present in the pool without the creation of any new threads.
In this article, the execute() method of ThreadPoolExececutor in Java will be discussed in detail.
ThreadPoolExecutor execute() Method in Java
The execute() method of Java is responsible for executing the given task. The task is executed from the thread pool that is already present, or a new thread is created.
Below is the syntax of the execute() method:
Public void execute(task)
Example 1: Implementation of execute() Method
The implementation of the execute() method is depicted in the below code:
//Import the required packages
import java.util.concurrent.*;
import java.lang.Thread;
import java.util.logging.*;
//Create a class for the execute method
class Threadexceute {
public static void main(String[] args) {
// Create two threads
Maintask firsttask = new Maintask("Thread 1");
Maintask secondtask = new Maintask("Thread 2");
// The threads will start working
System.out.println("The Threads will Start");
ThreadPoolExecutor threadexe = (ThreadPoolExecutor) Executors.newCachedThreadPool();
// The execute method will start the first task to start
threadexe.execute(firsttask);
// The execute will start the second task to start
threadexe.execute(secondtask);
// Close the worker thread
threadexe.shutdown();
System.out.println("Threads started, main ends\n");
}
}
// This method makes the thread run by its run() method
class Maintask implements Runnable {
private final int sleepTime;
private final String tName;
public Maintask(String name) {
// The name of the thread and the time for which the thread will sleep.
tName = name;
sleepTime = 2000;
}
// The run() method of Runnable interface.
public void run() {
// The output statement that the thread will sleep for this specified time.
System.out.printf("%s will go to sleep for %d milliseconds.\n", tName, sleepTime);
try {
// Make the thread to sleep
Thread.sleep(sleepTime);
// the exception for tasks that have not been submitted.
} catch (InterruptedException e) {
Logger.getLogger(Maintask.class.getName()).log(Level.SEVERE, null, e);
}
System.out.printf("%s has completed sleeping\n", tName);
}
}
In this Java code:
- Two tasks have been submitted to the threads respectively.
- The execute() method will run the tasks from the pool of threads.
- The Runnable interface implements the run() method.
- The Runnable interface makes the thread sleep for a specified time.
- The try block contains the sleeping time for the threads.
- The catch blocks handle the exception of tasks that are not submitted.
Output

In the above output:
- The threads have started to work.
- The threads have been made to sleep for the specified duration.
- The sleeping threads then wake and execute() method executes the tasks.
Conclusion
The thread pool executor has a number of methods present for different tasks. The execute() method of the ThreadpoolExecutor interface helps in the execution of tasks. The tasks are either run from the available threads in the pool, or new threads are created for the execution of tasks. The execute() method has been executed in detail in this piece of writing.