最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java concurrency線程池之線程池原理(四)_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理

 更新時(shí)間:2017年06月15日 15:03:33   作者:skywang12345  
這篇文章主要為大家詳細(xì)介紹了Java concurrency線程池之線程池原理,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

拒絕策略介紹

線程池的拒絕策略,是指當(dāng)任務(wù)添加到線程池中被拒絕,而采取的處理措施。

當(dāng)任務(wù)添加到線程池中之所以被拒絕,可能是由于:第一,線程池異常關(guān)閉。第二,任務(wù)數(shù)量超過(guò)線程池的最大限制。

線程池共包括4種拒絕策略,它們分別是:AbortPolicy, CallerRunsPolicy, DiscardOldestPolicy和DiscardPolicy。

  1. AbortPolicy         -- 當(dāng)任務(wù)添加到線程池中被拒絕時(shí),它將拋出 RejectedExecutionException 異常。
  2. CallerRunsPolicy    -- 當(dāng)任務(wù)添加到線程池中被拒絕時(shí),會(huì)在線程池當(dāng)前正在運(yùn)行的Thread線程池中處理被拒絕的任務(wù)。
  3. DiscardOldestPolicy -- 當(dāng)任務(wù)添加到線程池中被拒絕時(shí),線程池會(huì)放棄等待隊(duì)列中最舊的未處理任務(wù),然后將被拒絕的任務(wù)添加到等待隊(duì)列中。
  4. DiscardPolicy       -- 當(dāng)任務(wù)添加到線程池中被拒絕時(shí),線程池將丟棄被拒絕的任務(wù)。

線程池默認(rèn)的處理策略是AbortPolicy!

拒絕策略對(duì)比和示例

下面通過(guò)示例,分別演示線程池的4種拒絕策略。

1. DiscardPolicy 示例

 import java.lang.reflect.Field;
 import java.util.concurrent.ArrayBlockingQueue;
 import java.util.concurrent.ThreadPoolExecutor;
 import java.util.concurrent.TimeUnit;
 import java.util.concurrent.ThreadPoolExecutor.DiscardPolicy;
 
 public class DiscardPolicyDemo {
 
   private static final int THREADS_SIZE = 1;
   private static final int CAPACITY = 1;
 
   public static void main(String[] args) throws Exception {
 
     // 創(chuàng)建線程池。線程池的"最大池大小"和"核心池大小"都為1(THREADS_SIZE),"線程池"的阻塞隊(duì)列容量為1(CAPACITY)。
     ThreadPoolExecutor pool = new ThreadPoolExecutor(THREADS_SIZE, THREADS_SIZE, 0, TimeUnit.SECONDS,
         new ArrayBlockingQueue<Runnable>(CAPACITY));
     // 設(shè)置線程池的拒絕策略為"丟棄"
     pool.setRejectedExecutionHandler(new ThreadPoolExecutor.DiscardPolicy());
 
     // 新建10個(gè)任務(wù),并將它們添加到線程池中。
     for (int i = 0; i < 10; i++) {
       Runnable myrun = new MyRunnable("task-"+i);
       pool.execute(myrun);
     }
     // 關(guān)閉線程池
     pool.shutdown();
   }
 }
 
 class MyRunnable implements Runnable {
   private String name;
   public MyRunnable(String name) {
     this.name = name;
   }
   @Override
   public void run() {
     try {
       System.out.println(this.name + " is running.");
       Thread.sleep(100);
     } catch (Exception e) {
       e.printStackTrace();
     }
   }
 }

運(yùn)行結(jié)果:

task-0 is running.
task-1 is running.

結(jié)果說(shuō)明:線程池pool的"最大池大小"和"核心池大小"都為1(THREADS_SIZE),這意味著"線程池能同時(shí)運(yùn)行的任務(wù)數(shù)量最大只能是1"。

線程池pool的阻塞隊(duì)列是ArrayBlockingQueue,ArrayBlockingQueue是一個(gè)有界的阻塞隊(duì)列,ArrayBlockingQueue的容量為1。這也意味著線程池的阻塞隊(duì)列只能有一個(gè)線程池阻塞等待。

