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

Java concurrency線程池之線程池原理(四)_動力節(jié)點Java學院整理

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

拒絕策略介紹

線程池的拒絕策略,是指當任務添加到線程池中被拒絕,而采取的處理措施。

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

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

  1. AbortPolicy         -- 當任務添加到線程池中被拒絕時,它將拋出 RejectedExecutionException 異常。
  2. CallerRunsPolicy    -- 當任務添加到線程池中被拒絕時,會在線程池當前正在運行的Thread線程池中處理被拒絕的任務。
  3. DiscardOldestPolicy -- 當任務添加到線程池中被拒絕時,線程池會放棄等待隊列中最舊的未處理任務,然后將被拒絕的任務添加到等待隊列中。
  4. DiscardPolicy       -- 當任務添加到線程池中被拒絕時,線程池將丟棄被拒絕的任務。

線程池默認的處理策略是AbortPolicy!

拒絕策略對比和示例

下面通過示例,分別演示線程池的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),"線程池"的阻塞隊列容量為1(CAPACITY)。
     ThreadPoolExecutor pool = new ThreadPoolExecutor(THREADS_SIZE, THREADS_SIZE, 0, TimeUnit.SECONDS,
         new ArrayBlockingQueue<Runnable>(CAPACITY));
     // 設(shè)置線程池的拒絕策略為"丟棄"
     pool.setRejectedExecutionHandler(new ThreadPoolExecutor.DiscardPolicy());
 
     // 新建10個任務,并將它們添加到線程池中。
     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();
     }
   }
 }

運行結(jié)果:

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

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

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

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

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),"線程池"的阻塞隊列容量為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個任務,并將它們添加到線程池中。
     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();
     }
   }
 }

運行結(jié)果:

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

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

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),"線程池"的阻塞隊列容量為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個任務,并將它們添加到線程池中。
       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();
     }
   }
 }

(某一次)運行結(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é)果說明:將"線程池的拒絕策略"由DiscardPolicy修改為AbortPolicy之后,當有任務添加到線程池被拒絕時,會拋出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),"線程池"的阻塞隊列容量為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個任務,并將它們添加到線程池中。
     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();
     }
   }
 }

(某一次)運行結(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é)果說明:將"線程池的拒絕策略"由DiscardPolicy修改為CallerRunsPolicy之后,當有任務添加到線程池被拒絕時,線程池會將被拒絕的任務添加到"線程池正在運行的線程"中取運行

相關(guān)文章

最新評論