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 却不会执行到该语句。绞尽脑汁不得解,还望哪位大神解答。