wen涛 2019-07-03 15:44 采纳率: 0%
浏览 800

关于ThreadPoolExecutor 拒绝任务

public static void main(String... args) throws ExecutionException, InterruptedException {
        ThreadPoolExecutor executor = new ThreadPoolExecutor(2, 2, 60 * 10L,
                TimeUnit.SECONDS, new LinkedBlockingQueue<>(2), new ThreadPoolExecutor.AbortPolicy());
        List<Future> futures = new ArrayList<>();
        System.out.println("main:" + Thread.currentThread().getName());
        for (int i = 0; i < 20; i++) {
            try {
                futures.add(executor.submit(new Test.Worker(i)));
            } catch (Exception e) {
                System.out.println("what's the fuck");
            }
            if (futures.size() > 2) {
                for (Future future : futures) {
                    future.get();
                }
                futures.clear();
            }
        }
        for (Future future : futures) {
            future.get();
        }
        executor.shutdown();
    }

按道理,增加的任务不会达到 maxPoolSize 和 queue 之和,“whats the ”应该是不会执行,但是 通过IDE run的时候还是会走到,但是 debug 却不会执行到该语句。绞尽脑汁不得解,还望哪位大神解答。

  • 写回答

2条回答 默认 最新

  • 毕小宝 领域专家: 后端开发技术领域 2019-07-03 16:49
    关注

    你把堆栈异常打出来,看看最终是什么异常。

    评论

报告相同问题?