LinkedBlockingQueue 在 Executors.newFixedThreadPool 中的优势
benefit of LinkedBlockingQueue in Executors.newFixedThreadPool
我无法找出为什么 JDK 在 Executors.newFixedThreadPool 中使用 LinkedBlockingQueue 而不是 ArrayBlockingQueue 的好处。
return new ThreadPoolExecutor(nThreads, nThreads,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>());
ArrayBlockingQueue的好处是速度快,但是collection在开始的时候就应该有界定义,这在newFixedThreadPool中都是适用的
因此,ArrayBlockingQueue 无法实现某些优势或所需的额外功能。
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 是有界的并且其大小无法更改。
我无法找出为什么 JDK 在 Executors.newFixedThreadPool 中使用 LinkedBlockingQueue 而不是 ArrayBlockingQueue 的好处。
return new ThreadPoolExecutor(nThreads, nThreads,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>());
ArrayBlockingQueue的好处是速度快,但是collection在开始的时候就应该有界定义,这在newFixedThreadPool中都是适用的
因此,ArrayBlockingQueue 无法实现某些优势或所需的额外功能。
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 是有界的并且其大小无法更改。