【原理】:JAVA线程池源码分析

总结:线程池ThreadPoolExecutor是通过控制Worker对象的数量来维护工作的工人集合,并且通过任务队列workerQueue来存储提交到线程池的任务。通过配置相关的容量,以及拒绝策略来更方便使用以及处理容量饱满的情况。Worker使用了同步器来解决任务执行前执行时执行后的同步问题。
值得注意的是submit()execute()的区别主要是submit()方法会将任务用FutureTask进行包装,包装之后在用execute()执行

java线程池内容较多,简单的架构如下。

线程池结构

顶层接口定义了execute方法

public interface Executor {

    /**
     * Executes the given command at some time in the future.  The command
     * may execute in a new thread, in a pooled thread, or in the calling
     * thread, at the discretion of the {@code Executor} implementation.
     *
     * @param command the runnable task
     * @throws RejectedExecutionException if this task cannot be
     * accepted for execution
     * @throws NullPointerException if command is null
     */
    void execute(Runnable command);
}

ExecutorService定义了对像城池的一些操作,submit()shutdown()在这里定义了

public interface ExecutorService extends Executor {
    void shutdown();

    List shutdownNow();

    boolean isShutdown();

    boolean isTerminated();

    boolean awaitTermination(long var1, TimeUnit var3) throws InterruptedException;

     Future submit(Callable var1);

     Future submit(Runnable var1, T var2);

    Future submit(Runnable var1);

     List> invokeAll(Collection> var1) throws InterruptedException;

     List> invokeAll(Collection> var1, long var2, TimeUnit var4) throws InterruptedException;

     T invokeAny(Collection> var1) throws InterruptedException, ExecutionException;

     T invokeAny(Collection> var1, long var2, TimeUnit var4) throws InterruptedException, ExecutionException, TimeoutException;
}

除了这两个接口意外还定义了ScheduledExecutorService接口,并额外定义了定时执行的功能

public interface ScheduledExecutorService extends ExecutorService {
    ScheduledFuture schedule(Runnable var1, long var2, TimeUnit var4);

     ScheduledFuture schedule(Callable var1, long var2, TimeUnit var4);

    ScheduledFuture scheduleAtFixedRate(Runnable var1, long var2, long var4, TimeUnit var6);

    ScheduledFuture scheduleWithFixedDelay(Runnable var1, long var2, long var4, TimeUnit var6);
}

再往下就是具体的实现了ThreadPoolExecutor作为一个具体的实现。来看他的构造方法的参数。

  1. corePoolSize:核心数
  2. maximumPoolSize:最大核心数
  3. keepAliveTime:当线程空闲时间达到keepAliveTime,该线程会退出,直到线程数量等于corePoolSize。(超出核心数的线程最大存活时间)
  4. unit:时间单位
  5. workQueue:任务队列
  6. threadFactory:线程工厂,可以设置线程的名字
  7. handler:拒绝策略,图示**Policy的就是handler对应的策略ThreadPoolExecutor内部实现了这些策略,可选配。
public ThreadPoolExecutor(int corePoolSize,
                              int maximumPoolSize,
                              long keepAliveTime,
                              TimeUnit unit,
                              BlockingQueue workQueue,
                              ThreadFactory threadFactory,
                              RejectedExecutionHandler handler)
  • 再来看看线程池的源码入口execute()或者submit()
  1. 值得注意的是submit()调用了execute()方法,具有返回值,并且对task进行了包装。
  2. execute()方法就是线程池执行的核心了。结合配置的线程和核心数,拒绝策略,有三种场景
    public  Future submit(Callable task) {
        if (task == null) throw new NullPointerException();
        RunnableFuture ftask = newTaskFor(task);
        execute(ftask);
        return ftask;
    }


    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.
         */
        //获取线程池状态,从而判断场景
        //场景一: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);
    }

