池化思想集大成者------线程池

Java中有很多池化思想的技术,比如数据库连接池,HttpClient 连接池,内存池,线程池,而我们今天的主题就是线程池。有些文章我看到讲的很大白话,生动形象,但是我尽量讲的专业一点,准确一点。开始我们的正文吧。

Java中提供了几种线程池,我们根据具体的应用场景可以自行去选择,但是我们大多数时候都是自己去定义线程池,接下来本篇文章的主角来了: ThreadPoolExecutor

我们就先从ThreadPoolExecutor的构造方法说起吧:源码如下



   
    public ThreadPoolExecutor(int corePoolSize,
                              int maximumPoolSize,
                              long keepAliveTime,
                              TimeUnit unit,
                              BlockingQueue<Runnable> workQueue) {
        this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue,
             Executors.defaultThreadFactory(), defaultHandler);
    }

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

    public ThreadPoolExecutor(int corePoolSize,
                              int maximumPoolSize,
                              long keepAliveTime,
                              TimeUnit unit,
                              BlockingQueue<Runnable> workQueue,
                              RejectedExecutionHandler handler) {
        this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue,
             Executors.defaultThreadFactory(), handler);
    }

    /**
     * Creates a new {@code ThreadPoolExecutor} with the given initial
     * parameters.
     *
     * @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.
     * @param threadFactory the factory to use when the executor
     *        creates a new thread
     * @param handler the handler to use when execution is blocked
     *        because the thread bounds and queue capacities are reached
     * @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}
     *         or {@code threadFactory} or {@code handler} is null
     */
    public ThreadPoolExecutor(int corePoolSize,
                              int maximumPoolSize,
                              long keepAliveTime,
                              TimeUnit unit,
                              BlockingQueue<Runnable> workQueue,
                              ThreadFactory threadFactory,
                              RejectedExecutionHandler handler) {
        if (corePoolSize < 0 ||
            maximumPoolSize <= 0 ||
            maximumPoolSize < corePoolSize ||
            keepAliveTime < 0)
            throw new IllegalArgumentException();
        if (workQueue == null || threadFactory == null || handler == null)
            throw new NullPointerException();
        this.corePoolSize = corePoolSize;
        this.maximumPoolSize = maximumPoolSize;
        this.workQueue = workQueue;
        this.keepAliveTime = unit.toNanos(keepAliveTime);
        this.threadFactory = threadFactory;
        this.handler = handler;
    }

