为什么主线程没有终止

dai*_*dai 3 java program-entry-point executorservice threadpool

据我所知,未捕获的线程将以当前线程终止。在以下代码中,main方法已经执行,但是为什么不终止呢?

public static void main(String[] args) {
    ExecutorService executorService = Executors.newFixedThreadPool(2);
    executorService.execute(() -> {
        while (true) {
            throw new RuntimeException();
        }
    });
}
Run Code Online (Sandbox Code Playgroud)

MK.*_*MK. 7

您的运行时异常发生在ExecutorService线程池中。它捕获并吞下异常,并且线程继续运行。
当至少有一个非守护线程正在运行时,应用程序将继续运行。您有2个正在运行(在池中)。现在,如果在离开主线程之前调用executorService.shutdown(),它将完成所有任务的运行,然后应用程序将退出。