Executor, ExecutorService And Executors
Executor: The Executor interface has execute method which has void return type. and accepts Runnable interface
Get Anshu Kulshrestha’s stories in your inbox
Join Medium for free to get updates from this writer.
ExecutorService : This is an interface extending the Executor interface and is responsible for Thread Pool creation and management. The ExecutorService has submit method which returns future object and accepts callable/Runnable.
Executors: It is a class which provides factory methods to create different kind of thread pools
public class ExecutorServiceEx {
public static void main(String[] args) {
ExecutorService executorService = Executors.newFixedThreadPool(2);
executorService.execute(new Runnable() {
public void run() {
for (int i = 0; i < 100; i++) {
System.out.println("Asynchronous task 1"+" : "+ Thread.currentThread());
}
}
});
executorService.execute(new Runnable() {
public void run() {
for (int i = 0; i < 100; i++) {
System.out.println("Asynchronous task 2" + " : "+ Thread.currentThread());
}
}
});
executorService.execute(new Runnable() {
public void run() {
for (int i = 0; i < 100; i++) {
System.out.println("Asynchronous task 3" + " : "+ Thread.currentThread());
}
}
});
Future<?> submit = executorService.submit(new Runnable() {
public void run() {
for (int i = 0; i < 100; i++) {
System.out.println("Asynchronous task 4" + " : "+ Thread.currentThread());
}
}
});
System.out.println(submit.isDone());
executorService.shutdown();
}
}






