java線程池ThreadPoolExecutor原理

java線程池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;
    }
  • corePoolSize 線程池的核心線程數大小,當線程數小於這個值的時候,新任務過來會創建線程並執行任務。
  • maximumPoolSize 線程池的總線程數大小,當隊列滿的時候會判斷線程數是否小於這個值,如果小於則進行創建線程並執行任務。
  • keepAliveTime 當超過核心線程數創建的線程空閒時間,如果超過這個時間當前線程就會執行完畢並關閉,通過獲取隊列超時來實現的。
  • unit 上面時間值的單位 毫秒,秒,分鐘。。。
  • workQueue 工作隊列,當核心線程數滿的時候就會把任務放進工作隊列。
  • threadFactory 創建線程的線程工廠,可以實現接口ThreadFactory,實現newThread方法來創建自己定義的線程,默認:Executors.defaultThreadFactory()。
  • handler 拒絕處理器,當隊列滿時候並且達到最大線程數,新增任務會觸發拒絕處理器,拒絕器會獲取當前要執行的任務,默認是拒絕並拋異常。

任務提交到線程池執行過程

線程池狀態:

    // 通過一個原子性整數的高低位來保存線程池的運行狀態和工作線程數。
    private final AtomicInteger ctl = new AtomicInteger(ctlOf(RUNNING, 0));
    private static final int COUNT_BITS = Integer.SIZE - 3;
    private static final int CAPACITY   = (1 << COUNT_BITS) - 1;

    // runState is stored in the high-order bits 高位表示狀態
    private static final int RUNNING    = -1 << COUNT_BITS;
    private static final int SHUTDOWN   =  0 << COUNT_BITS;
    private static final int STOP       =  1 << COUNT_BITS;
    private static final int TIDYING    =  2 << COUNT_BITS;
    private static final int TERMINATED =  3 << COUNT_BITS;

    // Packing and unpacking ctl
    // 獲取狀態
    private static int runStateOf(int c)     { return c & ~CAPACITY; }
    // 獲取工作線程數
    private static int workerCountOf(int c)  { return c & CAPACITY; }
    // 狀態和工作線程數合併成一個整數
    private static int ctlOf(int rs, int wc) { return rs | wc; }
  • RUNNING 線程池在運行中。
  • SHUTDOWN 線程池關閉狀態,
    新的任務不允許加入線程池,等待剩餘老的任務和隊列執行完畢,shutdown()會進入此狀態。
  • STOP 線程池處於停止狀態,shutdownNow()會進入此狀態。
  • TIDYING 當工作線程爲0的時候進入此狀態。
  • TERMINATED 線程池終止。

提交任務:

    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();
        }
        // 如果超過核心數,則任務放入隊列,然後再次檢測線程池是否在執行,如果不在執行則移除當前任務並拒絕任務。否則判斷工作線程是否爲0,如果爲0則通過增加一個空的任務增加一個線程。
        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);
    }

增加工作線程和任務:

    // firstTask表示當前線程的第一個任務,如果任務爲空,則只增加一個線程。
    // core 表示是否是核心線程,主要是判斷工作線程和核心線程或最大線程數做對比。
    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;
    }

工作線程內部類:

private final class Worker
       extends AbstractQueuedSynchronizer
       implements Runnable
   {
       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;
       // 創建新的線程,並把this賦給新的線程
       Worker(Runnable firstTask) {
           setState(-1); // inhibit interrupts until runWorker
           this.firstTask = firstTask;
           this.thread = getThreadFactory().newThread(this);
       }
       // 當addWorker成功後,調用worker.t.start()則執行此方法(線程會調用Runnable的run方法)。
       public void run() {
           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();
        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 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)) {// 如果還沒有到STOP狀態
            if (!completedAbruptly) {// 正常結束的線程
                int min = allowCoreThreadTimeOut ? 0 : corePoolSize;
                if (min == 0 && ! workQueue.isEmpty())
                    min = 1;
                if (workerCountOf(c) >= min)
                    return; // replacement not needed
            }
            // 非正常結束或者工作隊列不空並且線程數爲0則增加一個線程。
            addWorker(null, false);
        }
    }

獲取任務:

    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 {
            // 此處就是根據timed判斷來決定線程是否需要超時或者阻塞。
                Runnable r = timed ?
                    workQueue.poll(keepAliveTime, TimeUnit.NANOSECONDS) :
                    workQueue.take();
                if (r != null)
                    return r;
                timedOut = true;
            } catch (InterruptedException retry) {
                timedOut = false;
            }
        }
    }

關閉線程池

shutdown:啓動有序關閉,其中先前提交的任務將被執行,但不會接受任何新任務。

    public void shutdown() {
        final ReentrantLock mainLock = this.mainLock;
        mainLock.lock();
        try {
            checkShutdownAccess();
            advanceRunState(SHUTDOWN);// 設爲關閉狀態
            interruptIdleWorkers();// 中斷空閒的工作線程
            onShutdown(); // hook for ScheduledThreadPoolExecutor
        } finally {
            mainLock.unlock();
        }
        tryTerminate(); // 嘗試終止線程池
    }

showdownNow:嘗試停止所有正在執行的任務(調用線程的interrupt,不保證一定停止),停止等待任務的處理,並返回等待執行的任務列表,停止接受任何新的任務。

    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;
    }
    // 排出所有任務隊列中的任務
    private List<Runnable> drainQueue() {
        BlockingQueue<Runnable> q = workQueue;
        ArrayList<Runnable> taskList = new ArrayList<Runnable>();
        q.drainTo(taskList);
        if (!q.isEmpty()) {
            for (Runnable r : q.toArray(new Runnable[0])) {
                if (q.remove(r))
                    taskList.add(r);
            }
        }
        return taskList;
    }

線程池其他方法

允許核心線程超時:

    // allowCoreThreadTimeOut默認是false,當設置爲true的時候,核心線程數也會超時被回收。
    public void allowCoreThreadTimeOut(boolean value) {
        if (value && keepAliveTime <= 0)
            throw new IllegalArgumentException("Core threads must have nonzero keep alive times");
        if (value != allowCoreThreadTimeOut) {
            allowCoreThreadTimeOut = value;
            if (value)
                interruptIdleWorkers();
        }
    }

準備所有核心線程數:

    public int prestartAllCoreThreads() {
        int n = 0;
        while (addWorker(null, true))
            ++n;
        return n;
    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章