根據(jù)""中分析的execute()代碼可知:線程池中共運(yùn)行了2個(gè)任務(wù)。第1個(gè)任務(wù)直接放到Worker中,通過(guò)線程去執(zhí)行;第2個(gè)任務(wù)放到阻塞隊(duì)列中等待。其他的任務(wù)都被丟棄了!

2. DiscardOldestPolicy 示例

 import java.lang.reflect.Field;
 import java.util.concurrent.ArrayBlockingQueue;
 import java.util.concurrent.ThreadPoolExecutor;
 import java.util.concurrent.TimeUnit;
 import java.util.concurrent.ThreadPoolExecutor.DiscardOldestPolicy;
 
 public class DiscardOldestPolicyDemo {
 
   private static final int THREADS_SIZE = 1;
   private static final int CAPACITY = 1;
 
   public static void main(String[] args) throws Exception {
 
     // 創(chuàng)建線程池。線程池的"最大池大小"和"核心池大小"都為1(THREADS_SIZE),"線程池"的阻塞隊(duì)列容量為1(CAPACITY)。
     ThreadPoolExecutor pool = new ThreadPoolExecutor(THREADS_SIZE, THREADS_SIZE, 0, TimeUnit.SECONDS,
         new ArrayBlockingQueue<Runnable>(CAPACITY));
     // 設(shè)置線程池的拒絕策略為"DiscardOldestPolicy"
     pool.setRejectedExecutionHandler(new ThreadPoolExecutor.DiscardOldestPolicy());
 
     // 新建10個(gè)任務(wù),并將它們添加到線程池中。
     for (int i = 0; i < 10; i++) {
       Runnable myrun = new MyRunnable("task-"+i);
       pool.execute(myrun);
     }
     // 關(guān)閉線程池
     pool.shutdown();
   }
 }
 
 class MyRunnable implements Runnable {
   private String name;
   public MyRunnable(String name) {
     this.name = name;
   }
   @Override
   public void run() {
     try {
       System.out.println(this.name + " is running.");
       Thread.sleep(200);
     } catch (Exception e) {
       e.printStackTrace();
     }
   }
 }

運(yùn)行結(jié)果:

task-0 is running.
task-9 is running.

結(jié)果說(shuō)明:將"線程池的拒絕策略"由DiscardPolicy修改為DiscardOldestPolicy之后,當(dāng)有任務(wù)添加到線程池被拒絕時(shí),線程池會(huì)丟棄阻塞隊(duì)列中末尾的任務(wù),然后將被拒絕的任務(wù)添加到末尾。 

3. AbortPolicy 示例

 import java.lang.reflect.Field;
 import java.util.concurrent.ArrayBlockingQueue;
 import java.util.concurrent.ThreadPoolExecutor;
 import java.util.concurrent.TimeUnit;
 import java.util.concurrent.ThreadPoolExecutor.AbortPolicy;
 import java.util.concurrent.RejectedExecutionException;
 
 public class AbortPolicyDemo {
 
   private static final int THREADS_SIZE = 1;
   private static final int CAPACITY = 1;
 
   public static void main(String[] args) throws Exception {
 
     // 創(chuàng)建線程池。線程池的"最大池大小"和"核心池大小"都為1(THREADS_SIZE),"線程池"的阻塞隊(duì)列容量為1(CAPACITY)。
     ThreadPoolExecutor pool = new ThreadPoolExecutor(THREADS_SIZE, THREADS_SIZE, 0, TimeUnit.SECONDS,
         new ArrayBlockingQueue<Runnable>(CAPACITY));
     // 設(shè)置線程池的拒絕策略為"拋出異常"
     pool.setRejectedExecutionHandler(new ThreadPoolExecutor.AbortPolicy());
 
     try {
 
       // 新建10個(gè)任務(wù),并將它們添加到線程池中。
       for (int i = 0; i < 10; i++) {
         Runnable myrun = new MyRunnable("task-"+i);
         pool.execute(myrun);
       }
     } catch (RejectedExecutionException e) {
       e.printStackTrace();
       // 關(guān)閉線程池
       pool.shutdown();
     }
   }
 }
 
 class MyRunnable implements Runnable {
   private String name;
   public MyRunnable(String name) {
     this.name = name;
   }
   @Override
   public void run() {
     try {
       System.out.println(this.name + " is running.");
       Thread.sleep(200);
     } catch (Exception e) {
       e.printStackTrace();
     }
   }
 }

