有:
ExecutorService executor = Executors.newFixedThreadPool(threadPoolSize);
需要正确停止执行器,以免中断正在运行的线程并删除所有在队列中等待的线程。
shutdownNow()- 降低执行者,所以这是不可能的。
shutdown()- 等到整个队列以及预期的队列结束,并且有数以万计的队列。
试过:
BlockingQueue<Runnable> queue = new ArrayBlockingQueue<>(threadPoolSize);
executor = new ThreadPoolExecutor(threadPoolSize, threadPoolSize, 0L, TimeUnit.MILLISECONDS, queue);
然后:
executor.shutdown();
queue.clear();
但是这里出了点问题,因为队列没有被刷新。
这是一个清除队列的工作示例。
只运行一项任务。