線程池原理

線程池框架.png

線程池源碼解析

核心的構(gòu)造方法

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

核心參數(shù)說明
int corePoolSize, 池中保持的最大線程數(shù),包括空線程
int maximumPoolSize, 池中允許最大線程數(shù)
long keepAliveTime, 當(dāng)線程大于核心線程時,此為終止前多余的線程等待新任務(wù)的最長時間
TimeUnit unit, keepAliveTime 參數(shù)的時長單位
BlockingQueue<Runnable> workQueue, 執(zhí)行用于保持任務(wù)的隊列。此隊列僅由execute方法提交的Runnable任務(wù)
ThreadFactory threadFactory, 執(zhí)行程序創(chuàng)建線程時使用的工廠
RejectedExecutionHandler handler 由于超出線程范圍和隊列容量而執(zhí)行的拒絕策略。

runnableTaskQueue(任務(wù)隊列):用于保存等待執(zhí)行的任務(wù)的阻塞隊列。可以選擇以下幾
個阻塞隊列。
ArrayBlockingQueue:是一個基于數(shù)組結(jié)構(gòu)的有界阻塞隊列,此隊列按FIFO(先進(jìn)先出)原則對元素進(jìn)行排序。
LinkedBlockingQueue:一個基于鏈表結(jié)構(gòu)的阻塞隊列,此隊列按FIFO排序元素,吞吐量通常要高于ArrayBlockingQueue。靜態(tài)工廠方法Executors.newFixedThreadPool()使用了這個隊列。
SynchronousQueue:一個不存儲元素的阻塞隊列。每個插入操作必須等到另一個線程調(diào)用移除操作,否則插入操作一直處于阻塞狀態(tài),吞吐量通常要高于Linked-BlockingQueue,靜態(tài)工廠方法Executors.newCachedThreadPool使用了這個隊列。
PriorityBlockingQueue:一個具有優(yōu)先級的無限阻塞隊列。

RejectedExecutionHandler(飽和策略):當(dāng)隊列和線程池都滿了,說明線程池處于飽和狀態(tài),那么必須采取一種策略處理提交的新任務(wù)。這個策略默認(rèn)情況下是AbortPolic

線程池運行核心代碼

  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();
//判斷是否小于核心數(shù)量,是直接新增work成功后直接退出 
        if (workerCountOf(c) < corePoolSize) {
            if (addWorker(command, true))
                return;
// 增加失敗后繼續(xù)獲取標(biāo)記
            c = ctl.get();
        }
//判斷是運行狀態(tài)并且扔到workQueue里成功后
        if (isRunning(c) && workQueue.offer(command)) {
            int recheck = ctl.get();
//再次check判斷運行狀態(tài)如果是非運行狀態(tài)就移除出去&reject掉
            if (! isRunning(recheck) && remove(command))
                reject(command);
 //否則發(fā)現(xiàn)可能運行線程數(shù)是0那么增加一個null的worker。
            else if (workerCountOf(recheck) == 0)
                addWorker(null, false);
        }
 //直接增加worker如果不成功直接reject
        else if (!addWorker(command, false))
            reject(command);
    }


 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;// 兩種情況1.如果非運行狀態(tài)  2.不是這種情況(停止?fàn)顟B(tài)并且是null對象并且workQueue不等于null)

            for (;;) {
                int wc = workerCountOf(c);

                if (wc >= CAPACITY ||
                    wc >= (core ? corePoolSize : maximumPoolSize))
                    return false; // 判斷是否飽和容量了
                if (compareAndIncrementWorkerCount(c))  //增加一個work數(shù)量 然后跳出去
                    break retry;
                c = ctl.get();  //  增加work失敗后繼續(xù)遞歸
                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);//增加一個worker
            final Thread t = w.thread;
            if (t != null) {//判斷是否 為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.
                 //鎖定后并重新檢查下 是否存在線程工廠的失敗或者鎖定前的關(guān)閉
                    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) {{ //本次要是新增加work成功就調(diào)用start運行
                    t.start();
                    workerStarted = true;
                }
            }
        } finally {
            if (! workerStarted)
                addWorkerFailed(w);
        }
        return workerStarted;
    }

   final void runWorker(Worker w) {
        Thread wt = Thread.currentThread();//1.取到當(dāng)前線程
        Runnable task = w.firstTask;
        w.firstTask = null;
        w.unlock(); // allow interrupts
        boolean completedAbruptly = true;
        try {
            while (task != null || (task = getTask()) != null) {//獲取任務(wù) 看看是否能拿到
                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);//開始任務(wù)前的鉤子
                    Throwable thrown = null;
                    try {
                        task.run();//執(zhí)行任務(wù)
                    } 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); //任務(wù)后的鉤子
                    }
                } finally {
                    task = null;
                    w.completedTasks++;
                    w.unlock();
                }
            }
            completedAbruptly = false;
        } finally {
            processWorkerExit(w, completedAbruptly);
        }
    }

  /**
     * Performs cleanup and bookkeeping for a dying worker. Called
     * only from worker threads. Unless completedAbruptly is set,
     * assumes that workerCount has already been adjusted to account
     * for exit.  This method removes thread from worker set, and
     * possibly terminates the pool or replaces the worker if either
     * it exited due to user task exception or if fewer than
     * corePoolSize workers are running or queue is non-empty but
     * there are no workers.
     *
     * @param w the worker
     * @param completedAbruptly if the worker died due to user exception
     */
    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); //移除work
        } finally {
            mainLock.unlock();
        }

        tryTerminate();

        int c = ctl.get();
        if (runStateLessThan(c, STOP)) {  //判斷是否還有任務(wù)
            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);
        }
    }



