0
点赞
收藏
分享

微信扫一扫

源码剖析ThreadPoolExecutor线程池及阻塞队列

本文章对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.acc = System.getSecurityManager() == null ?
                null :
                AccessController.getContext();
        //核心线程
        this.corePoolSize = corePoolSize;
        //最大线程数
        this.maximumPoolSize = maximumPoolSize;
        //阻塞队列,即今天主题
        this.workQueue = workQueue;
        //超时时间
        this.keepAliveTime = unit.toNanos(keepAliveTime);
        this.threadFactory = threadFactory;
        //拒绝策略
        this.handler = handler;
    }

这是我们线程池实例化的时候的参数,其实最大的实用性来说,就是核心线程与最大线程数的设定,这个完全靠个人经验,并没有一个真正意义上的公式可以适用所有的业务场景,这里博主为大家找了一篇关于设定线程数的文章:

  https://tech.meituan.com/2020/04/02/java-pooling-pratice-in-meituan.html

  我们的线程池初始化好后,我们自己会调用excute方法来让线程池运行我们的线程任务,那我们就先来看看这个方法的实现:

public void execute(Runnable command) {
        if (command == null)
            throw new NullPointerException();
        /*
         * 第一步:工作线程是否小于核心线程数量,如果是添加work中,worker其实也是一个线程,只不过它内部操作的是我们的上传的任务
         * 第二步:如果大于核心线程数量,添加到worker队列中,每一个不同的队列offer的实现方法也是不一样的,今天我们主要探讨这个
         * 第三步:阻塞队列被塞满了,需要创建新的非核心线程数量worker线程去处理我们的任务,创建worker线程失败了会触发拒绝策略,默认抛异常
         */
        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);
    }

我们看到当任务调用的时候,会执行addworker,那么worker是个什么东西呢?我们来看看它的构造实例:我们看一下worker类,就发现其实worker也是一个线程

private final class Worker
        extends AbstractQueuedSynchronizer
        implements Runnable
    {

    ......

    Worker(Runnable firstTask) {
            setState(-1); // inhibit interrupts until runWorker
            this.firstTask = firstTask;
            this.thread = getThreadFactory().newThread(this);
        }

        /** 覆盖执行run方法
          */
        public void run() {
            runWorker(this);
        }
    ......

    }

这次我们来看一下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 {
            //主要的创建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) {
                    //此处调用的是worker线程的start方法,并没有直接调用我们的 任务
                    //上面我们看worker的run方法了,里面调用的 是runWorker,那我们看看runWorker方法就可以了
                    t.start();
                    workerStarted = true;
                }
            }
        } finally {
            if (! workerStarted)
                addWorkerFailed(w);
        }
        return workerStarted;
    }

到这里添加完毕后,我们在看看它是是如何执行我们的线程的,来看看runworker方法实现:

final void runWorker(Worker w) {
        Thread wt = Thread.currentThread();
        Runnable task = w.firstTask;
        w.firstTask = null;
        w.unlock(); // allow interrupts
        boolean completedAbruptly = true;
        try {
            //这里体现的是线程的复用,复用的是worker线程,每处理一个线程都会getTask()从队列中取一个任务进行处理
            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 {
                        //直接调用我们任务的run方法,我们任务虽然是继承了runable,但是并没有调用start方法
                        //其实我们的线程放入线程池中,并不是让我们的线程运行,仅仅是定义了一个方法体,
                        //真正运行的是被线程池管理的worker线程
                        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);
        }
    }

这个时候,大家应该就解决了一个问题就是,线程池如何体现的线程复用,就在gettask那里体现的,复用的就是worker线程,好了,这个时候不仅worker创建完成了,并且直接调用start方法,让自己开始运行起来,执行本次添加的任务,但是细心的小伙伴会看到参数传入的核心线程为true,那么此时也仅仅启动了核心线程,那么超过核心线程数的就应该加入到队列中,那么有什么队列供我们选择呢?

  所以我们接下来看BlockingQueue的offer、poll(如果设置超时时间)、take方法,BlockingQueue有很多实现类,我们主要看以下几个:   ArrayBlockingQueue、LinkedBlockingQueue、PriorityBlockingQueue、SynchronousQueue、DelayQueue五种BlockingQueue;

  我们首先来说一说ArrayBlockingQueue,我们看看其构造函数

public ArrayBlockingQueue(int capacity) {
//必须指定队列大小,默认非公平锁
        this(capacity, false);
    }

public ArrayBlockingQueue(int capacity, boolean fair) {
        if (capacity <= 0)
            throw new IllegalArgumentException();
        this.items = new Object[capacity];
        lock = new ReentrantLock(fair);
        notEmpty = lock.newCondition();
        notFull =  lock.newCondition();
    }
