Backend Development 4 min read

Understanding Java Executors Thread Pools: newCachedThreadPool, newScheduledThreadPool, newSingleThreadExecutor, newWorkStealingPool, and newFixedThreadPool

This article explains the five main thread‑pool types provided by Java's Executors class, detailing their constructors, underlying queue implementations, and default thread‑count behaviors, and notes why the default pools may not suit everyday development needs.

Java Captain
Java Captain
Java Captain
Understanding Java Executors Thread Pools: newCachedThreadPool, newScheduledThreadPool, newSingleThreadExecutor, newWorkStealingPool, and newFixedThreadPool

First, there are five types of thread pools created via Executors .

Executors.newCachedThreadPool()

From the constructor we can see that the maximum pool size is Integer.MAX_VALUE and the work queue used is a SynchronousQueue , which has no capacity and transfers tasks directly to waiting consumers.

/**
        * 最大线程数是 Integer的最大值
        * 等待队列使用的是SynchronousQueue
        *  SynchronousQueue 没有容量,等待消费者 transfer
        */
public static ExecutorService newCachedThreadPool() {
    return new ThreadPoolExecutor(0, Integer.MAX_VALUE, 60L, TimeUnit.SECONDS,
        new SynchronousQueue
());
}

No‑argument constructor

The same constructor can also accept a custom ThreadFactory as a parameter.

/**
     * 通过参数传入线程工厂
     */
public static ExecutorService newCachedThreadPool(ThreadFactory threadFactory) {
    return new ThreadPoolExecutor(0, Integer.MAX_VALUE, 60L, TimeUnit.SECONDS,
        new SynchronousQueue
(), threadFactory);
}

Executors.newScheduledThreadPool(5)

This pool allows the core pool size to be set via the argument; the maximum size remains Integer.MAX_VALUE . It uses a DelayedWorkQueue that can grow dynamically.

/**
        * 核心线程数可以通过入参控制
        * 最大线程数是 Integer的最大值
        * 等待队列使用的是DelayedWork
        *  DelayedWork 数据结果使用的是可以动态扩容的数组
        */
public ScheduledThreadPoolExecutor(int corePoolSize) {
    super(corePoolSize, Integer.MAX_VALUE, 0, NANOSECONDS,
        new DelayedWorkQueue());
}

Executors.newSingleThreadExecutor()

This creates a single‑threaded pool where both core and maximum pool sizes are 1, and it uses a LinkedBlockingQueue with an effectively unbounded capacity.

/**
        * 核心线程和最大线程数都是1,只有单个线程的线程池
        * 等待队列使用的是LinkedBlockingQueue
        *  LinkedBlockingQueue 数据结构使用的是单向链表,同时等待队列的大小是 Integer的上限
        */
public static ExecutorService newSingleThreadExecutor() {
    return new FinalizableDelegatedExecutorService(
        new ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS,
            new LinkedBlockingQueue
()));
}

Executors.newWorkStealingPool()

No‑argument constructor

The pool’s parallelism level defaults to the number of available processors.

/**
        * 核心线程数使用的是jvm中可用的处理器数量
        */
public static ExecutorService newWorkStealingPool() {
    return new ForkJoinPool(Runtime.getRuntime().availableProcessors(),
        ForkJoinPool.defaultForkJoinWorkerThreadFactory, null, true);
}

Constructor with parallelism parameter

You can specify the parallelism level explicitly.

/**
        * 核心线程数是参数大小
        */
public static ExecutorService newWorkStealingPool(int parallelism) {
    return new ForkJoinPool(parallelism,
        ForkJoinPool.defaultForkJoinWorkerThreadFactory, null, true);
}

Executors.newFixedThreadPool(10)

This pool sets both core and maximum pool sizes to the same value provided as an argument and uses a LinkedBlockingQueue with an effectively unbounded capacity.

/**
        * 核心线程数和最大线程数是一样的,通过入参配置
        * LinkedBlockingQueue 数据结构使用的是单向链表,同时等待队列的大小是 Integer的上限
        */
public static ExecutorService newFixedThreadPool(int nThreads) {
    return new ThreadPoolExecutor(nThreads, nThreads, 0L, TimeUnit.MILLISECONDS,
        new LinkedBlockingQueue
());
}

Thread pools created via Executors often do not satisfy the specific requirements of everyday development scenarios.

JavaconcurrencythreadpoolExecutorServiceExecutors
Java Captain
Written by

Java Captain

Focused on Java technologies: SSM, the Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading; occasionally covers DevOps tools like Jenkins, Nexus, Docker, ELK; shares practical tech insights and is dedicated to full‑stack Java development.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.