欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

拒絕策略介紹

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

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

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

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

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

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

下面通過(guò)示例,分別演示線(xiàn)程池的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)建線(xiàn)程池。線(xiàn)程池的"最大池大小"和"核心池大小"都為1(THREADS_SIZE),"線(xiàn)程池"的阻塞隊(duì)列容量為1(CAPACITY)。
     ThreadPoolExecutor pool = new ThreadPoolExecutor(THREADS_SIZE, THREADS_SIZE, 0, TimeUnit.SECONDS,
         new ArrayBlockingQueue<Runnable>(CAPACITY));
     // 設(shè)置線(xiàn)程池的拒絕策略為"丟棄"
     pool.setRejectedExecutionHandler(new ThreadPoolExecutor.DiscardPolicy());
 
     // 新建10個(gè)任務(wù),并將它們添加到線(xiàn)程池中。
     for (int i = 0; i < 10; i++) {
       Runnable myrun = new MyRunnable("task-"+i);
       pool.execute(myrun);
     }
     // 關(guān)閉線(xià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ō)明:線(xiàn)程池pool的"最大池大小"和"核心池大小"都為1(THREADS_SIZE),這意味著"線(xiàn)程池能同時(shí)運(yùn)行的任務(wù)數(shù)量最大只能是1"。

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

根據(jù)""中分析的execute()代碼可知:線(xiàn)程池中共運(yùn)行了2個(gè)任務(wù)。第1個(gè)任務(wù)直接放到Worker中,通過(guò)線(xiàn)程去執(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)建線(xiàn)程池。線(xiàn)程池的"最大池大小"和"核心池大小"都為1(THREADS_SIZE),"線(xiàn)程池"的阻塞隊(duì)列容量為1(CAPACITY)。
     ThreadPoolExecutor pool = new ThreadPoolExecutor(THREADS_SIZE, THREADS_SIZE, 0, TimeUnit.SECONDS,
         new ArrayBlockingQueue<Runnable>(CAPACITY));
     // 設(shè)置線(xiàn)程池的拒絕策略為"DiscardOldestPolicy"
     pool.setRejectedExecutionHandler(new ThreadPoolExecutor.DiscardOldestPolicy());
 
     // 新建10個(gè)任務(wù),并將它們添加到線(xiàn)程池中。
     for (int i = 0; i < 10; i++) {
       Runnable myrun = new MyRunnable("task-"+i);
       pool.execute(myrun);
     }
     // 關(guān)閉線(xià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ō)明:將"線(xiàn)程池的拒絕策略"由DiscardPolicy修改為DiscardOldestPolicy之后,當(dāng)有任務(wù)添加到線(xiàn)程池被拒絕時(shí),線(xiàn)程池會(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)建線(xiàn)程池。線(xiàn)程池的"最大池大小"和"核心池大小"都為1(THREADS_SIZE),"線(xiàn)程池"的阻塞隊(duì)列容量為1(CAPACITY)。
     ThreadPoolExecutor pool = new ThreadPoolExecutor(THREADS_SIZE, THREADS_SIZE, 0, TimeUnit.SECONDS,
         new ArrayBlockingQueue<Runnable>(CAPACITY));
     // 設(shè)置線(xiàn)程池的拒絕策略為"拋出異常"
     pool.setRejectedExecutionHandler(new ThreadPoolExecutor.AbortPolicy());
 
     try {
 
       // 新建10個(gè)任務(wù),并將它們添加到線(xiàn)程池中。
       for (int i = 0; i < 10; i++) {
         Runnable myrun = new MyRunnable("task-"+i);
         pool.execute(myrun);
       }
     } catch (RejectedExecutionException e) {
       e.printStackTrace();
       // 關(guān)閉線(xià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ō)明:將"線(xiàn)程池的拒絕策略"由DiscardPolicy修改為AbortPolicy之后,當(dāng)有任務(wù)添加到線(xiàn)程池被拒絕時(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)建線(xiàn)程池。線(xiàn)程池的"最大池大小"和"核心池大小"都為1(THREADS_SIZE),"線(xiàn)程池"的阻塞隊(duì)列容量為1(CAPACITY)。
     ThreadPoolExecutor pool = new ThreadPoolExecutor(THREADS_SIZE, THREADS_SIZE, 0, TimeUnit.SECONDS,
         new ArrayBlockingQueue<Runnable>(CAPACITY));
     // 設(shè)置線(xiàn)程池的拒絕策略為"CallerRunsPolicy"
     pool.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
 
     // 新建10個(gè)任務(wù),并將它們添加到線(xiàn)程池中。
     for (int i = 0; i < 10; i++) {
       Runnable myrun = new MyRunnable("task-"+i);
       pool.execute(myrun);
     }
 
     // 關(guān)閉線(xià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ō)明:將"線(xiàn)程池的拒絕策略"由DiscardPolicy修改為CallerRunsPolicy之后,當(dāng)有任務(wù)添加到線(xiàn)程池被拒絕時(shí),線(xiàn)程池會(huì)將被拒絕的任務(wù)添加到"線(xiàn)程池正在運(yùn)行的線(xiàn)程"中取運(yùn)行

相關(guān)文章

最新評(píng)論