通过对源码的分析发现无论如何workQueueaddWorker(cammand,boolean)是关键`。

  1. workQueue是一个阻塞队列,用于存放所有被提交到线程池的任务(线程)。
  2. addWorker():线程池里面都是工人的概念,增加一个工人来执行线程任务。工人可复用。
    /**
     * The queue used for holding tasks and handing off to worker
     * threads.  We do not require that workQueue.poll() returning
     * null necessarily means that workQueue.isEmpty(), so rely
     * solely on isEmpty to see if the queue is empty (which we must
     * do for example when deciding whether to transition from
     * SHUTDOWN to TIDYING).  This accommodates special-purpose
     * queues such as DelayQueues for which poll() is allowed to
     * return null even if it may later return non-null when delays
     * expire.
     */
    private final BlockingQueue workQueue;

addWorker()的工作主要还是初始化工人并且让工人开始工作,执行任务。并且维护一个HashSet来维护工人workers集合

    private final HashSet workers = new HashSet();


 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持有一个thread
            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工人是ThreadPoolExecutor的一个内部类,实现了Runnable说明他本身是一个任务,继承了AbstractQueuedSynchronizer说明有使用AQS保证一些执行动作是同步

    private final class Worker extends AbstractQueuedSynchronizer implements Runnable {
        private static final long serialVersionUID = 6138294804551838833L;
        
        //持有线程,该线程有配置的线程工厂产生
        final Thread thread;
        //持有需要执行的任务本身
        Runnable firstTask;
        volatile long completedTasks;

        Worker(Runnable var2) {
            this.setState(-1);
            this.firstTask = var2;
            this.thread = ThreadPoolExecutor.this.getThreadFactory().newThread(this);
        }
        //实现了run方法
        public void run() {
            ThreadPoolExecutor.this.runWorker(this);
        }

        protected boolean isHeldExclusively() {
            return this.getState() != 0;
        }

        protected boolean tryAcquire(int var1) {
            if (this.compareAndSetState(0, 1)) {
                this.setExclusiveOwnerThread(Thread.currentThread());
                return true;
            } else {
                return false;
            }
        }

        protected boolean tryRelease(int var1) {
            this.setExclusiveOwnerThread((Thread)null);
            this.setState(0);
            return true;
        }

        public void lock() {
            this.acquire(1);
        }

        public boolean tryLock() {
            return this.tryAcquire(1);
        }

        public void unlock() {
            this.release(1);
        }

        public boolean isLocked() {
            return this.isHeldExclusively();
        }

        void interruptIfStarted() {
            Thread var1;
            if (this.getState() >= 0 && (var1 = this.thread) != null && !var1.isInterrupted()) {
                try {
                    var1.interrupt();
                } catch (SecurityException var3) {
                }
            }

        }
    }

通过源码分析我们知道Worker的run方法调用了runWorker()方法,其中的同步操作就是task.run(),执行具体任务的run方法,而task的来源除了worker持有的firtTask,就是getTask()方法从workQueue中获取得到了。

    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);
        }
    }


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


最后还会通过processWorkerExit然当前工人执行回收策略,回收工人。循环使用了workers

    private void processWorkerExit(Worker w, boolean completedAbruptly) {
        if (completedAbruptly) // If abrupt, then workerCount wasn't adjusted
            decrementWorkerCount();

        final ReentrantLock mainLock = this.mainLock;
        mainLock.lock();
        try {
            completedTaskCount += w.completedTasks;
            workers.remove(w);
        } finally {
            mainLock.unlock();
        }

        tryTerminate();

        int c = ctl.get();
        if (runStateLessThan(c, STOP)) {
            if (!completedAbruptly) {
                int min = allowCoreThreadTimeOut ? 0 : corePoolSize;
                if (min == 0 && ! workQueue.isEmpty())
                    min = 1;
                if (workerCountOf(c) >= min)
                    return; // replacement not needed
            }
            addWorker(null, false);
        }
    }

你可能感兴趣的:(【原理】:JAVA线程池源码分析)