zoukankan      html  css  js  c++  java
  • java并发:线程池之饱和策略

    一、饱和策略(线程池任务拒绝策略)

    ThreadPoolExecutor构造函数的RejectedExecutionHandler handler参数表示当提交的任务数超过maxmumPoolSize与workQueue之和时,任务会交给RejectedExecutionHandler来处理,此处我们来具体了解一下

    二、源码分析

    (1)ThreadPoolExecutor中默认的饱和策略定义如下:

        /**
         * The default rejected execution handler.
         */
        private static final RejectedExecutionHandler defaultHandler =
            new AbortPolicy();

    (2)ThreadPoolExecutor中获取、设置饱和策略的方法如下:

        /**
         * Sets a new handler for unexecutable tasks.
         *
         * @param handler the new handler
         * @throws NullPointerException if handler is null
         * @see #getRejectedExecutionHandler
         */
        public void setRejectedExecutionHandler(RejectedExecutionHandler handler) {
            if (handler == null)
                throw new NullPointerException();
            this.handler = handler;
        }
    
        /**
         * Returns the current handler for unexecutable tasks.
         *
         * @return the current handler
         * @see #setRejectedExecutionHandler(RejectedExecutionHandler)
         */
        public RejectedExecutionHandler getRejectedExecutionHandler() {
            return handler;
        }

    (3)RejectedExecutionHandler接口

    RejectedExecutionHandler的定义如下:

    public interface RejectedExecutionHandler{
        //被线程池丢弃的线程处理机制
        public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) ;
    }

    (4)AbortPolicy

    此策略继承RejectedExecutionHandler接口,其源码如下:

    public static class AbortPolicy implements RejectedExecutionHandler{
    
        public AbortPolicy(){}
        
        //直接抛出异常
        public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
            throw new RejectedExecutionException("Task"+r.toString()+"rejected from"+executor.toString());
        }
        
    }

    (5)自定义饱和策略

    实现RejectedExecutionHandler接口,代码如下:

    package com.test;
    
    import java.util.concurrent.RejectedExecutionHandler;
    import java.util.concurrent.ThreadPoolExecutor;
    
    public class RejectedExecutionHandlerDemo implements RejectedExecutionHandler{
    
        @Override
        public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
            // TODO Auto-generated method stub
            System.out.println("线程信息"+r.toString()+"被遗弃的线程池:"+executor.toString());
        }
        
    }

    Note:

    针对线程池使用 FutureTask 时,如果饱和策略设置为 DiscardPolicy 和 DiscardOldestPolicy,并且在被拒绝的任务的 Future对象上调用了无参 get方法,那么调用线程会一直被阻塞。

  • 相关阅读:
    Android Studio 2.3.1导出jar文件不能生成release解决办法
    AndroidStudio 3.0 生成jar包的方法
    Android Studio如何打jar包
    Android Studio 如何打JAR包(修订版)
    6款程序员必备的开源中文处理工具
    Qt5.8 下链接 Mysql 错误以及解决方法(无论 Mysql 是什么版本的,64 位 Qt 要用 64 位的 Mysql 驱动,32 位的 Qt 要用 32 位的Mysql 驱动)
    Go 语言如果按这样改进,能不能火过 Java?
    基于 CSP 的设计思想和 OOP 设计思想的异同
    DELPHI下多线程编程的几个思维误区(QDAC)
    如何使用表单
  • 原文地址:https://www.cnblogs.com/studyLog-share/p/15150872.html
Copyright © 2011-2022 走看看