線程池如何重用現(xiàn)有線程

聲明:此文章是從各個博客整理過來的嗅蔬,如有侵權(quán)請留言谋梭。

Java的Executors類和newCachedThreadPool( )方法类少,根據(jù)API,生成的線程池將重新使用現(xiàn)有Thread對象進(jìn)行新任務(wù)羹唠。

這是如何實(shí)現(xiàn)的奕枢,因?yàn)槲覠o法在API中找到任何方法來設(shè)置現(xiàn)有對象的行為Thread。

例如佩微,可以從一個Runnable對象創(chuàng)建一個新的 Thread缝彬,這使得Thread調(diào)用Runnable的run( )方法。但是哺眯,API中沒有使用setter方法Runnable作為參數(shù)谷浅。

看關(guān)于ThreadPoolExecutor參數(shù)時,看到了keepaliveTime這個參數(shù)奶卓,這個參數(shù)的意思是:“當(dāng)線程數(shù)大于CorePoolSize時一疯,如果有沒有等到新的Task,到了keepaliveTime時間后夺姑,就自動終止掉”墩邀。那么如果在這個時間之前,等到了新的Task盏浙,就可以重用這個線程眉睹。到底是怎么重用線程的呢?

線程重用的核心是只盹,我們知道辣往,Thread.start()只能調(diào)用一次,一旦這個調(diào)用結(jié)束殖卑,則該線程就到了stop狀態(tài)站削,不能再次調(diào)用start。
則要達(dá)到復(fù)用的目的孵稽,則必須從Runnable接口的run()方法上入手许起,可以這樣設(shè)計(jì)這個Runnable.run()方法(就叫外面的run()方法):
它本質(zhì)上是個無限循環(huán),跑的過程中不斷檢查我們是否有新加入的子Runnable對象(就叫內(nèi)部的runnable:run()吧菩鲜,它就是用來實(shí)現(xiàn)我們自己的任務(wù))园细,有就調(diào)一下我們的run(),其實(shí)就一個大run()把其它小run()#1,run()#2,...給串聯(lián)起來了接校,基本原理就這么簡單
不停地處理我們提交的Runnable任務(wù)猛频。

public void run() {
    while(true) {
        if(tasks available) {
           Runnable task = taskqueue.dequeue();
           task.run();
        } else {
           // wait or whatever
        }
    }
}

jdk節(jié)選

  /**
     * Main worker run loop.  Repeatedly gets tasks from queue and
     * executes them, while coping with a number of issues:
     *
     * 1. We may start out with an initial task, in which case we
     * don't need to get the first one. Otherwise, as long as pool is
     * running, we get tasks from getTask. If it returns null then the
     * worker exits due to changed pool state or configuration
     * parameters.  Other exits result from exception throws in
     * external code, in which case completedAbruptly holds, which
     * usually leads processWorkerExit to replace this thread.
     *
     * 2. Before running any task, the lock is acquired to prevent
     * other pool interrupts while the task is executing, and then we
     * ensure that unless pool is stopping, this thread does not have
     * its interrupt set.
     *
     * 3. Each task run is preceded by a call to beforeExecute, which
     * might throw an exception, in which case we cause thread to die
     * (breaking loop with completedAbruptly true) without processing
     * the task.
     *
     * 4. Assuming beforeExecute completes normally, we run the task,
     * gathering any of its thrown exceptions to send to afterExecute.
     * We separately handle RuntimeException, Error (both of which the
     * specs guarantee that we trap) and arbitrary Throwables.
     * Because we cannot rethrow Throwables within Runnable.run, we
     * wrap them within Errors on the way out (to the thread's
     * UncaughtExceptionHandler).  Any thrown exception also
     * conservatively causes thread to die.
     *
     * 5. After task.run completes, we call afterExecute, which may
     * also throw an exception, which will also cause thread to
     * die. According to JLS Sec 14.20, this exception is the one that
     * will be in effect even if task.run throws.
     *
     * The net effect of the exception mechanics is that afterExecute
     * and the thread's UncaughtExceptionHandler have as accurate
     * information as we can provide about any problems encountered by
     * user code.
     *
     * @param w the worker
     */
    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);
        }
    }
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市蛛勉,隨后出現(xiàn)的幾起案子鹿寻,更是在濱河造成了極大的恐慌,老刑警劉巖诽凌,帶你破解...
    沈念sama閱讀 217,734評論 6 505
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件毡熏,死亡現(xiàn)場離奇詭異,居然都是意外死亡侣诵,警方通過查閱死者的電腦和手機(jī)痢法,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,931評論 3 394
  • 文/潘曉璐 我一進(jìn)店門狱窘,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人财搁,你說我怎么就攤上這事蘸炸。” “怎么了妇拯?”我有些...
    開封第一講書人閱讀 164,133評論 0 354
  • 文/不壞的土叔 我叫張陵幻馁,是天一觀的道長。 經(jīng)常有香客問我越锈,道長仗嗦,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,532評論 1 293
  • 正文 為了忘掉前任甘凭,我火速辦了婚禮稀拐,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘丹弱。我一直安慰自己德撬,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,585評論 6 392
  • 文/花漫 我一把揭開白布躲胳。 她就那樣靜靜地躺著蜓洪,像睡著了一般。 火紅的嫁衣襯著肌膚如雪坯苹。 梳的紋絲不亂的頭發(fā)上隆檀,一...
    開封第一講書人閱讀 51,462評論 1 302
  • 那天,我揣著相機(jī)與錄音粹湃,去河邊找鬼恐仑。 笑死,一個胖子當(dāng)著我的面吹牛为鳄,可吹牛的內(nèi)容都是我干的裳仆。 我是一名探鬼主播,決...
    沈念sama閱讀 40,262評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼孤钦,長吁一口氣:“原來是場噩夢啊……” “哼歧斟!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起偏形,我...
    開封第一講書人閱讀 39,153評論 0 276
  • 序言:老撾萬榮一對情侶失蹤静袖,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后壳猜,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體勾徽,經(jīng)...
    沈念sama閱讀 45,587評論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡滑凉,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,792評論 3 336
  • 正文 我和宋清朗相戀三年统扳,在試婚紗的時候發(fā)現(xiàn)自己被綠了喘帚。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,919評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡咒钟,死狀恐怖吹由,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情朱嘴,我是刑警寧澤倾鲫,帶...
    沈念sama閱讀 35,635評論 5 345
  • 正文 年R本政府宣布,位于F島的核電站萍嬉,受9級特大地震影響乌昔,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜壤追,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,237評論 3 329
  • 文/蒙蒙 一磕道、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧行冰,春花似錦溺蕉、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,855評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至肛走,卻和暖如春漓雅,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背羹与。 一陣腳步聲響...
    開封第一講書人閱讀 32,983評論 1 269
  • 我被黑心中介騙來泰國打工故硅, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人纵搁。 一個月前我還...
    沈念sama閱讀 48,048評論 3 370
  • 正文 我出身青樓吃衅,卻偏偏與公主長得像,于是被迫代替她去往敵國和親腾誉。 傳聞我的和親對象是個殘疾皇子徘层,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,864評論 2 354