线程池ThreadPoolExecutor分析

线程池,线程池是什么,说到底,线程池是处理多线程的一种形式,管理线程的创建,任务的执行,避免了无限创建新的线程带来的资源消耗,能够提高应用的性能。很多相关操作都是离不开的线程池的,比如android应用中网络请求的封装。这篇博客要解决的问题是:

1.线程池的工作原理及过程。


要分析线程池的工作原理及过程,还是要从它的源码实现入手,首先是线程是构造方法,何谓构造方法,构造方法就是对成员变量进行初始化,在这里,我们可以看到它的构造方法:

/**
     * 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;
    }
第一个参数,corePoolSize核心线程的数量;maximumPoolSize最大线程数量;keepAliveTime和 TimeUnit非核心线程闲置时间,超过这个设置时间将会被终止,TimeUnit含有多种静态成员变量作为单位,如seconds;BlockingQueue任务阻塞队列,当核心线程创建数量达到最大值时,任务首先会加入到阻塞队列,等待执行;ThreadFactory 线程构造工厂,常用的有DefaultThreadFactory,我们也可以重写它的newThread方法,实现这个类;RejectedExecutionHandler,当任务被拒绝添加时,将会交给这个类处理。好了,构造的过程,就是这么简单,就是初始化一些成员变量。


分析的时候,从关键点着手,这里分析是从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 the task to execute
     * @throws RejectedExecutionException at discretion of
     *         {@code RejectedExecutionHandler}, if the task
     *         cannot be accepted for execution
     * @throws NullPointerException if {@code command} is null
     */
    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的方法,传入的是一个Runnable,这个方法就是我们需要执行的任务。下面我们分析execute是怎么执行这个任务的,也就是execute执行的过程:

        1.如果线程池中线程的数量小于核心线程数目,则启动一个新的线程处理这个任务。

        2.如果核心线程处于非空闲状态,则将任务插入到阻塞队列中,当有线程空闲时,会自动取出任务执行。

        3.如果阻塞队列任务已经满了,并且当前线程小于最大线程数目,则启动新的线程,执行任务,如果超过了最大线程数,则拒绝接受新的任务。

上面是整个代码的过程,现在我们队代码进行分析
        

 if (workerCountOf(c) < corePoolSize) {
            if (addWorker(command, true))
                return;
workerCountOf(c)表示的是当前线程的数量,如果这个数量小于corePoolSize,则调用addWorker(command,true)方法,如果addWorker执行成功,返回true,表示任务执行完成。下面我们看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方法比较长,我们分为两块,第一块的for循环,第二块try...。第一块,for循环,主要是判断当前是否可以执行任务,如果可以,则进行下面的try,通过的条件,可以分析出来,是当前线程小于核心线程,或者当前阻塞队列已满,线程数小于最大线程数量,满足这两个中的条件,才能往下走。       try里面通过当前的参数,新建了一个Worker,这个Worker实现了Runable接口,Worker里面通过ThreadFactory构建了一个Thread来执行这个任务,代码的后面,调用了t.start(),实际上,调用的是,Worker实现Runable的run方法,run方法调用的又是runWorker(),我们看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 {
            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.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);
        }
    }

首先我们要知道,整个runWorker方法是在Thread的线程中执行的。runWorker中,while循环中,加锁,第一次task不为空,执行这个firstTask,当task.run()执行完,解锁,然后调用getTask(),getTask是从阻塞队列中取出任务来执行,因此,这里,我们得出结论,当线程完成一个任务时,会从阻塞队列里取出任务来执行。        while循环的条件是,task 不为空,或者getTask不为空,如果task为空,并且getTask也为空,就跳出循环,但是,先请看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;
            }
        }
    }

这个方法的主要作用是从队列中取出一个任务执行,然而,我们仔细分析,如果allowCoreThreadTimeOut为true(也就是说允许核心线程超时),或者当前线程是非核心线程,那么timed=true,这时候,进入if语句判断,wc>maximuPoolsize,这个一般不成立,直接看timed&&timeOut,timed=true,timeOut为false,直接进入try,最后timeOut=true,当后面循环时,如果队列的任务为空,就会执行到compareAndDecrementWorkerCount(c)减少一个线程数量,然后返回null,线程也就终止执行了;但是如果allowCoreThreadTimeOut=false,就会直接调用workQueue.take(),直接取出一个Runnable,如果runnable为空,就将一直循环,线程阻塞在这里,也就是核心线程处于空闲状态。这里得出新的结论,如果核心线程没有允许超时,那么它将一直处于空闲状态,不会被回收。

        

我们再次回到execute方法,第二部分

 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);
        }
如果当前线程池处于运行状态,并且这个任务能插入到队列中(因为这里线程数目已经时等于核心线程数了,因此插入到队列中,等待执行),第二次判断,如果当前线程池不处于运行状态,那么移除这个任务,调用reject方法处理;如果当前线程池的线程数量为0,addWorker(),传入null和false,也就是说,不添加新任务,并且时启动一个非核心线程,这个核心线程会通过getTask()方法取出任务执行,这个和上面分析的过程一样。


第三部分

 else if (!addWorker(command, false))
            reject(command);
先启动一个非核心线程执行任务,如果非核心线程达到了最大线程数,才会拒绝执行任务。


最后的结论就是:

1.如果线程池中线程的数量小于核心线程数目,则启动一个新的线程处理这个任务。

2.如果核心线程处于非空闲状态,则将任务插入到阻塞队列中,当有线程空闲时,会自动取出任务执行。

3.如果阻塞队列任务已经满了,并且当前线程小于最大线程数目,则启动新的线程,执行任务,如果超过了最大线程数,则拒绝接受新的任务。

如果核心线程没有设置为允许超时,那么核心线程会一直存在,即时等待执行任务。






  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值