美文网首页
线程池的原理解析

线程池的原理解析

作者: 辞令 | 来源:发表于2019-04-08 14:06 被阅读0次

    线程池的原理

    上一篇介绍了为什么要使用线程,以及创建线程的的几种方式。接下来将详细介绍线程的实现原理。

    由源码可知,线程池实际上调用的还是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 线程池核心线程数量
         * @param maximumPoolSize 线程池最大线程数量
         * @param keepAliveTime 当活跃线程数大于核心线程数时,空闲的多余线程最大存活时间
         * @param unit. 存活时间的单位
         * @param workQueue 存放任务的队列
         * @param threadFactory 工厂使用执行器时创建一个新线程
         * @param handler 超出线程范围和队列容量的任务的处理程序
         */
        public ThreadPoolExecutor(int corePoolSize,
                                  int maximumPoolSize,
                                  long keepAliveTime,
                                  TimeUnit unit,
                                  BlockingQueue<Runnable> workQueue,
                                  ThreadFactory threadFactory,
                                  RejectedExecutionHandler handler) {
        }
    

    由源码可知,ThreadPoolExecutor实际上调用的是execute方法。下面将看一下execute的源代码和注释解析。

    /**
         * Executes the given task sometime in the future.  The task
         * may execute in a new thread or in an existing pooled thread.
         *
         * If the task cannot be submitted for execution, either because this
         * executor has been shutdown or because its capacity has been reached,
         * the task is handled by the current {@code RejectedExecutionHandler}.
         *
         * @param command 要执行的任务
         */
        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);
        }
    

    由源码可知,execute实际上调用的是addWorker方法。下面将看一下addWorker的源代码和注释解析。
    addWorker的作用:1.才用循环CAS操作来将线程数加1。2. 新建一个线程并启用。

    /**
         * Checks if a new worker can be added with respect to current
         * pool state and the given bound (either core or maximum). If so,
         * the worker count is adjusted accordingly, and, if possible, a
         * new worker is created and started, running firstTask as its
         * first task. This method returns false if the pool is stopped or
         * eligible to shut down. It also returns false if the thread
         * factory fails to create a thread when asked.  If the thread
         * creation fails, either due to the thread factory returning
         * null, or due to an exception (typically OutOfMemoryError in
         * Thread.start()), we roll back cleanly.
         *
         * @param firstTask the task the new thread should run first (or
         * null if none). Workers are created with an initial first task
         * (in method execute()) to bypass queuing when there are fewer
         * than corePoolSize threads (in which case we always start one),
         * or when the queue is full (in which case we must bypass queue).
         * Initially idle threads are usually created via
         * prestartCoreThread or to replace other dying workers.
         *
         * @param core if true use corePoolSize as bound, else
         * maximumPoolSize. (A boolean indicator is used here rather than a
         * value to ensure reads of fresh values after checking other pool
         * state).
         * @return true if successful
         */
        private boolean addWorker(Runnable firstTask, boolean core) {
          //循环CAS操作,将线程池中的线程数+1
            retry:
            for (;;) {
                int c = ctl.get();
                int rs = runStateOf(c);
    
                // 仅在必要时检查队列是否为空
                if (rs >= SHUTDOWN &&
                    ! (rs == SHUTDOWN &&
                       firstTask == null &&
                       ! workQueue.isEmpty()))
                    return false;
    
                for (;;) {
                    int wc = workerCountOf(c);
                    //core true代表是往核心线程池中增加线程 false代表往最大线程池中增加线程
                    //线程数超标,不能再添加了,直接返回
                    if (wc >= CAPACITY ||
                        wc >= (core ? corePoolSize : maximumPoolSize))
                        return false;
                    //CAS修改clt的值+1,在线程池中为将要添加的线程流出空间,成功退出cas循环,失败继续
                    if (compareAndIncrementWorkerCount(c))
                        break retry;
                    c = ctl.get();  // Re-read ctl
                    //如果线程池的状态发生了变化回到retry外层循环
                    if (runStateOf(c) != rs)
                        continue retry;
                    // else CAS failed due to workerCount change; retry inner loop
                }
            }
    
            boolean workerStarted = false;
            boolean workerAdded = false;
            //新建线程,并加入到线程池workers中
            Worker w = null;
            try {
                w = new Worker(firstTask);
                final Thread t = w.thread;
                if (t != null) {
                //对workers操作要通过加锁来实现
                    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();
                          /largestPoolSize的值
                          if (s > largestPoolSize)
                                largestPoolSize = s;
                            workerAdded = true;
                        }
                    } finally {
                        mainLock.unlock();
                    }
                    //线程添加线程池成功,则开启新线程
                    if (workerAdded) {
                        t.start();
                        workerStarted = true;
                    }
                }
            } finally {
                //线程添加线程池失败或者线程start失败,则需要调用addWorkerFailed函数,
                //如果添加成功则需要移除,并回复clt的值
                if (! workerStarted)
                    addWorkerFailed(w);
            }
            return workerStarted;
        }
    

    Worker类继承自AbstractQueuedSynchronizer,具有锁的功能,实现Runable接口,具有线程的功能。

    /**
         * Class Worker mainly maintains interrupt control state for
         * threads running tasks, along with other minor bookkeeping.
         * This class opportunistically extends AbstractQueuedSynchronizer
         * to simplify acquiring and releasing a lock surrounding each
         * task execution.  This protects against interrupts that are
         * intended to wake up a worker thread waiting for a task from
         * instead interrupting a task being run.  We implement a simple
         * non-reentrant mutual exclusion lock rather than use
         * ReentrantLock because we do not want worker tasks to be able to
         * reacquire the lock when they invoke pool control methods like
         * setCorePoolSize.  Additionally, to suppress interrupts until
         * the thread actually starts running tasks, we initialize lock
         * state to a negative value, and clear it upon start (in
         * runWorker).
         */
        private final class Worker
            extends AbstractQueuedSynchronizer
            implements Runnable
        {
            /**
             * This class will never be serialized, but we provide a
             * serialVersionUID to suppress a javac warning.
             */
            private static final long serialVersionUID = 6138294804551838833L;
    
            /** 线程池中正真运行的线程。通过我们指定的线程工厂创建而来, 如果工厂失败,则为空 */
            final Thread thread;
            /** 线程包装的任务。thread 在run时主要调用了该任务的run方法,要运行的初始任务。可能是零*/
            Runnable firstTask;
            /** 线程任务计数器 */
            volatile long completedTasks;
    
            /**
             * Creates with given first task and thread from ThreadFactory.
             * 使用线程工厂,创建第一个任务和线程
             * @param firstTask the first task (null if none)
             */
            Worker(Runnable firstTask) {
                setState(-1); // inhibit interrupts until runWorker
                this.firstTask = firstTask;
                //利用我们指定的线程工厂创建一个线程
                this.thread = getThreadFactory().newThread(this);
            }
    
            /** 将主运行循环委托给runWorker  */
            public void run() {
                runWorker(this);
            }
    
            // Lock methods
            //
            // 值0表示解锁状态。
            // 值1表示锁定状态。
    
            protected boolean isHeldExclusively() {
                return getState() != 0;
            }
    
            protected boolean tryAcquire(int unused) {
                if (compareAndSetState(0, 1)) {
                    setExclusiveOwnerThread(Thread.currentThread());
                    return true;
                }
                return false;
            }
    
            protected boolean tryRelease(int unused) {
                setExclusiveOwnerThread(null);
                setState(0);
                return true;
            }
    
            public void lock()        { acquire(1); }
            public boolean tryLock()  { return tryAcquire(1); }
            public void unlock()      { release(1); }
            public boolean isLocked() { return isHeldExclusively(); }
    
            void interruptIfStarted() {
                Thread t;
                if (getState() >= 0 && (t = thread) != null && !t.isInterrupted()) {
                    try {
                        t.interrupt();
                    } catch (SecurityException ignore) {
                    }
                }
            }
        }
    
    

    由源码可知,Worker类的run方法实际上调用的还是ThreadPoolExecutor的runworker方法。下面将看一下ThreadPoolExecutor的runworker源代码和注释解析。

    /**
         * Main worker run loop.  Repeatedly gets tasks from queue and
         * executes them, while coping with a number of issues:
         *
         * 1. We may start out with an initial task, in which case we
         * don't need to get the first one. Otherwise, as long as pool is
         * running, we get tasks from getTask. If it returns null then the
         * worker exits due to changed pool state or configuration
         * parameters.  Other exits result from exception throws in
         * external code, in which case completedAbruptly holds, which
         * usually leads processWorkerExit to replace this thread.
         *
         * 2. Before running any task, the lock is acquired to prevent
         * other pool interrupts while the task is executing, and then we
         * ensure that unless pool is stopping, this thread does not have
         * its interrupt set.
         *
         * 3. Each task run is preceded by a call to beforeExecute, which
         * might throw an exception, in which case we cause thread to die
         * (breaking loop with completedAbruptly true) without processing
         * the task.
         *
         * 4. Assuming beforeExecute completes normally, we run the task,
         * gathering any of its thrown exceptions to send to afterExecute.
         * We separately handle RuntimeException, Error (both of which the
         * specs guarantee that we trap) and arbitrary Throwables.
         * Because we cannot rethrow Throwables within Runnable.run, we
         * wrap them within Errors on the way out (to the thread's
         * UncaughtExceptionHandler).  Any thrown exception also
         * conservatively causes thread to die.
         *
         * 5. After task.run completes, we call afterExecute, which may
         * also throw an exception, which will also cause thread to
         * die. According to JLS Sec 14.20, this exception is the one that
         * will be in effect even if task.run throws.
         *
         * The net effect of the exception mechanics is that afterExecute
         * and the thread's UncaughtExceptionHandler have as accurate
         * information as we can provide about any problems encountered by
         * user code.
         *
         * @param w the worker
         */
        final void runWorker(Worker w) {
            Thread wt = Thread.currentThread();
            Runnable task = w.firstTask;
            w.firstTask = null;
            w.unlock(); // allow interrupts
            boolean completedAbruptly = true;
            try {
                while (task != null || (task = getTask()) != null) {
                    w.lock();
                    //线程池处于stop状态或者当前线程被中断时,线程池状态是stop状态。
                   //但是当前线程没有中断,则发出中断请求
                    if ((runStateAtLeast(ctl.get(), STOP) ||
                         (Thread.interrupted() &&
                          runStateAtLeast(ctl.get(), STOP))) &&
                        !wt.isInterrupted())
                        wt.interrupt();
                    try {
                       // 开始执行任务前的Hook
                        beforeExecute(wt, task);
                        Throwable thrown = null;
                        try {
                            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 {
                           // 任务执行后的Hook,类似回调函数
                            afterExecute(task, thrown);
                        }
                    } finally {
                       // 执行完毕后task重置,completedTasks计数器++,解锁
                        task = null;
                        w.completedTasks++;
                        w.unlock();
                    }
                }
                completedAbruptly = false;
            } finally {
               // 线程空闲达到我们设定的值时,Worker退出销毁。
                processWorkerExit(w, completedAbruptly);
            }
        }
    
     /**
         * Performs blocking or timed wait for a task, depending on
         * current configuration settings, or returns null if this worker
         * must exit because of any of:
         * 1. There are more than maximumPoolSize workers (due to
         *    a call to setMaximumPoolSize).
         * 2. The pool is stopped.
         * 3. The pool is shutdown and the queue is empty.
         * 4. This worker timed out waiting for a task, and timed-out
         *    workers are subject to termination (that is,
         *    {@code allowCoreThreadTimeOut || workerCount > corePoolSize})
         *    both before and after the timed wait, and if the queue is
         *    non-empty, this worker is not the last thread in the pool.
         *
         * @return task, 如果工作者必须退出,则为null,在这种情况下workerCount将递减
         */
        private Runnable getTask() {
            boolean timedOut = false; // Did the last poll() time out?
    
            for (;;) {
                int c = ctl.get();
                int rs = runStateOf(c);
                // 如果线程池处于shutdown状态,
               // 并且队列为空,或者线程池处于stop或者terminate状态,
               // 在线程池数量-1,返回null,回收线程
                if (rs >= SHUTDOWN && (rs >= STOP || workQueue.isEmpty())) {
                    decrementWorkerCount();
                    return null;
                }
    
                int wc = workerCountOf(c);
                //如果allowCoreThreadTimeOut 为ture
                //或者当前线程数量大于核心线程池数目,
                //则需要超时回收
                boolean timed = allowCoreThreadTimeOut || wc > corePoolSize;
                //如果线程数目小于最大线程数目,
                //且不允许超时回收或者未超时,
                //则跳出循环,继续去阻塞队列中取任务
                if ((wc > maximumPoolSize || (timed && timedOut))
                    && (wc > 1 || workQueue.isEmpty())) {
                    //如果上面if没有成立,则当前线程数-1,返回null,回收该线程
                    //如果上面if没有成立,则CAS修改ctl失败,重读,cas循环重新尝试修改
                    if (compareAndDecrementWorkerCount(c))
                        return null;
                    continue;
                }
    
                try {
                //如果允许空闲回收,则调用阻塞队列的poll,
                //否则take,一直等到队列中有可取任务
                    Runnable r = timed ?
                        workQueue.poll(keepAliveTime, TimeUnit.NANOSECONDS) :
                        workQueue.take();
                //取到任务,返回任务,
                //否则超时timedOut = true;进入下一个循环,
                //并且在  if ((wc > maximumPoolSize || (timed && timedOut))处会不成立,进而进入到cas修改ctl的程序中
                    if (r != null)
                        return r;
                    timedOut = true;
                } catch (InterruptedException retry) {
                    timedOut = false;
                }
            }
        }
    

    相关文章

      网友评论

          本文标题:线程池的原理解析

          本文链接:https://www.haomeiwen.com/subject/xvbuiqtx.html