public boolean offer(E e) {
        checkNotNull(e);
        final ReentrantLock lock = this.lock;
        lock.lock();
        try {//超过队列大小,将不继续存放,返回false创建worker线程
            if (count == items.length)
                return false;
            else {
                //数组后添加任务元素,使用到了循环数组的算法
                enqueue(e);
                return true;
            }
        } finally {
            lock.unlock();
        }
    }
public E poll(long timeout, TimeUnit unit) throws InterruptedException {
        long nanos = unit.toNanos(timeout);
        final ReentrantLock lock = this.lock;
        lock.lockInterruptibly();
        try {
            while (count == 0) {
                if (nanos <= 0)
                    return null;
                //等待时间,如果超过默认1000微妙,则将阻塞当前线程,等待添加任务时将其唤醒或者等待超时
                nanos = notEmpty.awaitNanos(nanos);
            }
            return dequeue();
        } finally {
            lock.unlock();
        }
    }
public E take() throws InterruptedException {
        final ReentrantLock lock = this.lock;
        lock.lockInterruptibly();
        try {
            while (count == 0)
                //等待被唤醒,无超时时间
                notEmpty.await();
            return dequeue();
        } finally {
            lock.unlock();
        }
    }

ArrayBlockingQueue讲解完了,再来看看LinkedBlockingQueue:三个方法对于任务的存放与取出与ArrayBlockingQueue并无太大差别,我们就不做太多的讲解,简单说一下,主要的就是节点阻塞队列与数组阻塞队列所用到的锁机制不一样,主要是因为数组是一个对象,而节点操作的则是对头与队尾节点,所以用到了两个taskLock与putLock锁,两者在对于队列的取与添加并不会产生冲突

public LinkedBlockingQueue() {
       //链表类型虽然不用指定大小队列,但是默认时int的最大值,实际场景下会引发内存溢出问题
        this(Integer.MAX_VALUE);
    }

 public LinkedBlockingQueue(int capacity) {
        if (capacity <= 0) throw new IllegalArgumentException();
        this.capacity = capacity;
        last = head = new Node<E>(null);
    }

PriorityBlockingQueue,总体来说,是具有优先级考虑的任务队列,因为任务需要实现comparator接口,先看看其构造器吧;

public PriorityBlockingQueue() {
//默认11长度大小,无比较器
        this(DEFAULT_INITIAL_CAPACITY, null);
    }

public PriorityBlockingQueue(int initialCapacity) {
//可指定长度大小
        this(initialCapacity, null);
    }

public PriorityBlockingQueue(int initialCapacity,
                                 Comparator<? super E> comparator) {
        if (initialCapacity < 1)
            throw new IllegalArgumentException();
        this.lock = new ReentrantLock();
        this.notEmpty = lock.newCondition();
//可指定比较器
        this.comparator = comparator;
        this.queue = new Object[initialCapacity];
    }
public boolean offer(E e) {
        if (e == null)
            throw new NullPointerException();
        final ReentrantLock lock = this.lock;
        lock.lock();
        int n, cap;
        Object[] array;
        while ((n = size) >= (cap = (array = queue).length))
//在这里队列的长度不再固定,而是实现了自动扩展
            tryGrow(array, cap);
        try {
            Comparator<? super E> cmp = comparator;
//默认与不默认都会使用comparator接口让数组进行比较,使优先级高的在数组最前面
            if (cmp == null)
                siftUpComparable(n, e, array);
            else
                siftUpUsingComparator(n, e, array, cmp);
            size = n + 1;
            notEmpty.signal();
        } finally {
            lock.unlock();
        }
        return true;
    }
public E poll(long timeout, TimeUnit unit) throws InterruptedException {
        long nanos = unit.toNanos(timeout);
        final ReentrantLock lock = this.lock;
        lock.lockInterruptibly();
        E result;
        try {
//每次取出任务的时候都会进行优先级比较,放到数组的第一个
            while ( (result = dequeue()) == null && nanos > 0)
                nanos = notEmpty.awaitNanos(nanos);
        } finally {
            lock.unlock();
        }
        return result;
    }
public E take() throws InterruptedException {
        final ReentrantLock lock = this.lock;
        lock.lockInterruptibly();
        E result;
        try {
//同理,也会进行优先级比较,虽然每次都比较但是在添加任务元素的时候已经是排好序的了
            while ( (result = dequeue()) == null)
                notEmpty.await();
        } finally {
            lock.unlock();
        }
        return result;
    }

