我不明白为什么看起来只有4个新的线程开始和计数,尽管8提交。
不要在线程和任务之间混淆。您已为4个线程创建了固定的ThreadPool,并且池中只剩下4个线程。
检查的Executors
/**
* Creates a thread pool that reuses a fixed number of threads
* operating off a shared unbounded queue. At any point, at most
* <tt>nThreads</tt> threads will be active processing tasks.
* If additional tasks are submitted when all threads are active,
* they will wait in the queue until a thread is available.
* If any thread terminates due to a failure during execution
* prior to shutdown, a new one will take its place if needed to
* execute subsequent tasks. The threads in the pool will exist
* until it is explicitly {@link ExecutorService#shutdown shutdown}.
*
* @param nThreads the number of threads in the pool
* @return the newly created thread pool
* @throws IllegalArgumentException if <tt>nThreads <= 0</tt>
*/
public static ExecutorService newFixedThreadPool(int nThreads) {
return new ThreadPoolExecutor(nThreads, nThreads,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>());
}
实现您可以找到有关官方文档页面的ThreadPoolExecutor各个参数的更多细节。
在你的情况下,线程数是4,职工任务(正在实施Runnable接口)数量8
他们是如何存储,如何对它们进行访问?是否也可以在中途操纵它们,也许为什么其他呼叫到达?
- 任务被提交到任务队列(
BlockingQueue
)
- 如果
BlockingQueue
满(有界队列),拒绝策略会被触发。
- 如果BlockingQueue未满,工作任务将等待线程提取它们。
相关SE帖子:
How to properly use Java Executor?
Task getting rejected although threads are available
*是此行为意*哪些行为?你觉得什么令人惊讶?你会期待什么样的行为? – shmosel
@shmosel,如果你不熟悉优惠条款,我会参考你的阅读:http://dictionary.cambridge.org/dictionary/english/concessive-clause – blauerschluessel
**在我的情况下,提交8 C,即使ExecutorService具有4 **的固定大小,Executors.newFixedThreadPool(int),处理当前同时运行的4个线程,并且当任何线程完成其执行时,通过e.execute(Runnable)排队的其他4个线程将采用取代死亡线程的位置,否则那些OTHER 4 FROM 8线程将等待OTHER 8 FROM线程完成,这些线程在池中处于运行状态,没有意图让人惊讶,尽管 – ShayHaned