Why the main thread is not terminated

dai :

As far as I know, the uncaught thread will terminate with the current thread. In the following code, the main method has been executed, but why is it not terminated?

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

Your runtime exception happens in the ExecutorService thread pool. It catches and swallows the exception and the thread keeps running.
The application will keep running while there is at least one non-daemon thread running. You have 2 of them running (in the pool). Now if before leaving the main thread you call executorService.shutdown(), then it will finish running all your tasks and then application will exit.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=140202&siteId=1