SynchronousQueue是一个比较特殊的队列,固定长度为1,并且可以说是实时进行任务运行,并且必须已经有worker任务结束在获取其他任务的时候才会在队列中添加任务元素,否则一直为返回null,非常适合在一个请求需要同时拉去多个服务的场景;

public SynchronousQueue() {
        this(false);
    }

 public SynchronousQueue(boolean fair) {
        transferer = fair ? new TransferQueue<E>() : new TransferStack<E>();
    }

并且是上面提到的offer、take、poll三个方法的操作都是一个transfer方法进行操作的,我们主要看一下这两个类在这个方法上有哪些区别;首先看一下结构简单的TransferQueue;

//存数据的时候,参数为transfer(e, true, 0)
//取数据的时候,参数为transfer(null, false, 0)
E transfer(E e, boolean timed, long nanos) {
//作者认为这个方法主要做了一下几件事情:
//1、根据传进来的任务参数,判断当前是取数据还是放数据
//2、如果是存数据,判断当前队列是否是空队列,如果是则返回false,线程池则会创建新的worker线程,不是空队列则会唤醒进行获取任务的worker线程并返回数据
//3、如果是拿数据,判断是否是空队列,则新创建节点并阻塞当前线程等待放数据时唤醒,不是空队列(换一种说法就是已经有一个线程正在等待获取任务),抛出头结点,继续往下走,这里有个疑问,抛出去后该节点的线程一直在等待,无法被唤醒了
QNode s = null; // constructed/reused as needed
            boolean isData = (e != null);

            for (;;) {
                QNode t = tail;
                QNode h = head;
                if (t == null || h == null)         // saw uninitialized value
                    continue;                       // spin

                if (h == t || t.isData == isData) { // empty or same-mode
                    QNode tn = t.next;
                    if (t != tail)                  // inconsistent read
                        continue;
                    if (tn != null) {               // lagging tail
                        advanceTail(t, tn);
                        continue;
                    }
                    if (timed && nanos <= 0)        // can't wait
                        return null;
                    if (s == null)
                        s = new QNode(e, isData);
                    if (!t.casNext(null, s))        // failed to link in
                        continue;

                    advanceTail(t, s);              // swing tail and wait
                    Object x = awaitFulfill(s, e, timed, nanos);
                    if (x == s) {                   // wait was cancelled
                        clean(t, s);
                        return null;
                    }

                    if (!s.isOffList()) {           // not already unlinked
                        advanceHead(t, s);          // unlink if head
                        if (x != null)              // and forget fields
                            s.item = s;
                        s.waiter = null;
                    }
                    return (x != null) ? (E)x : e;

                } else {                            // complementary-mode
                    QNode m = h.next;               // node to fulfill
                    if (t != tail || m == null || h != head)
                        continue;                   // inconsistent read

                    Object x = m.item;
                    if (isData == (x != null) ||    // m already fulfilled
                        x == m ||                   // m cancelled
                        !m.casItem(x, e)) {         // lost CAS
                        advanceHead(h, m);          // dequeue and retry
                        continue;
                    }

                    advanceHead(h, m);              // successfully fulfilled
                    LockSupport.unpark(m.waiter);
                    return (x != null) ? (E)x : e;
                }
            }
        }

不知道大家看到这里是否有疑问,为什么当多个worker线程开始获取任务时,已经等待的节点会被抛出去,只会给队列留下最新的一个等待节点,其他节点根本不会再被唤醒了,其实这也是我的疑问,不知道大家有没有注意到,希望高手们可以给一个解释;下面再看一个TransferStack,这个是默认线程池队列初始化中使用的节点类型,这个比较好理解,也通俗易懂一点;我们看看其源码:

