Java并发之线程池的实现原理

    上一篇我们简单介绍了Java线程池ThreadPoolExecutor的基本应用和简单的操作流程。这一篇我们将深入理解线程池的实现方法,只有彻底掌握,才能正确运用!

一、构造方法

    ThreadPoolExecutor的基本构造方法如下:
    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;
    这些参数都是上篇所介绍了的,不再赘述。

二、线程的提交

    ThreadPoolExecutor在构造完成后,便可以提交线程任务,提交方法主要有execute和submit。先介绍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();				//获取线程池状态
        if (workerCountOf(c) < corePoolSize) {		//工作线程数如果小于核心线程数
            if (addWorker(command, true))		//构造worker,添加线程并启动
                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))		//如果阻塞队列满,可能还没到最大线程数,所以尝试增加一个worker
            reject(command);
    }

    首先,线程池会获取自身状态,这里,线程池的状态由AtomicInteger变量ctl保存,ctl的高三位用来保存运行状态,第三位保存workerCount,也就是当前的有效线程数。因此,workerCount的上限是(2^29-1)。

    -1:RUNNING; 0:SHUTDOWN; 1:STOP; 2:TIDYING; 3:TERMINATED

    其次,理解Worker和Task的区别。Worker是Task的封装,构造它时,会装入task并创建一个线程。该线程在运行完task后,会从Queue中获取新的task运行。上述代码中,第一步和第三步会直接创建Worker来运行满足条件的task,第二步的task会进入阻塞队列等待线程池中的Worker调取。

    核心方法:addWorker

    

    源代码如下:

    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 (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 {
            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;
    }

    首先,addWorker对线程池状态进行了检测:

	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;

    这里,是对线程池状态的判定。

  • 如果状态为RUNNING(0),则直接跳过。
  • 如果状态为STOP、DITYING或者TERMINATED(rs>=SHUTDOWN && !(rs == SHUTDOWN)),那么不接受新线程,返回false。
  • 如果状态为>=SHUTDOWN,同时 firstTask != null,那么拒绝新线程。如果firstTask == null,那么可能是增加新线程来消耗Queue中的线程。但是同时还要检测queue是否为空,如果为空,那么队列已空,不需要增加消耗线程,如果队列没有空,那么可以将null插入队列中来清空队列。
    简单来说就是,如果state是RUNNING,那么一定是接收task的。如果为STOP、DITYING或者TERMINATED,那一定是不接收的。如果是SHUTDOWN,那么要看是不是为了清空队列而添加的空任务。如果是空任务并且队列不为空,那么接受。否则不接收。
    检测完成后:
            for (;;) {
                int wc = workerCountOf(c);	//获取工作线程数
                if (wc >= CAPACITY ||		//CAPACITY是ctl允许的最大工作线程数(2^29-1),不允许超出,否则导致状态变量出错
                    wc >= (core ? corePoolSize : maximumPoolSize))	//core为true 则判断最大核心线程数  否则最大线程数
                    return false;		//如果超出规定的最大线程数 返回false
                if (compareAndIncrementWorkerCount(c))	//CAS增加Worker数量
                    break retry;
                c = ctl.get();  // Re-read ctl
                if (runStateOf(c) != rs)	//查看状态是否改变 如果改变需要重来
                    continue retry;
                // else CAS failed due to workerCount change; retry inner loop
            }
    这段代码是从线程池内部参数的角度来进行判断的。内部线程数必须符合规定才能添加新的Worker。接下来是实例化Worker,之后会说。
    我们先看内部类Worker:
    private final class Worker
        extends AbstractQueuedSynchronizer
        implements Runnable
    可以发现,Worker继承了队列同步器,而且实现了Runnable。因此,它可以做Task使用,且实现了自己的同步规则。构造方法如下:
        Worker(Runnable firstTask) {
            setState(-1); // inhibit interrupts until runWorker  初始化 锁为-1
            this.firstTask = firstTask;
            this.thread = getThreadFactory().newThread(this);
        }
    Worker是对firstTask的封装,而且Worker本身就是Runable,是不是狸猫换太子,刺不刺激,意不意外?!通过线程工厂,为Worker为自己构建了一个线程。
    由于Worker本身就是Runnable,因此它也有自己的run方法。
        /** Delegates main run loop to outer runWorker  */
        public void run() {
            runWorker(this);
        }
    调用了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 {
            while (task != null || (task = getTask()) != null) {	//Worker初始时是有task的 如果没有,那就去队列中取
                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.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);
        }
    }
    可以看出,excute中的task是被Woker封装了一层。实际上,线程池中的线程,初始化时是一个有firstTask的Worker中的Thread字段。它在执行完自己的Runnable后,会从队列中取其他的Worker来执行它的task。
    getTask方法如下:
    private Runnable getTask() {
        boolean timedOut = false; // Did the last poll() time out?

        for (;;) {
            int c = ctl.get();
            int rs = runStateOf(c);

            // Check if queue empty only if necessary.
            if (rs >= SHUTDOWN && (rs >= STOP || workQueue.isEmpty())) {
                decrementWorkerCount();
                return null;
            }

            int wc = workerCountOf(c);

            // Are workers subject to culling?
            boolean timed = allowCoreThreadTimeOut || wc > corePoolSize;

            if ((wc > maximumPoolSize || (timed && timedOut))
                && (wc > 1 || workQueue.isEmpty())) {
                if (compareAndDecrementWorkerCount(c))
                    return null;
                continue;
            }

            try {
                Runnable r = timed ?
                    workQueue.poll(keepAliveTime, TimeUnit.NANOSECONDS) :
                    workQueue.take();
                if (r != null)
                    return r;
                timedOut = true;
            } catch (InterruptedException retry) {
                timedOut = false;
            }
        }
    }
    局部变量 timedOut判断后面的poll是否要超时。timed标识当前Worker超时是否要退出。
    线程池中的线程执行任务分两种情况:
    1)在execute()中创建一个线程,会让这个线程执行当前任务。
    2)在这个线程完成firstTask后,会反复从队列中获取任务执行。

三、终止线程

    shutdown方法停止接收新的任务,会完成线程池和队列中的任务。源码如下:

    public void shutdown() {
        final ReentrantLock mainLock = this.mainLock;
        mainLock.lock();
        try {
            checkShutdownAccess();	//判断可否操作目标线程
            advanceRunState(SHUTDOWN);	//设置线程池状态SHUTDOWN
            interruptIdleWorkers();	//中断所有空闲线程
            onShutdown(); // hook for ScheduledThreadPoolExecutor
        } finally {
            mainLock.unlock();
        }
        tryTerminate();
    }

    shutdown完成了这几件事:

    1)检查能否操作线程

    2)将线程池状态转为SHUTDOWN

    3)中断所有空闲线程
    shutdownNow会立刻停止接收新任务,且不再从队列中获取任务,而且停止正在运行的线程。

    public List<Runnable> shutdownNow() {
        List<Runnable> tasks;
        final ReentrantLock mainLock = this.mainLock;
        mainLock.lock();
        try {
            checkShutdownAccess();
            advanceRunState(STOP);	//设置状态
            interruptWorkers();
            tasks = drainQueue();
        } finally {
            mainLock.unlock();
        }
        tryTerminate();
        return tasks;
    }
    它会中断所有线程。而不是空闲线程。然后抛弃队列中的所有任务。






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值