java 线程池 陷入无限等待

315 阅读1分钟
    public static void main(String[] args) {
        ThreadPoolExecutor poolExecutor = new ThreadPoolExecutor(1,10,1000*10, TimeUnit.SECONDS, new LinkedBlockingDeque<>());
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                System.out.println("do thread work");
                throw new RuntimeException("error");
            }
        };
        
        Future future = poolExecutor.submit(runnable);
        Integer  i = Integer.valueOf("0\n"); //这里会有异常,然后主线程陷入无限等待中,注释掉这一行就可以执行结束
        try {
            future.get();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
        poolExecutor.shutdown();
    }
}

主线程遇到异常,会陷入无限等待 这里其实没有陷入无限等待,只是主线程异常,导致线程池没有关闭而已