E transfer(E e, boolean timed, long nanos) {
//该实现类跟上一个有一些区别,但是总体逻辑差不多,也是分以下几步:
//第一步:根据传进来的任务参数判断是请求数据,还是存入数据
//第二步:如果是存入数据,空节点时直接返回null,让线程池创建worker线程运行任务,如果有等待节点,那么存入当前任务数据,并且再移除存入的数据节点和等待的节点,等待的节点此时会被赋值存入的任务并被唤醒
//第三步:如果是取出数据,空节点时存入等待数据节点并阻塞当前线程,如果不是空节点,已经有了等待节点,那么将会除去等待节点并唤醒,还会除去当前钢加入的等待节点,使当前节点队列还是保持null
 SNode s = null; // constructed/reused as needed
            int mode = (e == null) ? REQUEST : DATA;

            for (;;) {
                SNode h = head;
                if (h == null || h.mode == mode) {  // empty or same-mode
                    if (timed && nanos <= 0) {      // can't wait
                        if (h != null && h.isCancelled())
                            casHead(h, h.next);     // pop cancelled node
                        else
                            return null;
                    } else if (casHead(h, s = snode(s, e, h, mode))) {
                        SNode m = awaitFulfill(s, timed, nanos);
                        if (m == s) {               // wait was cancelled
                            clean(s);
                            return null;
                        }
                        if ((h = head) != null && h.next == s)
                            casHead(h, s.next);     // help s's fulfiller
                        return (E) ((mode == REQUEST) ? m.item : s.item);
                    }
                } else if (!isFulfilling(h.mode)) { // try to fulfill
                    if (h.isCancelled())            // already cancelled
                        casHead(h, h.next);         // pop and retry
                    else if (casHead(h, s=snode(s, e, h, FULFILLING|mode))) {
                        for (;;) { // loop until matched or waiters disappear
                            SNode m = s.next;       // m is s's match
                            if (m == null) {        // all waiters are gone
                                casHead(s, null);   // pop fulfill node
                                s = null;           // use new node next time
                                break;              // restart main loop
                            }
                            SNode mn = m.next;
                            if (m.tryMatch(s)) {
                                casHead(s, mn);     // pop both s and m
                                return (E) ((mode == REQUEST) ? m.item : s.item);
                            } else                  // lost match
                                s.casNext(m, mn);   // help unlink
                        }
                    }
                } else {                            // help a fulfiller
                    SNode m = h.next;               // m is h's match
                    if (m == null)                  // waiter is gone
                        casHead(h, null);           // pop fulfilling node
                    else {
                        SNode mn = m.next;
                        if (m.tryMatch(h))          // help match
                            casHead(h, mn);         // pop both h and m
                        else                        // lost match
                            h.casNext(m, mn);       // help unlink
                    }
                }
            }
        }

接下来我们再来讨论一下DelayQueue,这个队列就和PriorityQueue有些关联,具体关联在哪里呢?我们看看它 的源码;

public class DelayQueue<E extends Delayed> extends AbstractQueue<E>
    implements BlockingQueue<E> {

    private final transient ReentrantLock lock = new ReentrantLock();
    //内部使用的是PriorityQueue作为存储对象,但是该DelayQueue为任务元素指定了比较器,就是时间比较器
    private final PriorityQueue<E> q = new PriorityQueue<E>();
}
//比较器是这个,定义了Comparable<Delayed>,查看时间是否到达或超过了指定时间
public interface Delayed extends Comparable<Delayed> {

    /**
     * Returns the remaining delay associated with this object, in the
     * given time unit.
     *
     * @param unit the time unit
     * @return the remaining delay; zero or negative values indicate
     * that the delay has already elapsed
     */
    long getDelay(TimeUnit unit);
}

剩下的offer、poll、take我就不讲解了,去元素的时候就是多判断了一步,是否超过或到达指定时间,否则将会使当前线程进行等待剩余的时间,而不是自旋

最后总结一下线程池以及使用到的队列原理:

  • 线程池为何会比自己创建线程更加高效、方便,第一点就是线程池已经帮我们封装好了并且对线程进行了管理,比如生产者消费者模式,使我们的线程池高效的利用CPU进行处理任务,也可以对我们的业务场景来看使用哪个队列;第二点是线程池帮我们把线程进行了复用,而不是处理完一个任务就丢弃一个线程;
  • 队列中ArrayBlockingQueue与LinkedBlockingQueue,处理节点存储类型不一样,一个是数组,一个是节点类型,还有LinkedBlockingQueue使用到了存储锁与取锁,两者操作并不冲突,而ArrayBlockingQueue则使用了一个排它锁,取数据与存数据用的是一把锁。
  • 而PriorityBlockingQueue和DelayQueue,虽然内部都使用了PriorityQueue作为存储介质,但是PriorityBlockingQueue不会强制要求你使用哪一种比较器,而DelayQueue必须使用时间比较器更加局限也明确了任务的类型。
  • 最后说一下SynchronousQueue,该队列比其他队列特殊一点,该队列是同步类型的队列,就是说队列不存储任务数据,而是必须有正在获取的等待节点才会让数据暂时放入队列中然后立马取出,或者不会放入队列,而是替换到等待队列中的任务并唤醒等待队列,从而到达任务不会被存储在队列中。也就是说不会缓冲任务放入队列中,更像是实时任务取出并处理。

好了,我们的学习也到此结束了,并且最后提示大家使用线程池的时候,最好自己定义线程池参数,而不是使用Executors使用默认参数来创建线程。就是因为写的队列长度过大,会导致程序崩溃

举报

相关推荐

0 条评论