Java : Executors with Examples
Executors (Java SE 19 & JDK 19) API Examples.
You will find code examples on most Executors methods.
Summary
try (final var executorService = Executors.newSingleThreadExecutor()) {
final Runnable task = () -> {
System.out.println(" task start");
System.out.println(" thread id = " + Thread.currentThread().threadId());
};
System.out.println("-- submit start --");
System.out.println("thread id = " + Thread.currentThread().threadId());
executorService.submit(task);
System.out.println("-- submit end --");
}
System.out.println("-- terminated --");
// Result
// ↓
//-- submit start --
//thread id = 1
//-- submit end --
// task start
// thread id = 33
//-- terminated --
Methods
static Callable<Object> callable (Runnable task)
final var callable = Executors.callable(() -> System.out.println("abcd"));
final var ret = callable.call(); // abcd
System.out.println(ret); // null
static <T> Callable<T> callable (Runnable task, T result)
final var callable = Executors.callable(() -> System.out.println("abcd"), "XYZ");
final var ret = callable.call(); // abcd
System.out.println(ret); // XYZ
static Callable<Object> callable (PrivilegedAction<?> action)
final var privilegedAction = new PrivilegedAction<>() {
@Override
public Object run() {
System.out.println("abcd");
return "XYZ";
}
};
final var callable = Executors.callable(privilegedAction);
final var ret = callable.call(); // abcd
System.out.println(ret); // XYZ
static Callable<Object> callable (PrivilegedExceptionAction<?> action)
final var privilegedExceptionAction = new PrivilegedExceptionAction<>() {
@Override
public Object run() {
System.out.println("abcd");
return "XYZ";
}
};
final var callable = Executors.callable(privilegedExceptionAction);
final var ret = callable.call(); // abcd
System.out.println(ret); // XYZ
static ThreadFactory defaultThreadFactory ()
final var threadFactory = Executors.defaultThreadFactory();
final var thread = threadFactory.newThread(() -> {
System.out.println("Run!");
});
System.out.println("thread id : " + thread.threadId());
System.out.println("thread name : " + thread.getName());
System.out.println("thread priority : " + thread.getPriority());
System.out.println("-- start --");
thread.start();
thread.join();
System.out.println("-- join --");
// Result
// ↓
//thread id : 33
//thread name : pool-1-thread-1
//thread priority : 5
//-- start --
//Run!
//-- join --
static ExecutorService newCachedThreadPool ()
class Task implements Runnable {
private final String name;
Task(String name) {
this.name = name;
}
@Override
public void run() {
try {
System.out.printf("%s : start (thread id = %d)%n",
name, Thread.currentThread().threadId());
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
System.out.println("Interrupted!");
} finally {
System.out.println(name + " : end");
}
}
}
try (var executorService = Executors.newCachedThreadPool()) {
executorService.submit(new Task("task1"));
executorService.submit(new Task("task2"));
executorService.submit(new Task("task3"));
TimeUnit.SECONDS.sleep(2);
executorService.submit(new Task("task4"));
executorService.submit(new Task("task5"));
}
// Result
// ↓
//task1 : start (thread id = 33)
//task2 : start (thread id = 34)
//task3 : start (thread id = 35)
//task1 : end
//task2 : end
//task3 : end
//task4 : start (thread id = 33)
//task5 : start (thread id = 35)
//task4 : end
//task5 : end
static ExecutorService newCachedThreadPool (ThreadFactory threadFactory)
class Task implements Runnable {
private final String name;
Task(String name) {
this.name = name;
}
@Override
public void run() {
try {
System.out.printf("%s : start (thread name = %s)%n",
name, Thread.currentThread().getName());
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
System.out.println("Interrupted!");
} finally {
System.out.println(name + " : end");
}
}
}
try (final var executorService = Executors.newCachedThreadPool(r -> {
final var thread = new Thread(r);
thread.setName("TID:" + thread.threadId());
return thread;
})) {
executorService.submit(new Task("task1"));
executorService.submit(new Task("task2"));
executorService.submit(new Task("task3"));
TimeUnit.SECONDS.sleep(2);
executorService.submit(new Task("task4"));
executorService.submit(new Task("task5"));
}
// Result
// ↓
//task1 : start (thread name = TID:33)
//task2 : start (thread name = TID:34)
//task3 : start (thread name = TID:35)
//task1 : end
//task2 : end
//task3 : end
//task4 : start (thread name = TID:34)
//task5 : start (thread name = TID:35)
//task4 : end
//task5 : end
static ExecutorService newFixedThreadPool (int nThreads)
class Task implements Runnable {
private final String name;
Task(String name) {
this.name = name;
}
@Override
public void run() {
try {
System.out.printf("%s : start (thread id = %d)%n",
name, Thread.currentThread().threadId());
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
System.out.println("Interrupted!");
} finally {
System.out.println(name + " : end");
}
}
}
try (final var executorService = Executors.newFixedThreadPool(3)) {
executorService.submit(new Task("task1"));
executorService.submit(new Task("task2"));
executorService.submit(new Task("task3"));
executorService.submit(new Task("task4"));
executorService.submit(new Task("task5"));
}
// Result
// ↓
//task1 : start (thread id = 33)
//task2 : start (thread id = 34)
//task3 : start (thread id = 35)
//task1 : end
//task2 : end
//task3 : end
//task4 : start (thread id = 33)
//task5 : start (thread id = 35)
//task4 : end
//task5 : end
static ExecutorService newFixedThreadPool (int nThreads, ThreadFactory threadFactory)
class Task implements Runnable {
private final String name;
Task(String name) {
this.name = name;
}
@Override
public void run() {
try {
System.out.printf("%s : start (thread name = %s)%n",
name, Thread.currentThread().getName());
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
System.out.println("Interrupted!");
} finally {
System.out.println(name + " : end");
}
}
}
try (final var executorService = Executors.newFixedThreadPool(3, r -> {
final var thread = new Thread(r);
thread.setName("TID:" + thread.threadId());
return thread;
})) {
executorService.submit(new Task("task1"));
executorService.submit(new Task("task2"));
executorService.submit(new Task("task3"));
executorService.submit(new Task("task4"));
executorService.submit(new Task("task5"));
}
// Result
// ↓
//task1 : start (thread name = TID:33)
//task2 : start (thread name = TID:34)
//task3 : start (thread name = TID:35)
//task1 : end
//task2 : end
//task3 : end
//task4 : start (thread name = TID:34)
//task5 : start (thread name = TID:35)
//task4 : end
//task5 : end
static ScheduledExecutorService newScheduledThreadPool (int corePoolSize)
class Task implements Runnable {
private final String name;
private final long currentTime;
Task(String name, long currentTime) {
this.name = name;
this.currentTime = currentTime;
}
@Override
public void run() {
try {
System.out.printf("%s : start (thread id = %d) : %f sec.%n",
name, Thread.currentThread().threadId(), getElapsedTime());
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
System.out.println("Interrupted!");
} finally {
System.out.println(name + " : end : " + getElapsedTime() + " sec.");
}
}
private double getElapsedTime() {
return (System.nanoTime() - currentTime) / 1000000000.0;
}
}
try (final var executorService = Executors.newScheduledThreadPool(3)) {
final var currentTime = System.nanoTime();
executorService.schedule(new Task("task1", currentTime), 3, TimeUnit.SECONDS);
executorService.schedule(new Task("task2", currentTime), 5, TimeUnit.SECONDS);
executorService.schedule(new Task("task3", currentTime), 7, TimeUnit.SECONDS);
executorService.submit(new Task("task4", currentTime));
executorService.submit(new Task("task5", currentTime));
}
// Result
// ↓
//task4 : start (thread id = 35) : 0.001707 sec.
//task5 : start (thread id = 34) : 0.001720 sec.
//task4 : end : 1.0058648 sec.
//task5 : end : 1.0060478 sec.
//task1 : start (thread id = 33) : 3.001571 sec.
//task1 : end : 4.0130198 sec.
//task2 : start (thread id = 34) : 5.010234 sec.
//task2 : end : 6.0181524 sec.
//task3 : start (thread id = 35) : 7.001654 sec.
//task3 : end : 8.0131763 sec.
static ScheduledExecutorService newScheduledThreadPool (int corePoolSize, ThreadFactory threadFactory)
class Task implements Runnable {
private final String name;
private final long currentTime;
Task(String name, long currentTime) {
this.name = name;
this.currentTime = currentTime;
}
@Override
public void run() {
try {
System.out.printf("%s : start (thread name = %s) : %f sec.%n",
name, Thread.currentThread().getName(), getElapsedTime());
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
System.out.println("Interrupted!");
} finally {
System.out.println(name + " : end : " + getElapsedTime() + " sec.");
}
}
private double getElapsedTime() {
return (System.nanoTime() - currentTime) / 1000000000.0;
}
}
try (final var executorService = Executors.newScheduledThreadPool(3, r -> {
final var thread = new Thread(r);
thread.setName("TID:" + thread.threadId());
return thread;
})) {
final var currentTime = System.nanoTime();
executorService.schedule(new Task("task1", currentTime), 3, TimeUnit.SECONDS);
executorService.schedule(new Task("task2", currentTime), 5, TimeUnit.SECONDS);
executorService.schedule(new Task("task3", currentTime), 7, TimeUnit.SECONDS);
executorService.submit(new Task("task4", currentTime));
executorService.submit(new Task("task5", currentTime));
}
// Result
// ↓
//task4 : start (thread name = TID:34) : 0.005763 sec.
//task5 : start (thread name = TID:35) : 0.005858 sec.
//task4 : end : 1.0167864 sec.
//task5 : end : 1.0167499 sec.
//task1 : start (thread name = TID:33) : 3.009088 sec.
//task1 : end : 4.0211626 sec.
//task2 : start (thread name = TID:35) : 5.016738 sec.
//task2 : end : 6.0314814 sec.
//task3 : start (thread name = TID:34) : 7.012653 sec.
//task3 : end : 8.0254665 sec.
static ExecutorService newSingleThreadExecutor ()
class Task implements Runnable {
private final String name;
Task(String name) {
this.name = name;
}
@Override
public void run() {
try {
System.out.printf("%s : start (thread id = %d)%n",
name, Thread.currentThread().threadId());
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
System.out.println("Interrupted!");
} finally {
System.out.println(name + " : end");
}
}
}
try (final var executorService = Executors.newSingleThreadExecutor()) {
executorService.submit(new Task("task1"));
executorService.submit(new Task("task2"));
executorService.submit(new Task("task3"));
}
// Result
// ↓
//task1 : start (thread id = 33)
//task1 : end
//task2 : start (thread id = 33)
//task2 : end
//task3 : start (thread id = 33)
//task3 : end
static ExecutorService newSingleThreadExecutor (ThreadFactory threadFactory)
class Task implements Runnable {
private final String name;
Task(String name) {
this.name = name;
}
@Override
public void run() {
try {
System.out.printf("%s : start (thread name = %s)%n",
name, Thread.currentThread().getName());
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
System.out.println("Interrupted!");
} finally {
System.out.println(name + " : end");
}
}
}
try (final var executorService = Executors.newSingleThreadExecutor(r -> {
final var thread = new Thread(r);
thread.setName("TID:" + thread.threadId());
return thread;
})) {
executorService.submit(new Task("task1"));
executorService.submit(new Task("task2"));
executorService.submit(new Task("task3"));
}
// Result
// ↓
//task1 : start (thread name = TID:33)
//task1 : end
//task2 : start (thread name = TID:33)
//task2 : end
//task3 : start (thread name = TID:33)
//task3 : end
static ScheduledExecutorService newSingleThreadScheduledExecutor ()
class Task implements Runnable {
private final String name;
private final long currentTime;
Task(String name, long currentTime) {
this.name = name;
this.currentTime = currentTime;
}
@Override
public void run() {
try {
System.out.printf("%s : start (thread id = %d) : %f sec.%n",
name, Thread.currentThread().threadId(), getElapsedTime());
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
System.out.println("Interrupted!");
} finally {
System.out.println(name + " : end : " + getElapsedTime() + " sec.");
}
}
private double getElapsedTime() {
return (System.nanoTime() - currentTime) / 1000000000.0;
}
}
try (final var executorService = Executors.newSingleThreadScheduledExecutor()) {
final var currentTime = System.nanoTime();
executorService.schedule(new Task("task1", currentTime), 3, TimeUnit.SECONDS);
executorService.schedule(new Task("task2", currentTime), 5, TimeUnit.SECONDS);
executorService.submit(new Task("task3", currentTime));
}
// Result
// ↓
//task3 : start (thread id = 33) : 0.001784 sec.
//task3 : end : 1.0113467 sec.
//task1 : start (thread id = 33) : 3.009510 sec.
//task1 : end : 4.0228934 sec.
//task2 : start (thread id = 33) : 5.010346 sec.
//task2 : end : 6.0217522 sec.
static ScheduledExecutorService newSingleThreadScheduledExecutor (ThreadFactory threadFactory)
class Task implements Runnable {
private final String name;
private final long currentTime;
Task(String name, long currentTime) {
this.name = name;
this.currentTime = currentTime;
}
@Override
public void run() {
try {
System.out.printf("%s : start (thread name = %s) : %f sec.%n",
name, Thread.currentThread().getName(), getElapsedTime());
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
System.out.println("Interrupted!");
} finally {
System.out.println(name + " : end : " + getElapsedTime() + " sec.");
}
}
private double getElapsedTime() {
return (System.nanoTime() - currentTime) / 1000000000.0;
}
}
try (final var executorService = Executors.newSingleThreadScheduledExecutor(r -> {
final var thread = new Thread(r);
thread.setName("TID:" + thread.threadId());
return thread;
})) {
final var currentTime = System.nanoTime();
executorService.schedule(new Task("task1", currentTime), 3, TimeUnit.SECONDS);
executorService.schedule(new Task("task2", currentTime), 5, TimeUnit.SECONDS);
executorService.submit(new Task("task3", currentTime));
}
// Result
// ↓
//task3 : start (thread name = TID:33) : 0.005336 sec.
//task3 : end : 1.0096395 sec.
//task1 : start (thread name = TID:33) : 3.001487 sec.
//task1 : end : 4.0157305 sec.
//task2 : start (thread name = TID:33) : 5.012736 sec.
//task2 : end : 6.0237463 sec.
static ExecutorService newThreadPerTaskExecutor (ThreadFactory threadFactory)
Preview.
static ExecutorService newVirtualThreadPerTaskExecutor ()
Preview.
static ExecutorService newWorkStealingPool ()
class Task implements Runnable {
private final String name;
Task(String name) {
this.name = name;
}
@Override
public void run() {
try {
System.out.printf("%s : start (thread id = %d)%n",
name, Thread.currentThread().threadId());
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
System.out.println("Interrupted!");
} finally {
System.out.println(name + " : end");
}
}
}
try (final var executorService = Executors.newWorkStealingPool()) {
executorService.submit(new Task("task1"));
executorService.submit(new Task("task2"));
executorService.submit(new Task("task3"));
executorService.submit(new Task("task4"));
executorService.submit(new Task("task5"));
}
// Result
// ↓
//task1 : start (thread id = 33)
//task2 : start (thread id = 34)
//task3 : start (thread id = 35)
//task4 : start (thread id = 36)
//task5 : start (thread id = 37)
//task1 : end
//task2 : end
//task3 : end
//task4 : end
//task5 : end
static ExecutorService newWorkStealingPool (int parallelism)
class Task implements Runnable {
private final String name;
Task(String name) {
this.name = name;
}
@Override
public void run() {
try {
System.out.printf("%s : start (thread id = %d)%n",
name, Thread.currentThread().threadId());
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
System.out.println("Interrupted!");
} finally {
System.out.println(name + " : end");
}
}
}
try (final var executorService = Executors.newWorkStealingPool(3)) {
executorService.submit(new Task("task1"));
executorService.submit(new Task("task2"));
executorService.submit(new Task("task3"));
executorService.submit(new Task("task4"));
executorService.submit(new Task("task5"));
}
// Result
// ↓
//task1 : start (thread id = 33)
//task2 : start (thread id = 34)
//task3 : start (thread id = 35)
//task1 : end
//task2 : end
//task3 : end
//task4 : start (thread id = 33)
//task5 : start (thread id = 34)
//task4 : end
//task5 : end
static <T> Callable<T> privilegedCallable (Callable<T> callable)
Deprecated.
static <T> Callable<T> privilegedCallableUsingCurrentClassLoader (Callable<T> callable)
Deprecated.
static ThreadFactory privilegedThreadFactory ()
Deprecated.
static ExecutorService unconfigurableExecutorService (ExecutorService executor)
// An example without unconfigurableExecutorService.
try (final var executorService = Executors.newFixedThreadPool(3)) {
// Can be downcast.
final var threadPoolExecutor = (ThreadPoolExecutor) executorService;
threadPoolExecutor.setCorePoolSize(1);
System.out.println(threadPoolExecutor.getCorePoolSize()); // 1
}
// An example with unconfigurableExecutorService.
try (final var executorService = Executors.unconfigurableExecutorService(
Executors.newFixedThreadPool(3))) {
// Can be not downcast.
final var threadPoolExecutor = (ThreadPoolExecutor) executorService;
} catch (ClassCastException e) {
System.out.println("ClassCastException!");
}
// Result
// ↓
//ClassCastException!
static ScheduledExecutorService unconfigurableScheduledExecutorService (ScheduledExecutorService executor)
// An example without unconfigurableScheduledExecutorService.
try (final var scheduledExecutorService = Executors.newScheduledThreadPool(3)) {
// Can be downcast.
final var threadPoolExecutor = (ScheduledThreadPoolExecutor) scheduledExecutorService;
threadPoolExecutor.setCorePoolSize(1);
System.out.println(threadPoolExecutor.getCorePoolSize()); // 1
}
// An example with unconfigurableScheduledExecutorService.
try (final var scheduledExecutorService = Executors.unconfigurableScheduledExecutorService(
Executors.newScheduledThreadPool(3))) {
// Can be not downcast.
final var threadPoolExecutor = (ScheduledThreadPoolExecutor) scheduledExecutorService;
} catch (ClassCastException e) {
System.out.println("ClassCastException!");
}
// Result
// ↓
//ClassCastException!
Related posts
- API Examples
- BlockingQueue
- Callable
- CancellationException
- ConcurrentHashMap.KeySetView
- ConcurrentLinkedDeque
- ConcurrentLinkedQueue
- ConcurrentMap
- ConcurrentModificationException
- ConcurrentSkipListSet
- Condition
- CopyOnWriteArrayList
- CopyOnWriteArraySet
- CountDownLatch
- CyclicBarrier
- Exchanger
- Executor
- ExecutorService
- Future
- Future.State
- FutureTask
- InterruptedException
- Lock
- Runnable
- Semaphore
- Thread
- ThreadGroup
- ThreadLocal
- TimeUnit