Last Updated : 24 Sep, 2021
The concurrent API in Java provides a feature known as an executor that initiates and controls the execution of threads. As such, an executor offers an alternative to managing threads using the thread class. At the core of an executor is the Executor interface. It refers to the objects that execute submitted Runnable tasks.
Class hierarchy:
java.util.concurrent ↳ Interface Executor
Implementing Sub-Interfaces:
ExecutorService ScheduledExecutorService
Implementing Classes:
AbstractExecutorService ForkJoinPool ScheduledThreadPoolExecutor ThreadPoolExecutor
Methods in Executor interface:
void execute(Runnable task)
Example to demonstrate an Executor.
Java
import java.util.concurrent.Executor;
import java.util.concurrent.RejectedExecutionException;
public class ExecutorDemo {
public static void main(String[] args)
{
ExecutorImp obj = new ExecutorImp();
try {
obj.execute(new NewThread());
}
catch (RejectedExecutionException
| NullPointerException exception) {
System.out.println(exception);
}
}
}
class ExecutorImp implements Executor {
@Override
public void execute(Runnable command)
{
new Thread(command).start();
}
}
class NewThread implements Runnable {
@Override
public void run()
{
System.out.println("Thread executed under an executor");
}
}
Thread executed under an executor
Reference:https://docs.oracle.com/javase/9/docs/api/java/util/concurrent/Executor.html
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4