java 线程池

参考:《实战java高并发程序设计》 3.2.3 刨根问底:核心线程池的内部是实现 java

java api 提供了几种工厂方法建立线程池 算法

public class ExecutorServiceTest {
    public static void main(String[] args) {
        // 可根据实际状况调整线程数量的线程池
        ExecutorService executorService1 = Executors.newCachedThreadPool();
        //固定大小的线程池
        ExecutorService executorService2 = Executors.newFixedThreadPool(1);
        // 线程池大小为1 
        ExecutorService executorService3 = Executors.newSingleThreadExecutor();
    }
}

查看底层代码的话,发现其均使用了ThreadPoolExecutor实现。api

public static ExecutorService newCachedThreadPool() {
        return new ThreadPoolExecutor(0, Integer.MAX_VALUE,
                                      60L, TimeUnit.SECONDS,
                                      new SynchronousQueue<Runnable>());
    }


    public static ExecutorService newFixedThreadPool(int nThreads) {
        return new ThreadPoolExecutor(nThreads, nThreads,
                                      0L, TimeUnit.MILLISECONDS,
                                      new LinkedBlockingQueue<Runnable>());
    }


 public static ExecutorService newSingleThreadExecutor() {
        return new FinalizableDelegatedExecutorService
            (new ThreadPoolExecutor(1, 1,
                                    0L, TimeUnit.MILLISECONDS,
                                    new LinkedBlockingQueue<Runnable>()));
    }

  

因此好好来看一下ThreadPoolExecutor的代码吧,下面是ThreadPoolExecutor的一个构造函数。并发

   public ThreadPoolExecutor(int corePoolSize,
                              int maximumPoolSize,
                              long keepAliveTime,
                              TimeUnit unit,
                              BlockingQueue<Runnable> workQueue,
                              ThreadFactory threadFactory,
                              RejectedExecutionHandler handler) {}

corePoolSize  :指定了线程中的核心线程数量函数

maximumPoolSize:指定了线程池中的最大线程数量高并发

keepAliveTime:当线程池中线程数量超过corePoolSize时,多余的空闲线程的存活时间。即超过corePoolSize的空闲线程,在多长时间,会被销毁。性能

unit: keepAliveTime 的单位。ui

workQueue:任务队列,被提交但还没有被执行的任务。this

threadFactory:线程工厂,用于建立线程,通常用默认的便可。atom

handler:拒绝策略,当任务太多来不及处理的时候,如何拒绝任务。

参数详解

workQueue 指被提交但未执行的任务队列,它是一个BlockingQueue接口的对象,仅用于存放Runnable对象,如下介绍几种BlockingQueue。

直接提交的队列:SynchronousQueue 对象提供。SynchronousQueue是一个特殊的BlockingQueue。SynchronousQueue没有容量,每个插入操做都要等待一个相应的删除操做,反之,每个删除操做都要等待一个插入操做。若是使用SynchronousQueue,提交的任务不会被真实的保存,而老是将新任务提交给线程执行,若是没有空闲的线程,则尝试建立新的线程,若是线程数量已经达到最大值,则执行拒绝策略。所以,若是使用SynchronousQueue,一般要设置很大的maximumPoolSize值,不然很容易执行拒绝策略。

有界的队列:有界的队列可使用ArrayBlockingQueue实现。ArrayBlockingQueue的构造函数必须带一个容量参数,表示该队列的最大容量。当使用有界队列时,如有新的任务执行,若是线程数量小于corePoolSize ,则优先建立新的线程,若大于corePoolSize,则会将任务加入队列中。若队列已满,没法加入,则在总线程数不大于maximumPoolSize的前提下建立新的线程执行任务,若大于maximumPoolSize时,则执行拒绝策略。可见,有界队列仅当在任务装满时,才可能将线程数量提高到corePoolSize以上。

无界的队列:无界的队列能够经过LinkedBlockingQueue 实现,与有界队列相比,除非系统资源耗尽,不然无界队列不存在任务入队失败的状况,当有新的任务到来,系统的线程数小于corePoolSize时,线程池会生成新的线程执行任务,但当系统的线程数达到corePoolSize后,就不会继续增长。若后续仍有新的任务加入,而又有空闲的线程资源,则任务直接进入队列等待。若任务建立和处理的速度差别很大,误解队列会保持快速增加,直到耗尽系统内存。

优先任务队列:优先队列是带有执行优先队列级的队列,它经过PriorityBlockingQueue实现,能够控制任务执行的前后顺序。它是一个无界队列。不管是有界队列ArrayBlockingQueue,仍是未指定大小的LinkedBlockingQueue都是按照先进先出算法处理任务的。而PriorityBlockQueue则能够根据任务自身的优先级顺序执行,在确保系统性能的同时,也能有很好的质量保证。

 

拒绝策略:

AbortPolicy:该策略会抛出异常,阻止系统正常工做。

CallerRunsPolicy: 只要线程池未关闭,该策略直接在调用者线程中,运行当前被丢弃的任务。显然这样作不会真的丢弃任务,可是,任务提交线程的性能极有可能会急剧降低。

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

DiscardPolicy:该策略默默丢弃没法处理的任务,不予任何处理。若是容许任务丢失,这多是最好的一种处理方案了。

 

ThreadPoolExecutor 线程池的核心调度代码

  public void execute(Runnable command) {
        if (command == null)
            throw new NullPointerException();
        /*
         * Proceed in 3 steps:
         *
         * 1. If fewer than corePoolSize threads are running, try to
         * start a new thread with the given command as its first
         * task.  The call to addWorker atomically checks runState and
         * workerCount, and so prevents false alarms that would add
         * threads when it shouldn't, by returning false.
         *
         * 2. If a task can be successfully queued, then we still need
         * to double-check whether we should have added a thread
         * (because existing ones died since last checking) or that
         * the pool shut down since entry into this method. So we
         * recheck state and if necessary roll back the enqueuing if
         * stopped, or start a new thread if there are none.
         *
         * 3. If we cannot queue task, then we try to add a new
         * thread.  If it fails, we know we are shut down or saturated
         * and so reject the task.
         */
        int c = ctl.get();
        if (workerCountOf(c) < corePoolSize) {
            if (addWorker(command, true))
                return;
            c = ctl.get();
        }
        if (isRunning(c) && workQueue.offer(command)) {
            int recheck = ctl.get();
            if (! isRunning(recheck) && remove(command))
                reject(command);
            else if (workerCountOf(recheck) == 0)
                addWorker(null, false);
        }
        else if (!addWorker(command, false))
            reject(command);
    }

 

是否小于corePoolSize,若是小于,分配线程执行。若是大于corePoolSize,则提交到队列中,若是提交到队列失败,则将任务直接提交给线程池。若是当前线程数已经达到maximumPoolSize,则提交失败,直接拒绝策略。

在这里有一个疑问,当队列已满时,提交的任务直接提交给线程池执行,那么这样的话是不是 后面提交的任务比先提交的任务先执行了吗?

 

阿里巴巴java开发手册强制不容许使用Executors去建立线程池,就是因为Executors建立的线程中对 线程的最大数量或者工做队列的长度设置为Integer.MAX_VALUE,会致使OOM。

相关文章
相关标签/搜索