浅析ThreadPoolExecutor

来源:互联网 发布:淘宝充话费怎么充 编辑:程序博客网 时间:2024/06/08 04:23

从Java5开始,Java提供了自己的线程池。线程池就是一个线程的容器,每次只执行额定数量的线程。ThreadPoolExecutor就是这样的线程池。

ThreadPoolExecutor有多个构造函数,以下以最简单的为例

/**     * Creates a new {@code ThreadPoolExecutor} with the given initial     * parameters and default thread factory and rejected execution handler.     * It may be more convenient to use one of the {@link Executors} factory     * methods instead of this general purpose constructor.     *     * @param corePoolSize the number of threads to keep in the pool, even     *        if they are idle, unless {@code allowCoreThreadTimeOut} is set     * @param maximumPoolSize the maximum number of threads to allow in the     *        pool     * @param keepAliveTime when the number of threads is greater than     *        the core, this is the maximum time that excess idle threads     *        will wait for new tasks before terminating.     * @param unit the time unit for the {@code keepAliveTime} argument     * @param workQueue the queue to use for holding tasks before they are     *        executed.  This queue will hold only the {@code Runnable}     *        tasks submitted by the {@code execute} method.     * @throws IllegalArgumentException if one of the following holds:<br>     *         {@code corePoolSize < 0}<br>     *         {@code keepAliveTime < 0}<br>     *         {@code maximumPoolSize <= 0}<br>     *         {@code maximumPoolSize < corePoolSize}     * @throws NullPointerException if {@code workQueue} is null     */    public ThreadPoolExecutor(int corePoolSize,                              int maximumPoolSize,                              long keepAliveTime,                              TimeUnit unit,                              BlockingQueue<Runnable> workQueue) {        this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue,             Executors.defaultThreadFactory(), defaultHandler);    }
  • corePoolSize-核心线程池大小
  • maximumPoolSize-线程池的最大大小
  • keepAliveTime-线程数量大于corePoolSize时,线程的存活时间
  • TimeUnit -是一个枚举,表示 keepAliveTime 的单位
  • workQueue-存放任务的队列

这里写图片描述

  • 线程池刚创建时,里面没有一个线程。任务队列作为参数传递。此时,就算队里里面有任务,线程池也不会立即执行
  • 当调用execute()方法添加一个方法时,线程池会做如下判断
    • 如果正在运行的线程数量小于corePoolSize,那么马上创建线程运行这个
    • 如果在运行的线程数量大于或等于corePoolSize,那么这个任务将放到队列里面
    • 如果这个时候队列满了,而且正在运行的线程数量小于maximumPoolSize,那么创建线程运行这个任务
    • 如果队列满了,切正在运行的线程数量大于等于maximumPoolSize,那么就执行拒绝策略
  • 当一个线程完成任务时,它会从队列中取下一个任务来执行
  • 当一个线程无事可做,超过keepAliveTime时,线程池会判断如果当前运行的线程数大约corePoolSize,那么这个线程就会被停掉。所以线程池的所有任务完成后,它会最终收缩到corePoolSize大小

此过程说明,并不是先加入的任务就一定会先执行。假设队列大小为10,corePoolSize为3,maximumPoolSize 为6那么当加入20个任务时.然后任务 4~13 被放入队列。这时候队列满了,任务 14、15、16 会被马上执行,而任务 17~20 则会抛出异常。执行的顺序是这样的:1,2,3,14,15,16,4,5,6,7,8,9,10,11,12,13

以下代码示例

public static void main(String[] args) {        BlockingQueue queue = new LinkedBlockingDeque<>();        //ThreadPoolExecutor executor = new ThreadPoolExecutor(3,6,1, TimeUnit.DAYS,queue);        ThreadPoolExecutor executor = new ThreadPoolExecutor(3,6,1,TimeUnit.DAYS,queue){            @Override            protected void afterExecute(Runnable r, Throwable t){                System.out.println("Task finished.");            }            @Override            protected void terminated(){                System.out.println("shutdown....");            }        };        for (int i=0; i<20; i++){            executor.execute(new Runnable() {                @Override                public void run() {                    try {                        Thread.sleep(1000);                    } catch (InterruptedException e) {                        e.printStackTrace();                    }                    System.out.println(String.format("thread %d finished", this.hashCode()));                }            });        }        executor.shutdown();    }

总结如下:

  • BlockingQueue 只是一个接口,常用的实现类有LinkedBlockingQueue 和 ArrayBlockingQueue。用LinkedBlockingQueue 的好处是在于没有大小限制。这样的话队列不会满,所以execute()不会抛出异常,而且线程池中运行的线程数不会超过corePoolSize个数,keepAliveTime 参数也就没有意义了。
  • shutdown()方法不会阻塞。调用shutdown()方法之后,主线就马上结束了,而是线程池会继续运行直到所有任务执行完才会停止。如果不调用shutdown()方法,那么线程池会一直保存下去,以便随时添加新的任务。

    java.util.concurrent.ThreadPoolExecutor 类提供了丰富的可扩展性。你可以通过创建它的子类来自定义它的行为。

ThreadPoolExecutor executor = new ThreadPoolExecutor(3,6,1,TimeUnit.DAYS,queue){            @Override            protected void afterExecute(Runnable r, Throwable t){                System.out.println("Task finished.");            }            @Override            protected void terminated(){                System.out.println("shutdown....");            }        };

失败策略

除了可以添加任务执行前后的动作之外,ThreadPoolExecutor还允许你自定义当前任务执行失败后的执行策略。可以调用线程池的 setRejectedExecutionHandler() 方法,用自定义的RejectedExecutionHandler对象替换现有的策略。ThreadPoolExecutor提供了4个现有的策略,分别是:

  • ThreadPoolExecutor.AbortPolicy:表示拒绝任务并抛出异常
  • ThreadPoolExecutor.DiscardPolicy:表示拒绝任务但不做任何动作
  • ThreadPoolExecutor.CallerRunsPolicy:表示拒绝任务,并在调用者的线程中直接执行该任务
  • ThreadPoolExecutor.DiscardOldestPolicy:表示先丢弃任务队列中的第一个任务,然后把这个任务加进队列。

    可以通过如下两种方式设置拒绝策略

ThreadPoolExecutor executor = new ThreadPoolExecutor(3,6,1,TimeUnit.DAYS,queue);        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.AbortPolicy());
executor = new ThreadPoolExecutor(3,6,1,TimeUnit.DAYS,queue,new ThreadPoolExecutor.AbortPolicy());

除此之外也可以通过实现RejectedExecutionHandler 来自定义拒绝策略
代码如下

ThreadPoolExecutor executor = new ThreadPoolExecutor(3, 6, 1, TimeUnit.SECONDS, queue,        new RejectedExecutionHandler() {            public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {                System.out.println(String.format("Task %d rejected.", r.hashCode()));            }        });
原创粉丝点击