(某一次)運(yùn)行結(jié)果:

java.util.concurrent.RejectedExecutionException
  at java.util.concurrent.ThreadPoolExecutor$AbortPolicy.rejectedExecution(ThreadPoolExecutor.java:1774)
  at java.util.concurrent.ThreadPoolExecutor.reject(ThreadPoolExecutor.java:768)
  at java.util.concurrent.ThreadPoolExecutor.execute(ThreadPoolExecutor.java:656)
  at AbortPolicyDemo.main(AbortPolicyDemo.java:27)
task-0 is running.
task-1 is running.

結(jié)果說(shuō)明:將"線程池的拒絕策略"由DiscardPolicy修改為AbortPolicy之后,當(dāng)有任務(wù)添加到線程池被拒絕時(shí),會(huì)拋出RejectedExecutionException。

4. CallerRunsPolicy 示例

 import java.lang.reflect.Field;
 import java.util.concurrent.ArrayBlockingQueue;
 import java.util.concurrent.ThreadPoolExecutor;
 import java.util.concurrent.TimeUnit;
 import java.util.concurrent.ThreadPoolExecutor.CallerRunsPolicy;
 
 public class CallerRunsPolicyDemo {
 
   private static final int THREADS_SIZE = 1;
   private static final int CAPACITY = 1;
 
   public static void main(String[] args) throws Exception {
 
     // 創(chuàng)建線程池。線程池的"最大池大小"和"核心池大小"都為1(THREADS_SIZE),"線程池"的阻塞隊(duì)列容量為1(CAPACITY)。
     ThreadPoolExecutor pool = new ThreadPoolExecutor(THREADS_SIZE, THREADS_SIZE, 0, TimeUnit.SECONDS,
         new ArrayBlockingQueue<Runnable>(CAPACITY));
     // 設(shè)置線程池的拒絕策略為"CallerRunsPolicy"
     pool.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
 
     // 新建10個(gè)任務(wù),并將它們添加到線程池中。
     for (int i = 0; i < 10; i++) {
       Runnable myrun = new MyRunnable("task-"+i);
       pool.execute(myrun);
     }
 
     // 關(guān)閉線程池
     pool.shutdown();
   }
 }
 
 class MyRunnable implements Runnable {
   private String name;
   public MyRunnable(String name) {
     this.name = name;
   }
   @Override
   public void run() {
     try {
       System.out.println(this.name + " is running.");
       Thread.sleep(100);
     } catch (Exception e) {
       e.printStackTrace();
     }
   }
 }

(某一次)運(yùn)行結(jié)果:

task-2 is running.
task-3 is running.
task-4 is running.
task-5 is running.
task-6 is running.
task-7 is running.
task-8 is running.
task-9 is running.
task-0 is running.
task-1 is running.

結(jié)果說(shuō)明:將"線程池的拒絕策略"由DiscardPolicy修改為CallerRunsPolicy之后,當(dāng)有任務(wù)添加到線程池被拒絕時(shí),線程池會(huì)將被拒絕的任務(wù)添加到"線程池正在運(yùn)行的線程"中取運(yùn)行