1、如果當(dāng)前池大小 poolSize 小于 corePoolSize ,則創(chuàng)建新線程執(zhí)行任務(wù)
2、如果當(dāng)前池大小poolSize 大于cprePoolSize 且等待隊列未滿,則進(jìn)入等待隊列
3、如果當(dāng)前池大小 poolSize 大于 corePoolSize 且小于 maximumPoolSize ,且等待隊列已滿,則創(chuàng)建新線程執(zhí)行任務(wù)
4、如果當(dāng)前池大小 poolSize 大于 corePoolSize 且大于 maximumPoolSize ,且等待隊列已滿,則調(diào)用拒絕策
略來處理該任務(wù)

拒絕策略

AbortPolicy:拋出異常, 默認(rèn)
CallerRunsPolicy:不使用線程池執(zhí)行
DiscardPolicy:直接丟棄任務(wù)
DiscardOldestPolicy:丟棄隊列中最舊的任務(wù)對 于 線 程 池 選 擇 的 拒 絕 策 略 可 以 通 過 RejectedExecutionHandler handler = new
ThreadPoolExecutor.CallerRunsPolicy();來設(shè)置。

線程池執(zhí)行流程.png

調(diào)度線程池原理

    public static void main(String[] args) {
        ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(10);
        scheduledExecutorService.schedule(new Runnable() {
            public void run() {
                System.out.println("5秒之后執(zhí)行");
            }
        }, 5, TimeUnit.SECONDS);
    }

    public ScheduledFuture<?> schedule(Runnable command,
                                       long delay,
                                       TimeUnit unit) {
        if (command == null || unit == null)
            throw new NullPointerException();
        RunnableScheduledFuture<?> t = decorateTask(command,
            new ScheduledFutureTask<Void>(command, null,
                                          triggerTime(delay, unit)));
        delayedExecute(t);
        return t;
    }

    /**
     * Main execution method for delayed or periodic tasks.  If pool
     * is shut down, rejects the task. Otherwise adds task to queue
     * and starts a thread, if necessary, to run it.  (We cannot
     * prestart the thread to run the task because the task (probably)
     * shouldn't be run yet.)  If the pool is shut down while the task
     * is being added, cancel and remove it if required by state and
     * run-after-shutdown parameters.
     *
     * @param task the task
     */
    private void delayedExecute(RunnableScheduledFuture<?> task) {
        if (isShutdown())
            reject(task);
        else {  
            super.getQueue().add(task);  //獲取延遲隊列(DelayedWorkQueue)添加任務(wù),通過DelayedWorkQueue 延遲隊列實現(xiàn) offer獲取對象的延遲
            if (isShutdown() &&
                !canRunInCurrentRunState(task.isPeriodic()) &&
                remove(task))
                task.cancel(false);
            else
                ensurePrestart();
        }
    }
    /**
      確保有work執(zhí)行
     * Same as prestartCoreThread except arranges that at least one
     * thread is started even if corePoolSize is 0.
     */
    void ensurePrestart() {
        int wc = workerCountOf(ctl.get());
        if (wc < corePoolSize)
            addWorker(null, true);
        else if (wc == 0)
            addWorker(n ull, false);
    }
     //add 方法內(nèi)部
       public boolean offer(Runnable x) {
            if (x == null)
                throw new NullPointerException();
            RunnableScheduledFuture<?> e = (RunnableScheduledFuture<?>)x;
            final ReentrantLock lock = this.lock;
            lock.lock(); //加鎖。此處會阻塞
            try {
                int i = size;
                if (i >= queue.length) //擴(kuò)容
                    grow(); 
                size = i + 1;
                if (i == 0) {
                    queue[0] = e;
                    setIndex(e, 0);//第一個直接設(shè)置索引和下標(biāo)0
                } else {
                    siftUp(i, e); //排序算法對任務(wù)進(jìn)行排序 
                }
                if (queue[0] == e) {
                    leader = null;
                    available.signal(); //喚醒所有的被擠壓的wait線程
                }
            } finally {
                lock.unlock();
            }
            return true;
        }

 /**
         * Sifts element added at bottom up to its heap-ordered spot.
         * Call only when holding lock.
           二叉樹的堆排序算法
         */
        private void siftUp(int k, RunnableScheduledFuture<?> key) {
            while (k > 0) {
                int parent = (k - 1) >>> 1;
                RunnableScheduledFuture<?> e = queue[parent];
                if (key.compareTo(e) >= 0)
                    break;
                queue[k] = e;
                setIndex(e, k);
                k = parent;
            }
            queue[k] = key;
            setIndex(key, k);
        }

