Executors.newFixedThreadPool

时间:2016-02-14 11:51:46

标签: java threadpoolexecutor

我无法找到JDK在Executors.newFixedThreadPool中使用LinkedBlockingQueue而不是ArrayBlockingQueue的好处。

return new ThreadPoolExecutor(nThreads, nThreads,
                                      0L, TimeUnit.MILLISECONDS,
                                      new LinkedBlockingQueue<Runnable>());

ArrayBlockingQueue的好处是它的速度很快但是集合应该在开头界定和定义,这在newFixedThreadPool中都适用。

因此,ArrayBlockingQueue无法实现一些好处或额外功能。

1 个答案:

答案 0 :(得分:2)

fixedThreadPool并不意味着内部队列是有界的,而是只能创建最大数量的线程。来自Oracle文档:Tasks are submitted to the pool via an internal queue, which holds extra tasks whenever there are more active tasks than threads.(请参阅https://docs.oracle.com/javase/tutorial/essential/concurrency/pools.html

这导致得出结论,如果队列是无界的,那么LinkedBlockingQueue更相关,因为ArrayBlockingQueue是有界的,并且它的大小不能改变。