【騰訊阿里最全面試題】Java 線程池的實(shí)現(xiàn)原理,ThreadPoolExecutor關(guān)鍵參數(shù)解釋

【騰訊阿里最全面試題】Java 線程池的實(shí)現(xiàn)原理,ThreadPoolExecutor關(guān)鍵參數(shù)解釋

Thread Pool, concurrency concept.
java.util.concurrent.ThreadPoolExecutor:
The API documentation :ThreadPoolExecutor. https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ThreadPoolExecutor.html
is comprehensive and detailed but difficult to imagine the working flow since there's no diagram or illustration, only descriptive text. But another clue that enlightens me to understand about it is the producer/consumer pattern.

Simply put, tasks are sent from multiple producers into a waiting queue, waiting to be handled by multiple consumers.

https://medium.com/@vipulgupta_19290/threadpool-or-executor-framework-7007844cac52

Initially, there are 2 available threads in the thread pool. The 2 first tasks go through the queue and get redirected to idle Thread 1 and 2 for execution.
When task 3 is enqueued, as there’s no available thread, ThreadFactory inside the ThreadPoolExecutor creates a new Thread 3 to handle the task. The same thing happens to task 4 and 5 with the creation of Thread 4 and 5. The maximum thread pool size is now reached, no more thread can be created.
Task 6, 7, 8 are enqueued and wait until there’s an available Thread worker to handle.
When the queue is full, it rejects any incoming tasks, so task 9 and 10 get blocked and redirected to a fallback function.
Notes that if Thread 3, 4 or 5 is idle longer than keepAliveInMinutes period, it will get disposed. The thread pool shrinks its size to 2 (coreSize).
In summary, there are 8 tasks get executed and 2 get rejected.
That’s it! I hope this article is informative and useful for you. Feel free have any question or concern on the comment section below.
Thank you for your time!

Model-Relationship

ThreadPoolExecutor.png

其中,Worker 的模型如下:

ThreadPoolExecutor 線程池的幾個(gè)主要參數(shù)的作用

public ThreadPoolExecutor(int corePoolSize,
                              int maximumPoolSize,
                              long keepAliveTime,
                              TimeUnit unit,
                              BlockingQueue<Runnable> workQueue,
                              ThreadFactory threadFactory,
                              RejectedExecutionHandler handler)

1、corePoolSize: 規(guī)定線程池有幾個(gè)線程(worker)在運(yùn)行。
2、maximumPoolSize: 當(dāng)workQueue滿了,不能添加任務(wù)的時(shí)候,這個(gè)參數(shù)才會(huì)生效。規(guī)定線程池最多只能有多少個(gè)線程(worker)在執(zhí)行。
3、keepAliveTime: 超出corePoolSize大小的那些線程的生存時(shí)間,這些線程如果長時(shí)間沒有執(zhí)行任務(wù)并且超過了keepAliveTime設(shè)定的時(shí)間,就會(huì)消亡。
4、unit: 生存時(shí)間對于的單位
5、workQueue: 存放任務(wù)的隊(duì)列
6、threadFactory: 創(chuàng)建線程的工廠
7、handler: 當(dāng)workQueue已經(jīng)滿了,并且線程池線程數(shù)已經(jīng)達(dá)到8、maximumPoolSize,將執(zhí)行拒絕策略。

任務(wù)提交后的流程分析

用戶通過submit提交一個(gè)任務(wù)。線程池會(huì)執(zhí)行如下流程:

  • 判斷當(dāng)前運(yùn)行的worker數(shù)量是否超過corePoolSize,如果不超過corePoolSize。就創(chuàng)建一個(gè)worker直接執(zhí)行該任務(wù)?!?線程池最開始是沒有worker在運(yùn)行的
  • 如果正在運(yùn)行的worker數(shù)量超過或者等于corePoolSize,那么就將該任務(wù)加入到workQueue隊(duì)列中去。
  • 如果workQueue隊(duì)列滿了,也就是offer方法返回false的話,就檢查當(dāng)前運(yùn)行的worker數(shù)量是否小于maximumPoolSize,如果小于就創(chuàng)建一個(gè)worker直接執(zhí)行該任務(wù)。
  • 如果當(dāng)前運(yùn)行的worker數(shù)量是否大于等于maximumPoolSize,那么就執(zhí)行RejectedExecutionHandler來拒絕這個(gè)任務(wù)的提交。

參考資料

https://medium.com/@truongminhtriet96/playing-with-hystrix-thread-pool-c7eebb5b0ddc

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容