Skip to content
Tutorials Center
My learning

Topics

Java Reference ยท Concurrency

Executors factory methods

Reference for the java.util.concurrent.Executors factory methods: fixed, cached, single-thread, scheduled, work-stealing and virtual-thread executors.

The Executors class is a set of static factory methods that build the most common ExecutorService configurations. Each method picks a pool size, a work queue and a thread factory for you, so you do not have to configure a ThreadPoolExecutor by hand.

Choosing the right one depends on your tasks. Fixed pools bound the thread count, cached pools grow on demand, single-thread executors guarantee ordering, and the virtual-thread executor starts a new virtual thread for every task.

Java
ExecutorService fixed  = Executors.newFixedThreadPool(4);
ExecutorService single = Executors.newSingleThreadExecutor();
ExecutorService perTask = Executors.newVirtualThreadPerTaskExecutor();

Try it

Java Fixed and single-thread pools with named threads

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Java Cached, custom and per-task executors

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Syntax

Java
public static ExecutorService newFixedThreadPool(int nThreads)
public static ExecutorService newFixedThreadPool(int nThreads, ThreadFactory factory)
public static ExecutorService newCachedThreadPool()
public static ExecutorService newSingleThreadExecutor()
public static ExecutorService newWorkStealingPool()
public static ExecutorService newWorkStealingPool(int parallelism)
public static ExecutorService newVirtualThreadPerTaskExecutor()
public static ExecutorService newThreadPerTaskExecutor(ThreadFactory factory)
public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize)
public static ScheduledExecutorService newSingleThreadScheduledExecutor()
Factory Behavior
newFixedThreadPool(n) At most n threads, kept alive; extra tasks wait in an unbounded queue.
newCachedThreadPool() No limit on threads; idle threads die after 60 seconds. Tasks are handed directly to a free or new thread.
newSingleThreadExecutor() One thread, tasks run strictly in order. If that thread dies, a new one replaces it. The returned object cannot be cast to ThreadPoolExecutor.
newWorkStealingPool(...) A ForkJoinPool in async mode; the default parallelism is the CPU count. Threads are daemon threads.
newVirtualThreadPerTaskExecutor() Starts a new virtual thread for each task. Nothing is pooled.
newThreadPerTaskExecutor(factory) One thread per task, created by your factory (platform or virtual).
newScheduledThreadPool(n) A ScheduledExecutorService with n core threads.

Throws: IllegalArgumentException when a pool size is zero or negative.

Usage notes

  • Fixed pool queues are unbounded. Producers that outrun the workers eat memory. Use new ThreadPoolExecutor(...) with an ArrayBlockingQueue and a CallerRunsPolicy for back pressure.
  • Cached pools can explode. Thousands of blocked tasks mean thousands of platform threads. Use it only for many short, non-blocking tasks, or switch to virtual threads for blocking work.
  • Name your threads with a ThreadFactory. Default names such as pool-1-thread-3 are hard to trace in logs.
  • Daemon status. Threads from the classic factories are non-daemon; forgetting shutdown() keeps the JVM alive. Work-stealing and virtual-thread executors use daemon threads.
  • Task order. Only the single-thread executor guarantees that tasks start in submission order.
  • newWorkStealingPool suits many small, independent or recursive CPU tasks, not blocking calls.
  • Pool size for CPU work: Runtime.getRuntime().availableProcessors(). For blocking work, measure; or use virtual threads.