JDK线程池原理之四:拒绝策略示例及实现源码

一、拒绝策略介绍

二、拒绝策略对比和示例

三、拒绝策略怎么实现的?

  3.1、线程池中的拒绝策略用的是策略模式
  3.2、 四种拒绝策略的实现,ThreadPoolExecutor的静态内部类
    3.2.1、DiscardPolicy
    3.2.2、DiscardOldestPolicy
    3.2.3、AbortPolicy
    3.2.4、CallerRunsPolicy

四、第三方实现的拒绝策略
  4.1、dubbo中的线程拒绝策略
  4.2、Netty中的线程池拒绝策略
  4.3、activeMq中的线程池拒绝策略
  4.4、pinpoint中的线程池拒绝策略

一、拒绝策略介绍

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

线程池共包括4种拒绝策略,它们分别是:AbortPolicyCallerRunsPolicyDiscardOldestPolicyDiscardPolicy

AbortPolicy         -- 当任务添加到线程池中被拒绝时,它将抛出 RejectedExecutionException 异常。
CallerRunsPolicy    -- 当任务添加到线程池中被拒绝时,那么主线程会自己去执行该任务。"调用者运行"一种调节机制,该策略既不会抛弃任务,也不会抛出异常,而是将某些任务回退到调用者,从而降低新任务的流量。
DiscardOldestPolicy -- 当任务添加到线程池中被拒绝时,线程池会放弃等待队列中最旧的未处理任务,然后将被拒绝的任务添加到等待队列中。
DiscardPolicy       -- 当任务添加到线程池中被拒绝时,线程池将丢弃被拒绝的任务。

线程池默认的处理策略是AbortPolicy!

二、拒绝策略对比和示例

下面通过示例,分别演示线程池的4种拒绝策略。

2.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.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之后,当有任务添加到线程池被拒绝时,线程池会丢弃阻塞队列中末尾的任务,然后将被拒绝的任务添加到末尾。

2.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。

2.4. CallerRunsPolicy 示例

package com.dxz.threadpool;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

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 + Thread.currentThread() + " is running.");
            Thread.sleep(1000);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

(某一次)运行结果

task-2Thread[main,5,main] is running.
task-0Thread[pool-1-thread-1,5,main] is running.
task-3Thread[main,5,main] is running.
task-1Thread[pool-1-thread-1,5,main] is running.
task-5Thread[main,5,main] is running.
task-4Thread[pool-1-thread-1,5,main] is running.
task-7Thread[main,5,main] is running.
task-6Thread[pool-1-thread-1,5,main] is running.
task-9Thread[main,5,main] is running.
task-8Thread[pool-1-thread-1,5,main] is running.
结果说明:将"线程池的拒绝策略"由DiscardPolicy修改为CallerRunsPolicy之后,当有任务添加到线程池被拒绝时,任务会在调用者线程中执行,上面示例的mian线程。

 三、拒绝策略怎么实现的?

3.1、线程池中的拒绝策略用的是策略模式

UML图如下:

RejectedExecutionHandler接口就一个方法void rejectedExecution(Runnable r, ThreadPoolExecutor executor),源码如下:

public interface RejectedExecutionHandler {
    void rejectedExecution(Runnable r, ThreadPoolExecutor executor);
}

3.2、 四种拒绝策略的实现,ThreadPoolExecutor的静态内部类

3.2.1、DiscardPolicy
public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
}

空方法,什么也不做,相当于直接丢弃了任务。

3.2.2、DiscardOldestPolicy 
        public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
            if (!e.isShutdown()) {
                //把队列头结点弹出,腾出队列空间
                e.getQueue().poll();
                //然后通过递归调用,再次尝试提交当前任务
                e.execute(r);
            }
        }

解释:该策略将丢弃最老的一个请求,也就是即将被执行的一个任务,并尝试再次提交当前任务。

(问:为什么是弹出队列的头元素呢?

答:是因为队列的先进先出原则,所以队列的头元素肯定是最老的一个请求,而DiscardOldestPolicy策略就是把最老的请求废除,把这个机会(相当于抢了最老的请求的吃到嘴边的肉)留给当前新提交到线程池里面的线程(这个时候线程池已经满了,队列也已经满了,如果不满的话,也不会有拒绝策略了!))

3.2.3、AbortPolicy 默认的饱和策略
        public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
            throw new RejectedExecutionException("Task " + r.toString() +
                                                 " rejected from " +
                                                 e.toString());
        }

解释:该策略直接抛异常了,异常需要在调用线程中进行捕获。

3.2.4、CallerRunsPolicy 
        public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
            if (!e.isShutdown()) {
                //主线程中运行runnable
                r.run();
            }
        }

解释:既不抛弃任务也不抛出异常,直接运行任务的run方法,换言之将任务回退给调用者来直接运行。使用该策略时线程池饱和后将由调用线程池的主线程自己来执行任务,因此在执行任务的这段时间里主线程无法再提交新任务,从而使线程池中工作线程有时间将正在处理的任务处理完成。

四、第三方实现的拒绝策略

4.1、dubbo中的线程拒绝策略

public class AbortPolicyWithReport extends ThreadPoolExecutor.AbortPolicy {
 
    protected static final Logger logger = LoggerFactory.getLogger(AbortPolicyWithReport.class);
 
    private final String threadName;
 
    private final URL url;
 
    private static volatile long lastPrintTime = 0;
 
    private static Semaphore guard = new Semaphore(1);
 
    public AbortPolicyWithReport(String threadName, URL url) {
        this.threadName = threadName;
        this.url = url;
    }
 
    @Override
    public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
        String msg = String.format("Thread pool is EXHAUSTED!" +
                        " Thread Name: %s, Pool Size: %d (active: %d, core: %d, max: %d, largest: %d), Task: %d (completed: %d)," +
                        " Executor status:(isShutdown:%s, isTerminated:%s, isTerminating:%s), in %s://%s:%d!",
                threadName, e.getPoolSize(), e.getActiveCount(), e.getCorePoolSize(), e.getMaximumPoolSize(), e.getLargestPoolSize(),
                e.getTaskCount(), e.getCompletedTaskCount(), e.isShutdown(), e.isTerminated(), e.isTerminating(),
                url.getProtocol(), url.getIp(), url.getPort());
        logger.warn(msg);
        dumpJStack();
        throw new RejectedExecutionException(msg);
    }
 
    private void dumpJStack() {
       //省略实现
    }
}

解释:可以看到,当dubbo的工作线程触发了线程拒绝后,主要做了三个事情,原则就是尽量让使用者清楚触发线程拒绝策略的真实原因

  • 输出了一条警告级别的日志,日志内容为线程池的详细设置参数,以及线程池当前的状态,还有当前拒绝任务的一些详细信息。可以说,这条日志,使用dubbo的有过生产运维经验的或多或少是见过的,这个日志简直就是日志打印的典范,其他的日志打印的典范还有spring。得益于这么详细的日志,可以很容易定位到问题所在。
  • 输出当前线程堆栈详情,这个太有用了,当你通过上面的日志信息还不能定位问题时,案发现场的dump线程上下文信息就是你发现问题的救命稻草,这个可以参考《dubbo线程池耗尽事件-"CyclicBarrier惹的祸"》
  • 继续抛出拒绝执行异常,使本次任务失败,这个继承了JDK默认拒绝策略的特性

4.2、Netty中的线程池拒绝策略

    private static final class NewThreadRunsPolicy implements RejectedExecutionHandler {
        NewThreadRunsPolicy() {
            super();
        }
 
        public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
            try {
                final Thread t = new Thread(r, "Temporary task executor");
                t.start();
            } catch (Throwable e) {
                throw new RejectedExecutionException(
                        "Failed to start a new thread", e);
            }
        }

解释:netty中的实现很像JDK中的CallerRunsPolicy,舍不得丢弃任务。不同的是,CallerRunsPolicy是直接在调用者线程执行的任务。而 Netty是新建了一个线程来处理的。所以,Netty的实现相较于调用者执行策略的使用面就可以扩展到支持高效率高性能的场景了。但是也要注意一点,Netty的实现里,在创建线程时未做任何的判断约束,也就是说只要系统还有资源就会创建新的线程来处理,直到new不出新的线程了,才会抛创建线程失败的异常。

4.3、activeMq中的线程池拒绝策略

            new RejectedExecutionHandler() {
                @Override
                public void rejectedExecution(final Runnable r, final ThreadPoolExecutor executor) {
                    try {
                        executor.getQueue().offer(r, 60, TimeUnit.SECONDS);
                    } catch (InterruptedException e) {
                        throw new RejectedExecutionException("Interrupted waiting for BrokerService.worker");
                    }
 
                    throw new RejectedExecutionException("Timed Out while attempting to enqueue Task.");
                }
            });

activeMq中的策略属于最大努力执行型策略,当触发拒绝策略时,会再次努力一分钟。重新将任务塞进任务队列,当一分钟超时还没成功时,就抛出异常。

4.4、pinpoint中的线程池拒绝策略

public class RejectedExecutionHandlerChain implements RejectedExecutionHandler {
    private final RejectedExecutionHandler[] handlerChain;
 
    public static RejectedExecutionHandler build(List<RejectedExecutionHandler> chain) {
        Objects.requireNonNull(chain, "handlerChain must not be null");
        RejectedExecutionHandler[] handlerChain = chain.toArray(new RejectedExecutionHandler[0]);
        return new RejectedExecutionHandlerChain(handlerChain);
    }
 
    private RejectedExecutionHandlerChain(RejectedExecutionHandler[] handlerChain) {
        this.handlerChain = Objects.requireNonNull(handlerChain, "handlerChain must not be null");
    }
 
    @Override
    public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
        for (RejectedExecutionHandler rejectedExecutionHandler : handlerChain) {
            rejectedExecutionHandler.rejectedExecution(r, executor);
        }
    }
}

解释:pinpoint的拒绝策略实现很特别,和其他的实现都不同。他定义了一个拒绝策略链,包装了一个拒绝策略列表,当触发拒绝策略时,会将策略链中的rejectedExecution依次执行一遍。

参考:

https://blog.csdn.net/qq_40241957/article/details/85468994

http://www.cnblogs.com/skywang12345/p/3512947.html

https://my.oschina.net/keking/blog/3080826

 

posted on 2013-11-03 21:17  duanxz  阅读(932)  评论(0编辑  收藏  举报