线程池(三):线程池排队机制

90 阅读4分钟

1、为什么JDK1.5引入线程池

在Java中,如果每个任务都创建一个新的thread,开销是非常大的。除了创建和销毁线程的时间开销外,还消耗大量的系统资源。为了规避以上问题,尽可能减少创建和销毁线程的次数,特别是一些资源耗费比较大的线程的创建和销毁,尽量利用已有的线程对象来进行服务,这就是线程池引入的原因。

2、ThreadPoolExecutor类分析

(a)ThreadPoolExecutor构造函数:

public ThreadPoolExecutor(int corePoolSize,

int maximumPoolSize,

long keepAliveTime,

TimeUnit unit,

BlockingQueue workQueue,

ThreadFactory threadFactory,

RejectedExecutionHandler handler);

1)corePoolSize 核心线程数

(2)maximumPoolSize 最大线程数

(3)keepAliveTime 线程被回收前的空闲时间

(4)workQueue 任务队列

(5)threadFactory 线程创建工厂

(6)handler 线程池对拒绝任务的处理策略

(b)ThreadPoolExecutor运行原理:

ThreadPoolExecutor将根据corePoolSize和maximumPoolSize设置的值调整线程池大小。当新任务调用方法execute(Runnable)提交时,如果运行的线程少于corePoolSize,则创建新线程来处理请求。如果正在运行的线程数等于corePoolSize时,则新任务被添加到队列中,直到队列满。当队列满了后,会继续开辟新线程来处理任务,但不超过最大线程数。当任务队列满了并且已开辟了最大线程数,此时又来了新任务,ThreadPoolExecutor会拒绝服务。

(c)keepAliveTime注意事项:

/**

* Timeout in nanoseconds for idle threads waiting for work.

* Threads use this timeout when there are more than corePoolSize

* present or if allowCoreThreadTimeOut. Otherwise they wait

* forever for new work.

*/

private volatile long keepAliveTime;

说明:当线程空闲超过keepAliveTime,非核心线程会被回收,若allowCoreThreadTimeOut为true则核心线程也会被回收。

/**

* If false (default), core threads stay alive even when idle.

* If true, core threads use keepAliveTime to time out waiting

* for work.

*/

private volatile boolean allowCoreThreadTimeOut;

说明:默认情况下,核心线程不会被回收;当allowCoreThreadTimeOut为true,核心线程也会被回收。

3、任务队列

BlockingQueue workQueue;

workQueue是一个BlockingQueue接口的对象,仅用于存放Runnable对象。

(1)直接提交(如SynchronousQueue)

直接提交策略表示线程池不对任务进行缓存。新进任务直接提交给线程池,当线程池中没有空闲线程时,创建一个新的线程处理此任务。这种策略需要线程池具有无限增长的可能性。

Executors.newCachedThreadPool()使用SynchronousQueue创建线程池。

(2)无界队列(如不具有预定义容量的LinkedBlockingQueue)

LinkedBlockingQueue将导致当所有 corePoolSize 线程都忙时新任务在队列中等待。这样,创建的线程就不会超过 corePoolSize。(因此,maximumPoolSize 的值也就无效了。)当每个任务完全独立于其他任务,即任务执行互不影响时,适合于使用无界队列。

Executors.newFixedThreadPool(3)使用LinkedBlockingQueue创建线程池。

Executors.newSingleThreadExecutor()使用LinkedBlockingQueue创建线程池。

(3)有界队列(如ArrayBlockingQueue)

有界队列(如ArrayBlockingQueue)有助于防止资源耗尽当最大线程数有限时,但是可能较难调整和控制。队列大小和最大池大小可能需要相互折衷。

(4)优先级队列(如PriorityBlockingQueue)

(5)DelayedWorkQueue

DelayedWorkQueue是ScheduledThreadPoolExecutor的静态内部类。

Executors.newScheduledThreadPool(3)使用DelayedWorkQueue创建线程池。

4、拒绝策略

接口RejectedExecutionHandler提供了拒绝任务处理的自定义方法的机会。在ThreadPoolExecutor中已经包含四种拒绝策略。

(1)AbortPolicy

拒绝策略:抛出运行时异常RejectedExecutionException。

这种策略丢弃任务,并抛出异常。(jdk默认策略)

(2)DiscardPolicy

拒绝策略:不能执行的任务将被丢弃。

这种策略什么都没做。

(3)DiscardOldestPolicy

拒绝策略:如果执行程序尚未关闭,则位于工作队列头部的任务将被删除,然后重试执行程序。

public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {

    if (!e.isShutdown()) {

        e.getQueue().poll();

        e.execute(r);

    }

}

该策略稍微复杂一些,在pool没有关闭的前提下首先丢掉缓存在队列中的最早的任务,然后重新尝试运行该任务。

(4)CallerRunsPolicy

拒绝策略:线程调用运行该任务的 execute 本身。此策略提供简单的反馈控制机制,能够减缓新任务的提交速度。

public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {

    if (!e.isShutdown()) {

        r.run();

    }

}

这个策略不想放弃执行任务。但是由于池中已经没有任何资源了,那么就直接使用调用该execute的线程本身来执行。