环境运维

线程池拒绝策略

线程池拒绝策略

当任务过多且线程池的任务队列已满时,此时就会执行线程池的拒绝策略,线程池的拒绝策略默认有以下 4 种:

  1. AbortPolicy:中止策略,线程池会抛出异常并中止执行此任务;

  2. CallerRunsPolicy:把任务交给添加此任务的(main)线程来执行;

  3. DiscardPolicy:忽略此任务,忽略最新的一个任务;

  4. DiscardOldestPolicy:忽略最早的任务,最先加入队列的任务。

默认的拒绝策略为 AbortPolicy 中止策略。

DiscardPolicy拒绝策略

接下来我们以 DiscardPolicy 忽略此任务,忽略最新的一个任务为例,演示一下拒绝策略的具体使用,实现代码如下:


 
  1. public static void main(String[] args) {

  2.     // 任务的具体方法

  3.     Runnable runnable = new Runnable() {

  4.         @Override

  5.         public void run() {

  6.             System.out.println("当前任务被执行,执行时间:" + new Date() +

  7.                                " 执行线程:" + Thread.currentThread().getName());

  8.             try {

  9.                 // 等待 1s

  10.                 TimeUnit.SECONDS.sleep(1);

  11.             } catch (InterruptedException e) {

  12.                 e.printStackTrace();

  13.             }

  14.         }

  15.     };

  16.     // 创建线程,线程的任务队列的长度为 1

  17.     ThreadPoolExecutor threadPool = new ThreadPoolExecutor(1, 1,

  18.                                                            100, TimeUnit.SECONDS, new LinkedBlockingQueue<>(1),

  19.                                                            new ThreadPoolExecutor.DiscardPolicy());

  20.     // 添加并执行 4 个任务

  21.     threadPool.execute(runnable);

  22.     threadPool.execute(runnable);

  23.     threadPool.execute(runnable);

  24.     threadPool.execute(runnable);

  25.     // 线程池执行完任务,关闭线程池

  26.     threadPool.shutdown();

  27. }

以上程序的执行结果如下:02f5d6ab51feb8bfbaed5767dd0be368.png从上述执行结果可以看出,给线程池添加了 4 个任务,而线程池只执行了 2 个任务就结束了,其他两个任务执行了拒绝策略 DiscardPolicy 被忽略了,这就是拒绝策略的作用。

AbortPolicy拒绝策略

为了和 DiscardPolicy 拒绝策略对比,我们来演示一下 JDK 默认的拒绝策略 AbortPolicy 中止策略,线程池会抛出异常并中止执行此任务,示例代码如下:


 
  1. public static void main(String[] args) {

  2.     // 任务的具体方法

  3.     Runnable runnable = new Runnable() {

  4.         @Override

  5.         public void run() {

  6.             System.out.println("当前任务被执行,执行时间:" + new Date() +

  7.                                " 执行线程:" + Thread.currentThread().getName());

  8.             try {

  9.                 // 等待 1s

  10.                 TimeUnit.SECONDS.sleep(1);

  11.             } catch (InterruptedException e) {

  12.                 e.printStackTrace();

  13.             }

  14.         }

  15.     };

  16.     // 创建线程,线程的任务队列的长度为 1

  17.     ThreadPoolExecutor threadPool = new ThreadPoolExecutor(1, 1,

  18.                                                            100, TimeUnit.SECONDS, new LinkedBlockingQueue<>(1),

  19.                                                            new ThreadPoolExecutor.AbortPolicy()); // 显式指定拒绝策略,也可以忽略此设置,它为默认拒绝策略

  20.     // 添加并执行 4 个任务

  21.     threadPool.execute(runnable);

  22.     threadPool.execute(runnable);

  23.     threadPool.execute(runnable);

  24.     threadPool.execute(runnable);

  25.     // 线程池执行完任务,关闭线程池

  26.     threadPool.shutdown();

  27. }

以上程序的执行结果如下:8aeea3023b79e23653d06e28ffac0e37.png从结果可以看出,给线程池添加了 4 个任务,线程池正常执行了 2 个任务,其他两个任务执行了中止策略,并抛出了拒绝执行的异常 RejectedExecutionException。

自定义拒绝策略

当然除了 JDK 提供的四种拒绝策略之外,我们还可以实现通过 new RejectedExecutionHandler,并重写 rejectedExecution 方法来实现自定义拒绝策略,实现代码如下:


 
  1. public static void main(String[] args) {

  2.     // 任务的具体方法

  3.     Runnable runnable = new Runnable() {

  4.         @Override

  5.         public void run() {

  6.             System.out.println("当前任务被执行,执行时间:" + new Date() +

  7.                                " 执行线程:" + Thread.currentThread().getName());

  8.             try {

  9.                 // 等待 1s

  10.                 TimeUnit.SECONDS.sleep(1);

  11.             } catch (InterruptedException e) {

  12.                 e.printStackTrace();

  13.             }

  14.         }

  15.     };

  16.     // 创建线程,线程的任务队列的长度为 1

  17.     ThreadPoolExecutor threadPool = new ThreadPoolExecutor(1, 1,

  18.                                                            100, TimeUnit.SECONDS, new LinkedBlockingQueue<>(1),

  19.                                                            new RejectedExecutionHandler() {

  20.                                                                @Override

  21.                                                                public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {

  22.                                                                    // 执行自定义拒绝策略的相关操作

  23.                                                                    System.out.println("我是自定义拒绝策略~");

  24.                                                                }

  25.                                                            });

  26.     // 添加并执行 4 个任务

  27.     threadPool.execute(runnable);

  28.     threadPool.execute(runnable);

  29.     threadPool.execute(runnable);

  30.     threadPool.execute(runnable);

  31. }

以上程序的执行结果如下:b57e54b1f04aa8eca0f2a0371d9cf2a0.png

总结

线程池的执行流程有 3 个重要的判断点(判断顺序依次往后):判断当前线程数和核心线程数、判断当前任务队列是否已满、判断当前线程数是否已达到最大线程数。如果经过以上 3 个判断,得到的结果都会 true,则会执行线程池的拒绝策略。JDK 提供了 4 种拒绝策略,我们还可以通过 new RejectedExecutionHandler 并重写 rejectedExecution 方法来实现自定义拒绝策略。