通过以上源码我们可以看到一共有4个构造方法,涉及到的参数总共有7个,接下来我们就对这7个参数来进行解释,其实源码上面已经解释的很好了,我只是将英文翻译成中文罢了。

   /** @param corePoolSize(核心线程数) 池中保留的线程数,即使它们是空闲的,
       除非设置了allowCoreThreadTimeOut
     * @param maximumPoolSize 池中允许的最大线程数
     * @param  当线程数量大于核心时,这是多余空闲线程在终止前等待新任务的最大时间。
     * @param unit {@code keepAliveTime}参数的时间单位
     * @param workQueue 工作队列在执行任务之前用于保存任务的队列。此队列将只保存由{@code execute}方法提交的{@code Runnable}任务。
     * @param threadFactory  执行程序创建新线程时要使用的工厂
     * @param handler 当执行因达到线程边界和队列容量而阻塞时使用的处理程序
     * /

可能有的小伙伴觉得这样的解释不是那么容易理解,还是比较习惯我们平时说的:核心线程数,最大线程数,非核心线程的最大空闲时间,阻塞队列,线程工厂,拒绝策略这样的几个名词,其实这也是完全OK的,我这里只是为了和原作者保持一致,直接翻译的。以下内容为了便于理解,我还是使用这几个名词来进行说明。

好了,接下来我们就看看线程池是如何工作的吧:
以下图是线程池的工作原理图:
在这里插入图片描述

虽然上图已经很详细的说明了线程池的工作流程,但是为了更加说明非非是个勤快并且负责人的同学,还是使用文字再给小伙伴们叙述一遍吧:

  • 在主线程中执行excute()方法,将task提交给线程池
  • 如果当前线程池中保留的线程数小于核心线程数,那么就创建核心线程去执行task
  • 如果当前线程池中保留的线程大于核心线程数,那么就将task放到BlockingQueue中,然后核心线程从BlockingQueue中获取task再去执行。
  • 如果BlockingQueue已经放满了,并且线程池中保留的线程数小于最大线程数,那么就创建非核心线程去执行task
  • 如果线程池中保留的线程数已经达到了最大线程数,那么此时线城池就不能在继续处理task了,则执行拒绝策略

Talk is cheap,show me the code!
我们来看execute()方法,与上面的图进行对比:

 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();
        
        
        //步骤1:如果线程池的线程数小于核心线程数  那么就添加一个线程执行Task,对应上图的步骤1
        if (workerCountOf(c) < corePoolSize) {
            if (addWorker(command, true))
                return;
            c = ctl.get();
        }
        
        //步骤2:将Task加入阻塞队列    对应上图的步骤2     
        if (isRunning(c) && workQueue.offer(command)) {
            int recheck = ctl.get();
            //下面的2个判断是说,如果在上一行判断的时候,这个时候线程池被关闭了,那么需要将加入到阻塞队列里面的任务拿出来(回滚),然后执行拒绝策略
            if (! isRunning(recheck) && remove(command))
                reject(command);
              //  开始存在的线程全部都死掉了,那么创建一个线程,让他能够在阻塞队列中将加入的任务获取出来
            else if (workerCountOf(recheck) == 0)
                addWorker(null, false);
        }
        //如果不能放入任务队列,那么我们尝试添加一个新的线程(非核心线程)去执行任务。如果添加失败了,我们就执行拒绝策略。添加失败的条件就是:线程池中的线程数已经达到了最大线程数
        //步骤3:  对应上图的步骤3
        else if (!addWorker(command, false))
            //步骤4:  对应上图的步骤4
            reject(command);
    }

我们可以看到作者在上面写了很大的一段注释,整个线程池的工作原理一共分为3个步骤,再次细分的话可以拆成上图的4个步骤。

接下来我们再来看看addWorker()方法到底是怎么执行的吧,源码如下:
addWorker()方法里面的代码挺多的,但是我们只看最关键的代码,其余的代码有兴趣的小伙伴可以去研究下。

 //我们看这个core参数,如果是true就代表创建的是核心线程,如果是false就代表创建的是非核心线程
private boolean addWorker(Runnable firstTask, boolean core) {
        retry:
        for (;;) {
            int c = ctl.get();
            int rs = runStateOf(c);

            // Check if queue empty only if necessary.
            if (rs >= SHUTDOWN &&
                ! (rs == SHUTDOWN &&
                   firstTask == null &&
                   ! workQueue.isEmpty()))
                return false;

            for (;;) {
                int wc = workerCountOf(c);
                //我们重点关注的是以下这个if条件代码   创建线程是否成功就是看这一行代码的
                //如果core为true,那么判断当前线程数是否大于核心线程数,如果大于则创建失败返回fasle
                //人如果core为fasle,那么判断当前线程个数是否大于最大线程数,如果大于则创建失败返回false
                if (wc >= CAPACITY ||
                    wc >= (core ? corePoolSize : maximumPoolSize))
                    return false;
                if (compareAndIncrementWorkerCount(c))
                    break retry;
                c = ctl.get();  // Re-read ctl
                if (runStateOf(c) != rs)
                    continue retry;
                // else CAS failed due to workerCount change; retry inner loop
            }
        }

        boolean workerStarted = false;
        boolean workerAdded = false;
        Worker w = null;
        try {
           //这个代码也是需要重点关注的:
           //创建线程的时候创建一个Worker对象,然后通过这个对象获取到一个线程。
            w = new Worker(firstTask);
            final Thread t = w.thread;
            if (t != null) {
                final ReentrantLock mainLock = this.mainLock;
                mainLock.lock();
                try {
                    // Recheck while holding lock.
                    // Back out on ThreadFactory failure or if
                    // shut down before lock acquired.
                    int rs = runStateOf(ctl.get());

                    if (rs < SHUTDOWN ||
                        (rs == SHUTDOWN && firstTask == null)) {
                        if (t.isAlive()) // precheck that t is startable
                            throw new IllegalThreadStateException();
                        workers.add(w);
                        int s = workers.size();
                        if (s > largestPoolSize)
                            largestPoolSize = s;
                        workerAdded = true;
                    }
                } finally {
                    mainLock.unlock();
                }
                if (workerAdded) {
                   //在这里,我们将获取到的线程开启
                    t.start();
                    workerStarted = true;
                }
            }
        } finally {
            if (! workerStarted)
                addWorkerFailed(w);
        }
        return workerStarted;
    }

我们再来看看Worker里面到底是怎么实现的:

//Worker这个类实现了Runnable接口,那么就需要重写run方法
 Worker(Runnable firstTask) {
            setState(-1); // inhibit interrupts until runWorker
            this.firstTask = firstTask;
            this.thread = getThreadFactory().newThread(this);
        }


 /** Delegates main run loop to outer runWorker  */
        public void run() {
            runWorker(this);
        }


