我不明白爲什麼看起來只有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