What Happens When a Java ThreadPoolExecutor Core Size Is Set to Zero?

When the core pool size of a Java ThreadPoolExecutor is configured to zero, new tasks are initially placed into the work queue, and the executor may create non‑core threads to process them, a behavior that often surprises interview candidates.

Senior Tony
Senior Tony
Senior Tony
What Happens When a Java ThreadPoolExecutor Core Size Is Set to Zero?

During Java interview preparation, a common question is what occurs when the core thread count of a ThreadPoolExecutor is set to 0. Many candidates mistakenly answer that tasks will be rejected or cause an exception, which is incorrect.

Default ThreadPoolExecutor workflow

The executor follows these steps:

If the number of submitted tasks is less than the core pool size, a core thread is created to handle the task.

When the task count reaches or exceeds the core size, tasks are placed into the work queue.

If the queue is full but the total thread count is still below the maximum pool size, a non‑core (extra) thread is created.

When the maximum pool size is reached and the queue is also full, the rejection policy is triggered.

Effect of setting core size to zero

With a core size of 0, the first step never creates a core thread, so every incoming task is enqueued. The subsequent handling depends on the type of queue:

Unbounded queue (e.g., LinkedBlockingQueue): tasks accumulate in the queue until memory is exhausted, potentially causing an OOM.

Bounded queue (e.g., ArrayBlockingQueue): once the queue is full, the executor creates non‑core threads up to the maximum pool size to process pending tasks.

Illustrative example

Consider a pool with core size 0, maximum size 5, and a bounded queue of capacity 2:

The first two submitted tasks are placed directly into the queue; no threads are created.

The third task finds the queue full, prompting the executor to spawn a non‑core thread to execute it.

Experiment

package com.example.demo;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
public class ThreadPoolCoreZeroTest {
    public static void main(String[] args) throws InterruptedException {
        ThreadPoolExecutor executor = new ThreadPoolExecutor(
                0,
                5,
                3,
                TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(2)
        );
        executor.submit(() -> {
            System.out.println("任务1执行中,线程:" + Thread.currentThread().getName());
        });
        executor.shutdown();
    }
}

Running this code shows that even a single submitted task is executed, contradicting the expectation that it would remain idle in the queue.

Why the task is processed

The ThreadPoolExecutor.execute() method contains logic that, after enqueuing a task, checks whether the number of active threads is zero. If it is, the executor invokes addWorker(null, false), which creates a non‑core worker thread that immediately pulls a task from the queue and runs it. This internal mechanism guarantees that queued tasks are not forgotten.

Therefore, setting the core pool size to zero does not prevent task execution; instead, tasks are handled by non‑core threads created on demand.

JavaExecutor
Senior Tony
Written by

Senior Tony

Former senior tech manager at Meituan, ex‑tech director at New Oriental, with experience at JD.com and Qunar; specializes in Java interview coaching and regularly shares hardcore technical content. Runs a video channel of the same name.

0 followers
Reader feedback

How this landed with the community

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.