ThreadPoolExecutor 从不 运行 超过 poolsize
ThreadPoolExecutor never run exceeding poolsize
无论我设置池的大小,运行 结果似乎都不会超过池 size.For 示例:
BlockingQueue<Runnable> queue = new LinkedBlockingQueue<>();
ThreadPoolExecutor executor = new ThreadPoolExecutor(8, 15, 1, TimeUnit.DAYS, queue);
executor.execute(new Runnable() {
public void run() {
}
});
当我设置8时,前8个结果会同步。不过下面只有运行一一。
出了什么问题以及如何解决?
阅读the javadoc。
它说:
Queuing
Any BlockingQueue may be used to transfer and hold submitted tasks. The use of this queue interacts with pool sizing:
If fewer than corePoolSize threads are running, the Executor always prefers adding a new thread rather than queuing.
If corePoolSize or more threads are running, the Executor always prefers queuing a request rather than adding a new thread.
If a request cannot be queued, a new thread is created unless this would exceed maximumPoolSize, in which case, the task will be rejected.
[...]
Unbounded queues. Using an unbounded queue (for example a LinkedBlockingQueue without a predefined capacity) will cause new tasks to wait in the queue when all corePoolSize threads are busy. Thus, no more than corePoolSize threads will ever be created.
无论我设置池的大小,运行 结果似乎都不会超过池 size.For 示例:
BlockingQueue<Runnable> queue = new LinkedBlockingQueue<>();
ThreadPoolExecutor executor = new ThreadPoolExecutor(8, 15, 1, TimeUnit.DAYS, queue);
executor.execute(new Runnable() {
public void run() {
}
});
当我设置8时,前8个结果会同步。不过下面只有运行一一。
出了什么问题以及如何解决?
阅读the javadoc。
它说:
Queuing
Any BlockingQueue may be used to transfer and hold submitted tasks. The use of this queue interacts with pool sizing:
If fewer than corePoolSize threads are running, the Executor always prefers adding a new thread rather than queuing. If corePoolSize or more threads are running, the Executor always prefers queuing a request rather than adding a new thread. If a request cannot be queued, a new thread is created unless this would exceed maximumPoolSize, in which case, the task will be rejected.
[...]
Unbounded queues. Using an unbounded queue (for example a LinkedBlockingQueue without a predefined capacity) will cause new tasks to wait in the queue when all corePoolSize threads are busy. Thus, no more than corePoolSize threads will ever be created.