<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          線程池是如何執(zhí)行的?拒絕策略有哪些?

          共 488字,需瀏覽 1分鐘

           ·

          2022-03-17 04:19

          作者 | 磊哥

          來(lái)源 | Java面試真題解析(ID:aimianshi666)

          轉(zhuǎn)載請(qǐng)聯(lián)系授權(quán)(微信ID:GG_Stone)

          聊到線程池就一定會(huì)聊到線程池的執(zhí)行流程,也就是當(dāng)有一個(gè)任務(wù)進(jìn)入線程池之后,線程池是如何執(zhí)行的?我們今天就來(lái)聊聊這個(gè)話題。線程池是如何執(zhí)行的?線程池的拒絕策略有哪些?

          線程池執(zhí)行流程

          想要真正的了解線程池的執(zhí)行流程,就得先從線程池的執(zhí)行方法 execute() 說起,execute() 實(shí)現(xiàn)源碼如下:

          public?void?execute(Runnable?command)?{
          ????if?(command?==?null)
          ????????throw?new?NullPointerException();
          ????int?c?=?ctl.get();
          ????//?當(dāng)前工作的線程數(shù)小于核心線程數(shù)
          ????if?(workerCountOf(c)?????????//?創(chuàng)建新的線程執(zhí)行此任務(wù)
          ????????if?(addWorker(command,?true))
          ????????????return;
          ????????c?=?ctl.get();
          ????}
          ????//?檢查線程池是否處于運(yùn)行狀態(tài),如果是則把任務(wù)添加到隊(duì)列
          ????if?(isRunning(c)?&&?workQueue.offer(command))?{
          ????????int?recheck?=?ctl.get();
          ????????//?再次檢線程池是否處于運(yùn)行狀態(tài),防止在第一次校驗(yàn)通過后線程池關(guān)閉
          ????????//?如果是非運(yùn)行狀態(tài),則將剛加入隊(duì)列的任務(wù)移除
          ????????if?(!?isRunning(recheck)?&&?remove(command))
          ????????????reject(command);
          ????????//?如果線程池的線程數(shù)為?0?時(shí)(當(dāng)?corePoolSize?設(shè)置為?0?時(shí)會(huì)發(fā)生)
          ????????else?if?(workerCountOf(recheck)?==?0)
          ????????????addWorker(null,?false);?//?新建線程執(zhí)行任務(wù)
          ????}
          ????//?核心線程都在忙且隊(duì)列都已爆滿,嘗試新啟動(dòng)一個(gè)線程執(zhí)行失敗
          ????else?if?(!addWorker(command,?false))?
          ????????//?執(zhí)行拒絕策略
          ????????reject(command);
          }

          從上述源碼我們可以看出,當(dāng)任務(wù)來(lái)了之后,線程池的執(zhí)行流程是:先判斷當(dāng)前線程數(shù)是否大于核心線程數(shù)?如果結(jié)果為 false,則新建線程并執(zhí)行任務(wù);如果結(jié)果為 true,則判斷任務(wù)隊(duì)列是否已滿?如果結(jié)果為 false,則把任務(wù)添加到任務(wù)隊(duì)列中等待線程執(zhí)行,否則則判斷當(dāng)前線程數(shù)量是否超過最大線程數(shù)?如果結(jié)果為 false,則新建線程執(zhí)行此任務(wù),否則將執(zhí)行線程池的拒絕策略,如下圖所示:

          線程池拒絕策略

          當(dāng)任務(wù)過多且線程池的任務(wù)隊(duì)列已滿時(shí),此時(shí)就會(huì)執(zhí)行線程池的拒絕策略,線程池的拒絕策略默認(rèn)有以下 4 種:

          1. AbortPolicy:中止策略,線程池會(huì)拋出異常并中止執(zhí)行此任務(wù);
          2. CallerRunsPolicy:把任務(wù)交給添加此任務(wù)的(main)線程來(lái)執(zhí)行;
          3. DiscardPolicy:忽略此任務(wù),忽略最新的一個(gè)任務(wù);
          4. DiscardOldestPolicy:忽略最早的任務(wù),最先加入隊(duì)列的任務(wù)。

          默認(rèn)的拒絕策略為 AbortPolicy 中止策略。

          DiscardPolicy拒絕策略

          接下來(lái)我們以 DiscardPolicy 忽略此任務(wù),忽略最新的一個(gè)任務(wù)為例,演示一下拒絕策略的具體使用,實(shí)現(xiàn)代碼如下:

          public?static?void?main(String[]?args)?{
          ????//?任務(wù)的具體方法
          ????Runnable?runnable?=?new?Runnable()?{
          ????????@Override
          ????????public?void?run()?{
          ????????????System.out.println("當(dāng)前任務(wù)被執(zhí)行,執(zhí)行時(shí)間:"?+?new?Date()?+
          ???????????????????????????????"?執(zhí)行線程:"?+?Thread.currentThread().getName());
          ????????????try?{
          ????????????????//?等待?1s
          ????????????????TimeUnit.SECONDS.sleep(1);
          ????????????}?catch?(InterruptedException?e)?{
          ????????????????e.printStackTrace();
          ????????????}
          ????????}
          ????};
          ????//?創(chuàng)建線程,線程的任務(wù)隊(duì)列的長(zhǎng)度為?1
          ????ThreadPoolExecutor?threadPool?=?new?ThreadPoolExecutor(1,?1,
          ???????????????????????????????????????????????????????????100,?TimeUnit.SECONDS,?new?LinkedBlockingQueue<>(1),
          ???????????????????????????????????????????????????????????new?ThreadPoolExecutor.DiscardPolicy());
          ????//?添加并執(zhí)行?4?個(gè)任務(wù)
          ????threadPool.execute(runnable);
          ????threadPool.execute(runnable);
          ????threadPool.execute(runnable);
          ????threadPool.execute(runnable);
          ????//?線程池執(zhí)行完任務(wù),關(guān)閉線程池
          ????threadPool.shutdown();
          }

          以上程序的執(zhí)行結(jié)果如下:從上述執(zhí)行結(jié)果可以看出,給線程池添加了 4 個(gè)任務(wù),而線程池只執(zhí)行了 2 個(gè)任務(wù)就結(jié)束了,其他兩個(gè)任務(wù)執(zhí)行了拒絕策略 DiscardPolicy 被忽略了,這就是拒絕策略的作用。

          AbortPolicy拒絕策略

          為了和 DiscardPolicy 拒絕策略對(duì)比,我們來(lái)演示一下 JDK 默認(rèn)的拒絕策略 AbortPolicy 中止策略,線程池會(huì)拋出異常并中止執(zhí)行此任務(wù),示例代碼如下:

          public?static?void?main(String[]?args)?{
          ????//?任務(wù)的具體方法
          ????Runnable?runnable?=?new?Runnable()?{
          ????????@Override
          ????????public?void?run()?{
          ????????????System.out.println("當(dāng)前任務(wù)被執(zhí)行,執(zhí)行時(shí)間:"?+?new?Date()?+
          ???????????????????????????????"?執(zhí)行線程:"?+?Thread.currentThread().getName());
          ????????????try?{
          ????????????????//?等待?1s
          ????????????????TimeUnit.SECONDS.sleep(1);
          ????????????}?catch?(InterruptedException?e)?{
          ????????????????e.printStackTrace();
          ????????????}
          ????????}
          ????};
          ????//?創(chuàng)建線程,線程的任務(wù)隊(duì)列的長(zhǎng)度為?1
          ????ThreadPoolExecutor?threadPool?=?new?ThreadPoolExecutor(1,?1,
          ???????????????????????????????????????????????????????????100,?TimeUnit.SECONDS,?new?LinkedBlockingQueue<>(1),
          ???????????????????????????????????????????????????????????new?ThreadPoolExecutor.AbortPolicy());?//?顯式指定拒絕策略,也可以忽略此設(shè)置,它為默認(rèn)拒絕策略
          ????//?添加并執(zhí)行?4?個(gè)任務(wù)
          ????threadPool.execute(runnable);
          ????threadPool.execute(runnable);
          ????threadPool.execute(runnable);
          ????threadPool.execute(runnable);
          ????//?線程池執(zhí)行完任務(wù),關(guān)閉線程池
          ????threadPool.shutdown();
          }

          以上程序的執(zhí)行結(jié)果如下:從結(jié)果可以看出,給線程池添加了 4 個(gè)任務(wù),線程池正常執(zhí)行了 2 個(gè)任務(wù),其他兩個(gè)任務(wù)執(zhí)行了中止策略,并拋出了拒絕執(zhí)行的異常 RejectedExecutionException。

          自定義拒絕策略

          當(dāng)然除了 JDK 提供的四種拒絕策略之外,我們還可以實(shí)現(xiàn)通過 new RejectedExecutionHandler,并重寫 rejectedExecution 方法來(lái)實(shí)現(xiàn)自定義拒絕策略,實(shí)現(xiàn)代碼如下:

          public?static?void?main(String[]?args)?{
          ????//?任務(wù)的具體方法
          ????Runnable?runnable?=?new?Runnable()?{
          ????????@Override
          ????????public?void?run()?{
          ????????????System.out.println("當(dāng)前任務(wù)被執(zhí)行,執(zhí)行時(shí)間:"?+?new?Date()?+
          ???????????????????????????????"?執(zhí)行線程:"?+?Thread.currentThread().getName());
          ????????????try?{
          ????????????????//?等待?1s
          ????????????????TimeUnit.SECONDS.sleep(1);
          ????????????}?catch?(InterruptedException?e)?{
          ????????????????e.printStackTrace();
          ????????????}
          ????????}
          ????};
          ????//?創(chuàng)建線程,線程的任務(wù)隊(duì)列的長(zhǎng)度為?1
          ????ThreadPoolExecutor?threadPool?=?new?ThreadPoolExecutor(1,?1,
          ???????????????????????????????????????????????????????????100,?TimeUnit.SECONDS,?new?LinkedBlockingQueue<>(1),
          ???????????????????????????????????????????????????????????new?RejectedExecutionHandler()?{
          ???????????????????????????????????????????????????????????????@Override
          ???????????????????????????????????????????????????????????????public?void?rejectedExecution(Runnable?r,?ThreadPoolExecutor?executor)?{
          ???????????????????????????????????????????????????????????????????//?執(zhí)行自定義拒絕策略的相關(guān)操作
          ???????????????????????????????????????????????????????????????????System.out.println("我是自定義拒絕策略~");
          ???????????????????????????????????????????????????????????????}
          ???????????????????????????????????????????????????????????});
          ????//?添加并執(zhí)行?4?個(gè)任務(wù)
          ????threadPool.execute(runnable);
          ????threadPool.execute(runnable);
          ????threadPool.execute(runnable);
          ????threadPool.execute(runnable);
          }

          以上程序的執(zhí)行結(jié)果如下:

          總結(jié)

          線程池的執(zhí)行流程有 3 個(gè)重要的判斷點(diǎn)(判斷順序依次往后):判斷當(dāng)前線程數(shù)和核心線程數(shù)、判斷當(dāng)前任務(wù)隊(duì)列是否已滿、判斷當(dāng)前線程數(shù)是否已達(dá)到最大線程數(shù)。如果經(jīng)過以上 3 個(gè)判斷,得到的結(jié)果都會(huì) true,則會(huì)執(zhí)行線程池的拒絕策略。JDK 提供了 4 種拒絕策略,我們還可以通過 new RejectedExecutionHandler 并重寫 rejectedExecution 方法來(lái)實(shí)現(xiàn)自定義拒絕策略。

          是非審之于己,毀譽(yù)聽之于人,得失安之于數(shù)。

          公眾號(hào):Java面試真題解析

          面試合集:https://gitee.com/mydb/interview


          往期推薦

          面試突擊29:說一下線程池7個(gè)參數(shù)的含義?


          面試突擊28:線程池有幾種創(chuàng)建方式?推薦使用哪種?


          匯總 | 2022版Java最新面試題(共29篇)


          瀏覽 67
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評(píng)論
          圖片
          表情
          推薦
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  日本无码久久嗯啊流水 | 久久er99热精品一区二区 | 国产激情性爱视频 | 久操资源 | 日韩性爱一区 |