欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Java concurrency线程池之线程池原理(四)_动力节点Java学院整理

程序员文章站 2023-12-18 23:21:04
拒绝策略介绍 线程池的拒绝策略,是指当任务添加到线程池中被拒绝,而采取的处理措施。 当任务添加到线程池中之所以被拒绝,可能是由于:第一,线程池异常关闭。第二,任...

拒绝策略介绍

线程池的拒绝策略,是指当任务添加到线程池中被拒绝,而采取的处理措施。

当任务添加到线程池中之所以被拒绝,可能是由于:第一,线程池异常关闭。第二,任务数量超过线程池的最大限制。

线程池共包括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 {
 
     // 创建线程池。线程池的"最大池大小"和"核心池大小"都为1(threads_size),"线程池"的阻塞队列容量为1(capacity)。
     threadpoolexecutor pool = new threadpoolexecutor(threads_size, threads_size, 0, timeunit.seconds,
         new arrayblockingqueue<runnable>(capacity));
     // 设置线程池的拒绝策略为"丢弃"
     pool.setrejectedexecutionhandler(new threadpoolexecutor.discardpolicy());
 
     // 新建10个任务,并将它们添加到线程池中。
     for (int i = 0; i < 10; i++) {
       runnable myrun = new myrunnable("task-"+i);
       pool.execute(myrun);
     }
     // 关闭线程池
     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();
     }
   }
 }

运行结果:

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

结果说明:线程池pool的"最大池大小"和"核心池大小"都为1(threads_size),这意味着"线程池能同时运行的任务数量最大只能是1"。

线程池pool的阻塞队列是arrayblockingqueue,arrayblockingqueue是一个有界的阻塞队列,arrayblockingqueue的容量为1。这也意味着线程池的阻塞队列只能有一个线程池阻塞等待。

根据""中分析的execute()代码可知:线程池*运行了2个任务。第1个任务直接放到worker中,通过线程去执行;第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 {
 
     // 创建线程池。线程池的"最大池大小"和"核心池大小"都为1(threads_size),"线程池"的阻塞队列容量为1(capacity)。
     threadpoolexecutor pool = new threadpoolexecutor(threads_size, threads_size, 0, timeunit.seconds,
         new arrayblockingqueue<runnable>(capacity));
     // 设置线程池的拒绝策略为"discardoldestpolicy"
     pool.setrejectedexecutionhandler(new threadpoolexecutor.discardoldestpolicy());
 
     // 新建10个任务,并将它们添加到线程池中。
     for (int i = 0; i < 10; i++) {
       runnable myrun = new myrunnable("task-"+i);
       pool.execute(myrun);
     }
     // 关闭线程池
     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();
     }
   }
 }

运行结果:

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

结果说明:将"线程池的拒绝策略"由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 {
 
     // 创建线程池。线程池的"最大池大小"和"核心池大小"都为1(threads_size),"线程池"的阻塞队列容量为1(capacity)。
     threadpoolexecutor pool = new threadpoolexecutor(threads_size, threads_size, 0, timeunit.seconds,
         new arrayblockingqueue<runnable>(capacity));
     // 设置线程池的拒绝策略为"抛出异常"
     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();
       // 关闭线程池
       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();
     }
   }
 }

(某一次)运行结果:

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.

结果说明:将"线程池的拒绝策略"由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 {
 
     // 创建线程池。线程池的"最大池大小"和"核心池大小"都为1(threads_size),"线程池"的阻塞队列容量为1(capacity)。
     threadpoolexecutor pool = new threadpoolexecutor(threads_size, threads_size, 0, timeunit.seconds,
         new arrayblockingqueue<runnable>(capacity));
     // 设置线程池的拒绝策略为"callerrunspolicy"
     pool.setrejectedexecutionhandler(new threadpoolexecutor.callerrunspolicy());
 
     // 新建10个任务,并将它们添加到线程池中。
     for (int i = 0; i < 10; i++) {
       runnable myrun = new myrunnable("task-"+i);
       pool.execute(myrun);
     }
 
     // 关闭线程池
     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();
     }
   }
 }

(某一次)运行结果:

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.

结果说明:将"线程池的拒绝策略"由discardpolicy修改为callerrunspolicy之后,当有任务添加到线程池被拒绝时,线程池会将被拒绝的任务添加到"线程池正在运行的线程"中取运行

上一篇:

下一篇: