美文网首页JUC并发包
ThreadPoolExecutor源码历险-拒绝策略静态内部类

ThreadPoolExecutor源码历险-拒绝策略静态内部类

作者: 于情于你 | 来源:发表于2021-01-19 13:44 被阅读0次

    jdk提供的线程池拒绝策略都实现了同一个接口:RejectedExecutionHandler

    image.png

    DiscardPolicy 丢弃任务

     public static class DiscardPolicy implements RejectedExecutionHandler {
            /**
             * Creates a {@code DiscardPolicy}.
             */
            public DiscardPolicy() { }
    
            /**
              * 什么也不做就是丢弃任务了
             * Does nothing, which has the effect of discarding task r.
             *
             * @param r the runnable task requested to be executed
             * @param e the executor attempting to execute this task
             */
            public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
            }
        }
    

    CallerRunsPolicy 由调用exectue方法的线程处理该任务

     public static class CallerRunsPolicy implements RejectedExecutionHandler {
            /**
             * Creates a {@code CallerRunsPolicy}.
             */
            public CallerRunsPolicy() { }
    
            /**
             * Executes task r in the caller's thread, unless the executor
             * has been shut down, in which case the task is discarded.
             *
             * @param r the runnable task requested to be executed
             * @param e the executor attempting to execute this task
             */
            public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
                if (!e.isShutdown()) {
                    r.run();
                }
            }
        }
    

    DiscardOldestPolicy 丢弃队头任务(最老的那个),重新尝试执行任务

    public static class DiscardOldestPolicy implements RejectedExecutionHandler {
           /**
            * Creates a {@code DiscardOldestPolicy} for the given executor.
            */
           public DiscardOldestPolicy() { }
    
           /**
            * Obtains and ignores the next task that the executor
            * would otherwise execute, if one is immediately available,
            * and then retries execution of task r, unless the executor
            * is shut down, in which case task r is instead discarded.
            *
            * @param r the runnable task requested to be executed
            * @param e the executor attempting to execute this task
            */
           public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
               if (!e.isShutdown()) {
                   // 获取工作队列workQueue,并且删除队头任务
                   e.getQueue().poll();
                   // 重新执行任务
                   e.execute(r);
               }
           }
       }
    

    拒绝任务,抛出一个RejectedExecutionException异常

    public static class AbortPolicy implements RejectedExecutionHandler {
           /**
            * Creates an {@code AbortPolicy}.
            */
           public AbortPolicy() { }
    
           /**
            * Always throws RejectedExecutionException.
            *
            * @param r the runnable task requested to be executed
            * @param e the executor attempting to execute this task
            * @throws RejectedExecutionException always
            */
           public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
               throw new RejectedExecutionException("Task " + r.toString() +
                                                    " rejected from " +
                                                    e.toString());
           }
       }
    

    相关文章

      网友评论

        本文标题:ThreadPoolExecutor源码历险-拒绝策略静态内部类

        本文链接:https://www.haomeiwen.com/subject/mmvqzktx.html