Java 为什么主线程没有终止

Java 为什么主线程没有终止,java,threadpool,main,executorservice,Java,Threadpool,Main,Executorservice,据我所知,未捕获的线程将以当前线程终止。 在下面的代码中,main方法已经执行,但是为什么它没有终止呢 public static void main(String[] args) { ExecutorService executorService = Executors.newFixedThreadPool(2); executorService.execute(() -> { while (true) { throw new Ru

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

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

运行时异常发生在ExecutorService线程池中。它捕获并吞咽异常,线程继续运行。
当至少有一个非守护进程线程在运行时,应用程序将保持运行。你有两个在游泳池里跑步。
现在,如果在离开主线程之前调用executorService.shutdown(),那么它将完成运行您的所有任务,然后应用程序将退出。

那么,您运行这个程序,并且它从未停止运行?@SamOrozco是的,您可以练习它。这个问题的实质是守护进程线程和非守护进程线程。我会去了解他们,谢谢你的回答。