final void runWorker(Worker w) {
        Thread wt = Thread.currentThread();
        Runnable task = w.firstTask;
        w.firstTask = null;
        w.unlock(); // allow interrupts
        boolean completedAbruptly = true;
        try {
            //线程循环调用阻塞队列里面的任务,然后执行,只要队列里面有任务,poll方法不返回空,就会一直执行
            while (task != null || (task = getTask()) != null) {
                w.lock();
                // If pool is stopping, ensure thread is interrupted;
                // if not, ensure thread is not interrupted.  This
                // requires a recheck in second case to deal with
                // shutdownNow race while clearing interrupt
                if ((runStateAtLeast(ctl.get(), STOP) ||
                     (Thread.interrupted() &&
                      runStateAtLeast(ctl.get(), STOP))) &&
                    !wt.isInterrupted())
                    wt.interrupt();
                try {
                    beforeExecute(wt, task);
                    Throwable thrown = null;
                    try {
                    //执行task
                        task.run();
                    } catch (RuntimeException x) {
                        thrown = x; throw x;
                    } catch (Error x) {
                        thrown = x; throw x;
                    } catch (Throwable x) {
                        thrown = x; throw new Error(x);
                    } finally {
                        afterExecute(task, thrown);
                    }
                } finally {
                    task = null;
                    w.completedTasks++;
                    w.unlock();
                }
            }
            completedAbruptly = false;
        } finally {
            processWorkerExit(w, completedAbruptly);
        }
    }

通过以上的几个方法的说明,我们应该基本能明白线程池的处理流程与逻辑了,如果有写的不对的地方,欢迎各位小伙伴指正。

下面来说一下拒绝策略:

以下是四种拒绝策略的源码:

 public static class CallerRunsPolicy implements RejectedExecutionHandler {       
        public CallerRunsPolicy() { }      
         //如果线程池没有关闭,那么再次去执行该任务
        public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
            if (!e.isShutdown()) {
                r.run();
            }
        }
    }

   //默认的拒绝策略
    public static class AbortPolicy implements RejectedExecutionHandler {     
        public AbortPolicy() { }
         //直接抛出异常信息
        public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
            throw new RejectedExecutionException("Task " + r.toString() +
                                                 " rejected from " +
                                                 e.toString());
        }
    }
   
    public static class DiscardPolicy implements RejectedExecutionHandler {      
        public DiscardPolicy() { }
         //什么也不做,将Task丢掉了
        public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
        }
    }
  
    public static class DiscardOldestPolicy implements RejectedExecutionHandler {  
        public DiscardOldestPolicy() { }
         // 如果线程池没有关闭,那么从BlockingQueue的尾部移除一个任务,通俗的说就是将最久未执行的Task丢掉,然后再次去执行新的Task
        public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
            if (!e.isShutdown()) {
                e.getQueue().poll();
                e.execute(r);
            }
        }

说了拒绝策略,我们再来说一说BlockingQueue:
在线程池中常用的有2中BlockingQueue,分别是:

  • ArrayBlockingQueue:
    使用数组实现的BlockingQueue,需要指定容量,有界队列。

  • LinkedBlockingQueue:
    使用链表实现的,无界队列,也不是真的无界队列,只是大小为Integer.MAX_VALUW。

java里面还有其他的很多阻塞队列,这里只简单的介绍2种线程池最常用的,后面有机会出一篇文章专门介绍阻塞队列。

好了此次的分享内容就到此结束了,写这篇文章大概断断续续写了一周左右,实习生也是很忙的,哈哈。希望大家都能在秋招找到好的工作。

作者感言:
大家在看我的讲知识点的文章的时候会发现,作者其实自己说的比较少,都是讲源码占据很大一部分,一直都是围绕源码在讲,我听过一个老师讲过的课,他说在讲java知识原理的时候,任何人在没有拿出证据之前,说的话都是不值得相信的。在业界有一句话:talk is cheap,show me the code!!!,非非我深以为然,我不喜欢空口说白话,难免有误人子弟的嫌疑,我看了网络上很多的文章,很多都是在空口说白话,我们应该用证据说话,用数据说话。当然作为面试之前背面经,这些完全是ok的,但是从治学的严谨态度来讲,作者还是比较认同:talk is cheap,show me the code!!!。非非个人观点,不喜勿喷。

码字不易,如果觉得好,请不要吝啬你们的大拇指,点个赞,嘿嘿嘿。!!! 你们的给力,就是我的动力,我是爱生活的袁非非!!!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值