ExecutorService executor = new ThreadPoolExecutor(5, 10, 10, TimeUnit.SECONDS, new LinkedBlockingDeque<>(5), new ThreadPoolExecutor.AbortPolicy()); for (int i=0; i<5; i++) { Task task = new Task(); try { executor.submit(task); } catch (Exception e) { LOGGER.error(e.getMessage()); } Thread.sleep(100); if (i == 2) { executorService.shutdown(); //executorService.shutdownNow(); } } LOGGER.info("done");
shutdown结果
17:34:01:347 INFO [pool-2-thread-1] run 17:34:01:456 INFO [pool-2-thread-2] run 17:34:01:566 INFO [pool-2-thread-3] run 17:34:01:675 ERROR [main] Task rejected from ThreadPoolExecutor@36b4cef0[Shutting down] 17:34:01:784 ERROR [main] Task rejected from ThreadPoolExecutor@36b4cef0[Shutting down] 17:34:01:894 INFO [main] done 17:34:02:347 INFO [pool-2-thread-1] done 17:34:02:456 INFO [pool-2-thread-2] done 17:34:02:566 INFO [pool-2-thread-3] done
17:37:00:663 INFO [pool-2-thread-1] run 17:37:00:756 INFO [pool-2-thread-2] run 17:37:00:866 INFO [pool-2-thread-3] run 17:37:00:975 ERROR [pool-2-thread-3] sleep interrupted 17:37:00:975 ERROR [pool-2-thread-2] sleep interrupted 17:37:00:975 ERROR [pool-2-thread-1] sleep interrupted 17:37:00:975 INFO [pool-2-thread-3] done 17:37:00:975 INFO [pool-2-thread-1] done 17:37:00:975 INFO [pool-2-thread-2] done 17:37:00:975 ERROR [main] Task rejected from ThreadPoolExecutor@36b4cef0[Terminated] 17:37:01:085 ERROR [main] Task rejected from ThreadPoolExecutor@36b4cef0[Terminated] 17:37:01:194 INFO [main] done
publicclassFutureTask<V> implementsRunnableFuture<V> { public V get()throws InterruptedException, ExecutionException { int s = state; if (s <= COMPLETING) s = awaitDone(false, 0L); return report(s); } }