我使用了几乎默认的 newCachedThreadPool,但我想限制线程创建,所以我像这样创建 ExecutorService
new ThreadPoolExecutor(0, Runtime.getRuntime().availableProcessors() * 2,
60L, TimeUnit.SECONDS,
new SynchronousQueue<>());
阅读 javadocs 后,我希望它的工作方式是,当我提交 Runnable 时,taskExecutor 在向 SynchronousQueue 提供新任务时会阻塞,直到有可用线程来执行它,然后发生切换。不幸的是,在达到线程池容量后并且所有线程都忙时,taskExecutor 会抛出 RejectedExecutionException。我知道我可以传递一个会阻塞的 RejectedExecutionHandler,但我很惊讶似乎我必须这样做。有人可以解释一下它是否真的按预期工作还是我做错了什么?
此代码重现了我的案例:
public static void main(String[] args) {
ThreadPoolExecutor executor = new ThreadPoolExecutor(0, Runtime.getRuntime().availableProcessors() * 2,
60L, TimeUnit.SECONDS,
new SynchronousQueue<>());
while (true) {
executor.submit(() -> System.out.println("bla"));
}
}
最佳答案
这符合ThreadPoolExecutor API:
• 如果请求无法排队,则会创建一个新线程,除非这超出了 MaximumPoolSize,在这种情况下,该任务将被拒绝。
至于为什么 SynchronousQueue
不会阻塞 - 因为 ThreadPoolExecutor
使用 queue.offer()
而不是 put()
关于java - 当 ThreadPoolExecutor 提供任务时,SynchronousQueue 不会阻塞,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44541784/