//work運行的時候調(diào)用queue的take方法
      public RunnableScheduledFuture<?> take() throws InterruptedException {
            final ReentrantLock lock = this.lock;
            lock.lockInterruptibly();
            try {
                for (;;) {
                    RunnableScheduledFuture<?> first = queue[0]; //獲取第一個對象
                    if (first == null)
                        available.await();
                    else {
                        long delay = first.getDelay(NANOSECONDS); //延遲時間
                        if (delay <= 0)
                            return finishPoll(first);
                        first = null; // don't retain ref while waiting
                        if (leader != null)
                            available.await(); ///因為沒有執(zhí)行線程初始化,所以等等什么時候有了自己被他人喚醒
                        else {
                            Thread thisThread = Thread.currentThread();
                            leader = thisThread;
                            try {
                                available.awaitNanos(delay);
                            } finally {
                                if (leader == thisThread)
                                    leader = null;
                            }
                        }
                    }
                }
            } finally {
                if (leader == null && queue[0] != null)
                    available.signal();
                lock.unlock();
            }
        }
        /**
         * Performs common bookkeeping for poll and take: Replaces
         * first element with last and sifts it down.  Call only when
         * holding lock.
         * @param f the task to remove and return
         */
        private RunnableScheduledFuture<?> finishPoll(RunnableScheduledFuture<?> f) {
            int s = --size;
            RunnableScheduledFuture<?> x = queue[s];  //重排序隊列
            queue[s] = null;
            if (s != 0)
                siftDown(0, x);
            setIndex(f, -1);
            return f;
        }

scheduleAtFixedRate 與 scheduleWithFixedDelay 的區(qū)別
scheduleAtFixedRate ,是以上一個任務(wù)開始的時間計時,period時間過去后,檢測上一個任務(wù)是否執(zhí)行完畢,如果上一個任務(wù)執(zhí)行完畢,則當(dāng)前任務(wù)立即執(zhí)行,如果上一個任務(wù)沒有執(zhí)行完畢,則需要等上一個任務(wù)執(zhí)行完畢后立即執(zhí)行。
scheduleWithFixedDelay,是以上一個任務(wù)結(jié)束時開始計時,period時間過去后,立即執(zhí)行。

合理配置線程池

要想合理地配置線程池,就必須首先分析任務(wù)特性,可以從以下幾個角度來分析。
任務(wù)的性質(zhì):CPU密集型任務(wù)、IO密集型任務(wù)和混合型任務(wù)
任務(wù)的優(yōu)先級:高中低
任務(wù)的執(zhí)行時間:長中短
任務(wù) 的依賴性;是否依賴其他系統(tǒng)資源
CPU密集型任務(wù)應(yīng)配置盡可能小的線程,如配置NCPU+1個線程的線程池,
IO密集型任務(wù)線程并不是一直在執(zhí)行任務(wù) ,則應(yīng)配置盡可能多的線程,如2*NCPU 。
可以通過Runtime.getRuntime().availableProcessors()方法獲得當(dāng)前設(shè)備的CPU個數(shù)
優(yōu)先級不同的任務(wù)可以使用優(yōu)先級隊列PriorityBlockingQueue來處理,它可以讓優(yōu)先級高的任務(wù)先執(zhí)行。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 阿呆的項目經(jīng)理給阿呆分配了一個統(tǒng)計點擊量的問題。情景是這樣的:每個廣告位上的創(chuàng)意都可以點擊,點擊過后會經(jīng)過服務(wù)器跳...
    等風(fēng)的豬_閱讀 737評論 0 3
  • 一,參數(shù) 1,corePoolSize:核心線程數(shù)量,線程池可以保留的活躍線程的數(shù)量。 2,maximumPool...
    Z_aa67閱讀 259評論 0 0
  • 線程池主要用來解決線程生命周期開銷問題和資源不足問題。通過對多個任務(wù)重復(fù)使用線程,線程創(chuàng)建的開銷就被分?jǐn)偟搅硕鄠€任...
    安仔夏天勤奮閱讀 1,121評論 0 10
  • 前言:線程是稀缺資源,如果被無限制的創(chuàng)建,不僅會消耗系統(tǒng)資源,還會降低系統(tǒng)的穩(wěn)定性,合理的使用線程池對線程進(jìn)行統(tǒng)一...
    SDY_0656閱讀 852評論 0 1
  • 我們平時寫文章喜歡就事論事,把中心思想確立之后,一針見血,處處都是干貨。 這樣雖然文章寫得比較緊湊,閱讀起來效率也...
    呱呱鳥閱讀 3,411評論 46 70

友情鏈接更多精彩內(nèi)容