相關(guān)文章

  • 利用Java實(shí)現(xiàn)和可被K整除的子數(shù)組完整實(shí)例

    利用Java實(shí)現(xiàn)和可被K整除的子數(shù)組完整實(shí)例

    這篇文章主要給大家介紹了關(guān)于利用Java實(shí)現(xiàn)和可被K整除的子數(shù)組的相關(guān)資料,這道題來(lái)自力扣,通過(guò)學(xué)習(xí)這道題的解題思路以及代碼對(duì)大家的學(xué)習(xí)或者工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2024-01-01
  • SSH框架網(wǎng)上商城項(xiàng)目第16戰(zhàn)之Hibernate二級(jí)緩存處理首頁(yè)熱門顯示

    SSH框架網(wǎng)上商城項(xiàng)目第16戰(zhàn)之Hibernate二級(jí)緩存處理首頁(yè)熱門顯示

    這篇文章主要介紹了SSH框架網(wǎng)上商城項(xiàng)目第16戰(zhàn)之Hibernate的二級(jí)緩存處理首頁(yè)的熱門顯示,感興趣的小伙伴們可以參考一下
    2016-06-06
  • Springboot實(shí)現(xiàn)驗(yàn)證碼登錄

    Springboot實(shí)現(xiàn)驗(yàn)證碼登錄

    這篇文章主要為大家詳細(xì)介紹了Springboot實(shí)現(xiàn)驗(yàn)證碼登錄,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-07-07
  • SpringBoot根據(jù)各地區(qū)時(shí)間設(shè)置接口有效時(shí)間的實(shí)現(xiàn)方式

    SpringBoot根據(jù)各地區(qū)時(shí)間設(shè)置接口有效時(shí)間的實(shí)現(xiàn)方式

    這篇文章給大家介紹了SpringBoot根據(jù)各地區(qū)時(shí)間設(shè)置接口有效時(shí)間的實(shí)現(xiàn)方式,文中通過(guò)代碼示例給大家講解的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下
    2024-01-01
  • Feign如何設(shè)置超時(shí)時(shí)間(不同情況)

    Feign如何設(shè)置超時(shí)時(shí)間(不同情況)

    本文主要介紹了Feign的超時(shí)時(shí)間設(shè)置,包括單獨(dú)使用Feign和在SpringCloud環(huán)境下的設(shè)置方式,以及與Ribbon和Hystrix的配合使用,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2024-11-11
  • Spring Cache的基本使用與實(shí)現(xiàn)原理詳解

    Spring Cache的基本使用與實(shí)現(xiàn)原理詳解

    緩存是實(shí)際工作中非經(jīng)常常使用的一種提高性能的方法, 我們會(huì)在很多場(chǎng)景下來(lái)使用緩存。下面這篇文章主要給大家介紹了關(guān)于Spring Cache的基本使用與實(shí)現(xiàn)原理的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2018-05-05
  • Java對(duì)稱與非對(duì)稱加密算法原理詳細(xì)講解

    Java對(duì)稱與非對(duì)稱加密算法原理詳細(xì)講解

    對(duì)稱加密算法指加密和解密使用相同密鑰的加密算法。對(duì)稱加密算法用來(lái)對(duì)敏感數(shù)據(jù)等信息進(jìn)行加密,非對(duì)稱加密算法指加密和解密使用不同密鑰的加密算法,也稱為公私鑰加密
    2022-11-11
  • MybatisGenerator文件生成不出對(duì)應(yīng)文件的問(wèn)題

    MybatisGenerator文件生成不出對(duì)應(yīng)文件的問(wèn)題

    本文介紹了使用MybatisGenerator生成文件時(shí)遇到的問(wèn)題及解決方法,主要步驟包括檢查目標(biāo)表是否存在、是否能連接到數(shù)據(jù)庫(kù)、配置生成器的路徑等,通過(guò)在項(xiàng)目結(jié)構(gòu)中引入相應(yīng)的jar包,并在GeneratorSqlmap.java文件中運(yùn)行,可以成功生成對(duì)應(yīng)的文件
    2025-01-01
  • mybatis初始化SqlSessionFactory失敗的幾個(gè)原因分析

    mybatis初始化SqlSessionFactory失敗的幾個(gè)原因分析

    這篇文章主要介紹了mybatis初始化SqlSessionFactory失敗的幾個(gè)原因分析,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-12-12
  • Spring實(shí)現(xiàn)處理跨域請(qǐng)求代碼詳解

    Spring實(shí)現(xiàn)處理跨域請(qǐng)求代碼詳解

    這篇文章主要介紹了Spring實(shí)現(xiàn)處理跨域請(qǐng)求代碼詳解,具有一定借鑒價(jià)值,需要的朋友可以了解下。
    2017-12-12

最新評(píng)論

安溪县| 延长县| 宝丰县| 宽城| 建始县| 偃师市| 鄂伦春自治旗| 通化县| 彩票| 安丘市| 阿勒泰市| 九龙坡区| 古交市| 揭东县| 客服| 遂溪县| 凤阳县| 千阳县| 德清县| 犍为县| 鄱阳县| 天水市| 海安县| 石景山区| 河池市| 普洱| 磐石市| 阿鲁科尔沁旗| 时尚| 福泉市| 黎川县| 扶沟县| 呼图壁县| 泸水县| 乐至县| 康定县| 沿河| 通许县| 德令哈市| 梧州市| 且末县|