Executor Survives Worker Task Death

An uncaught execute() task kills only the worker and the pool spawns a replacement, while submit() traps the exception inside its Future.

JavaResilience

Code

snippet
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class ExecutorSurvives {

    public static void main(String[] args) throws InterruptedException, ExecutionException {
        // 自定义线程工厂:线程名递增 + 未捕获异常打到 stdout(避免 stderr 堆栈噪音)
        ExecutorService executor = Executors.newFixedThreadPool(1, Thread.ofPlatform()
                .name("worker-", 0)
                .uncaughtExceptionHandler((t, e) -> System.out.println("handler: " + e.getMessage()))
                .factory());

        // execute():未捕获异常杀死工作线程,池创建 replacement 继续服务
        executor.execute(() -> { throw new IllegalStateException("boom"); });
        executor.execute(() ->
                System.out.println("serving on " + Thread.currentThread().getName()));

        // submit():异常被 FutureTask 捕获存进 Future,线程不死 (survives)
        executor.submit(() -> { throw new IllegalStateException("boom2"); });
        executor.execute(() ->
                System.out.println("serving on " + Thread.currentThread().getName()));

        try {
            executor.submit(() -> { throw new IllegalStateException("boom3"); }).get();
        } catch (ExecutionException e) {
            System.out.println("future saw: " + e.getCause().getMessage());
        }
        executor.shutdown();
    }
}

Executor Survives Worker Task Death: inside the code

Everything happens inside the class ExecutorSurvives.

IllegalStateException is thrown at three sites, and each lands differently. The first, submitted through execute(), is not inside any try: it escapes the task, reaches the uncaughtExceptionHandler (handler: boom) and kills the worker thread, which the pool replaces so the following tasks still run. The second, submitted through submit(), is captured into its Future instead, so that worker survives. Only the third throw travels through get() inside the try, where it surfaces as an ExecutionException whose cause is the original exception.

Points of care

  • The stock toolchain is enough — add nothing.

Putting it to use

The run prints handler:, serving on, future saw: at the head of each relevant line.

Core lines

ExecutorService executor = Executors.newFixedThreadPool(1, Thread.ofPlatform()
        .name("worker-", 0)
        .uncaughtExceptionHandler((t, e) -> System.out.println("handler: " + e.getMessage()))
        .factory());
executor.submit(() -> { throw new IllegalStateException("boom3"); }).get();
executor.execute(() -> { throw new IllegalStateException("boom"); });

Interactive Tools for This Topic

FAQ

Does the snippet validate its input?
No. None of the three `IllegalStateException` throw sites checks any input; each exists to show where a task exception lands. The `execute()` throw is not inside any `try` — it escapes the task, reaches the `uncaughtExceptionHandler` (`handler: boom`) and kills the worker thread, which the pool replaces. The `submit()` throw is captured into its `Future`, so that worker survives. Only the third throw is observed through `get()` inside the `try`, and it surfaces as an `ExecutionException` wrapping the original exception.
What does Executor Survives Worker Task Death print when run?
Execute the whole file; each printed line is prefixed with a label like `handler:`, `serving on`, `future saw:`.