美文网首页javaAndroid开发程序员
Java线程池的实现原理

Java线程池的实现原理

作者: Frank_Kivi | 来源:发表于2017-09-30 01:08 被阅读258次

    Java提供了一整套的线程管理机制,就是我们常常提起的线程池。今天我们来研究一下它的实现原理,重点研究的是我们提交的任务是如何得到执行的。
    我们在构建ExecutorService时,都是调用Executors的方法。我们来查看一下Executors的方法。

        public static ExecutorService newFixedThreadPool(int nThreads) {
            return new ThreadPoolExecutor(nThreads, nThreads,
                                          0L, TimeUnit.MILLISECONDS,
                                          new LinkedBlockingQueue<Runnable>());
        }
        
        public static ExecutorService newFixedThreadPool(int nThreads, ThreadFactory threadFactory) {
            return new ThreadPoolExecutor(nThreads, nThreads,
                                          0L, TimeUnit.MILLISECONDS,
                                          new LinkedBlockingQueue<Runnable>(),
                                          threadFactory);
        }
        public static ExecutorService newCachedThreadPool() {
            return new ThreadPoolExecutor(0, Integer.MAX_VALUE,
                                          60L, TimeUnit.SECONDS,
                                          new SynchronousQueue<Runnable>());
        }
    

    我们以几个常用的为例,可以看到,它们都是调用了ThreadPoolExecutor这个类的构造。也就是说当我们得到的ExecutorService实际是ThreadPoolExecutor的实例。
    下边我们来查看一下ThreadPoolExecutor的构造方法。

        public ThreadPoolExecutor(int corePoolSize,
                                  int maximumPoolSize,
                                  long keepAliveTime,
                                  TimeUnit unit,
                                  BlockingQueue<Runnable> workQueue) {
            this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue,
                 Executors.defaultThreadFactory(), defaultHandler);
        }
    
        public ThreadPoolExecutor(int corePoolSize,
                                  int maximumPoolSize,
                                  long keepAliveTime,
                                  TimeUnit unit,
                                  BlockingQueue<Runnable> workQueue,
                                  ThreadFactory threadFactory) {
            this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue,
                 threadFactory, defaultHandler);
        }
    
        public ThreadPoolExecutor(int corePoolSize,
                                  int maximumPoolSize,
                                  long keepAliveTime,
                                  TimeUnit unit,
                                  BlockingQueue<Runnable> workQueue,
                                  RejectedExecutionHandler handler) {
            this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue,
                 Executors.defaultThreadFactory(), handler);
        }
    
        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;
        }
    

    我们关心的其实是把任务提交之后,它的执行流程。所以来查看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.
             */
            <!--获得当前运行的线程数,使用原子操作,线程安全。使用按位与的方式,保证这个c可以表示不同状态栏的线程,-->
            int c = ctl.get();
            if (workerCountOf(c) < corePoolSize) {
                <!--如果小于线程池的核心线程数,就调用addWorker,addWorker请看下边分析-->
                if (addWorker(command, true))
                    return;
                c = ctl.get();
            }
            <!--没有创建一个新的线程来执行当前command-->
            <!--把command加入到当前的workQueue队列中-->
            if (isRunning(c) && workQueue.offer(command)) {
                int recheck = ctl.get();
                <!--如果线程池没有运行的线程,同时也可以把command成功出队,就调用reject,其实就是用那个处理错误的handler来处理-->
                if (! isRunning(recheck) && remove(command))
                    reject(command);
                else if (workerCountOf(recheck) == 0)
                <!--如果线程数目为0,就重新创建,这次不限制只使用corePoolSize-->
                    addWorker(null, false);
            }
            else if (!addWorker(command, false))
                <!--再创建线程执行一次,失败就通知handler来处理-->
                reject(command);
        }
    
    <!--创建一个新的线程来执行当前任务,成功返回true-->
    private boolean addWorker(Runnable firstTask, boolean core) {
            retry:
            for (;;) {
                int c = ctl.get();
                int rs = runStateOf(c);
                
                // Check if queue empty only if necessary.
                <!--如果当前线程池的状态是SHUTDOWN但是 firstTask不为空,或者workQueue还有任务,表示出现了异常,返回false-->
                if (rs >= SHUTDOWN &&
                    ! (rs == SHUTDOWN &&
                       firstTask == null &&
                       ! workQueue.isEmpty()))
                    return false;
                <!--开启循环去创建线程-->
                for (;;) {
                    int wc = workerCountOf(c);
                    <!--如果当前线路程数已经大于或等于限制,创建失败,返回false-->
                    if (wc >= CAPACITY ||
                        wc >= (core ? corePoolSize : maximumPoolSize))
                        return false;
                    <!--尝试去创建线程,如果成功,就跳出循环  compareAndIncrementWorkerCount下边分析-->
                    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可以简单理解为就是一个封装咱们提交的Runnable的任务。-->
                w = new Worker(firstTask);
                <!--woker在new的同时会创建一个thread,后边分析-->
                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();
                                <!--创建成功后把Worker加入到它的管理集合中-->
                            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;
        }
        
    

    这里我们已经明白了,当一个任务提交之后,当前线程数小于corePoolSize是会去创建一个Woker来执行它。有些情况下会把它加入到workQueue中,当加入到集合中它是如何执行的呢。
    这就要说到Worker了,我们前边已经说了,有一个专门来执行任务的Woker对象,还有一个专门管理Worker的集合Workers。

    <!--可以看到WorKer本身实现了 Runnable-->
     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;
    
            /** Thread this worker is running in.  Null if factory fails. */
            final Thread thread;
            /** Initial task to run.  Possibly null. */
            Runnable firstTask;
            /** Per-thread task counter */
            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;
                <!--这就是前边说的Worker里边的线程,它又把this放进去,所以前边addWorker中的t.start最后执行的其实就是worker里边run方法-->
                this.thread = getThreadFactory().newThread(this);
            }
    
            /** Delegates main run loop to outer runWorker. */
            public void run() {
                <!--最终是调用runWorker为实现的-->
                runWorker(this);
            }
    
            // Lock methods
            //
            // The value 0 represents the unlocked state.
            // The value 1 represents the locked state.
    
            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) {
                    }
                }
            }
        }
    
    <!--最终去执行任务的方法-->
    final void runWorker(Worker w) {
            Thread wt = Thread.currentThread();
            <!--首先去把当前Worker身上绑定的任务取出来,然后把worker身上的任务置为null-->
            Runnable task = w.firstTask;
            w.firstTask = null;
            w.unlock(); // allow interrupts
            boolean completedAbruptly = true;
            try {
            <!--这里是关键,开启一个循环,当前任务不为空或者还能从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 {
                            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);
            }
        }
    

    看似worker执行完任务是会退出的,实际上却很难,因为getTask()是个阻塞式的方法,只有在特殊情况下才会返回null。

    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())) {
                    只有当线程池关闭了并且 线程池停止或者队列为空的时候才会返回null, 正常使用的时候都不会进来。
                    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())) {
                    如果worker的数量比maximumPoolSize还大,或者     等待超时并且队列为空的时候 才会返回null
                    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;
                }
            }
        }
    

    到此为止,我们简单的线程池的实现原理分析了一下。流程是这样的,首先我们提交一个任务。线程池可能会new一个worker也就是线程来执行这个任务,也可能把它加入到任务队列中(创建worker失败),然后等待执行。一旦一个worker启动之后,正常情况下它就不会停下来,执行完一个任务就执行下一个任务,因为取得任务的方法是阻塞式的。

    相关文章

      网友评论

      • 123861edb623:ctl是很重要的一个点,里面很多方法都在用ctl维护着
        Frank_Kivi:是的,暂时能明白的就是它是一个原子操作单位,所以线程安全。然后可以通过位运算来得到非常多的信息,具体是怎么操作的还不是太明白,有了解的可以分享下。

      本文标题:Java线程池的实现原理

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