ExecutorService 的 Future 執(zhí)行分析

ExecutorService executorService = new ThreadPoolExecutor(0,
2000, 60L , TimeUnit.SECONDS, new SynchronousQueue<Runnable>());

0: corePoolSize the number of threads to keep in the pool, even if they are idle, unless {@code allowCoreThreadTimeOut} is set
2000: the maximum number of threads to allow in the pool
60L: keepAliveTime when the number of threads is greater than the core, this is the maximum time that excess idle threads will wait for new tasks before terminating. Timeout in nanoseconds for idle threads waiting for work. Threads use this timeout when there are more than corePoolSize present or if allowCoreThreadTimeOut. Otherwise they wait forever for new work.

默認線程池里面沒有任何線程问顷,最多允許生成2000個線程竹挡,而且線程最多空閑60s

Future future = executorService.submit(() -> {Thread.sleep(1000)});
Object a = future.get()

Future 實際類型是FutureTask
/**
* @throws CancellationException {@inheritDoc}
*/
public V get() throws InterruptedException, ExecutionException {
int s = state;
if (s <= COMPLETING)
s = awaitDone(false, 0L);
return report(s);
}

FutureTask初始的時候state是NEW
/**
* Creates a {@code FutureTask} that will, upon running, execute the
* given {@code Callable}.
*
* @param callable the callable task
* @throws NullPointerException if the callable is null
*/
public FutureTask(Callable<V> callable) {
if (callable == null)
throw new NullPointerException();
this.callable = callable;
this.state = NEW; // ensure visibility of callable
}

state取值為
private volatile int state;
private static final int NEW = 0;
private static final int COMPLETING = 1;
private static final int NORMAL = 2;
private static final int EXCEPTIONAL = 3;
private static final int CANCELLED = 4;
private static final int INTERRUPTING = 5;
private static final int INTERRUPTED = 6;

awaitDone等待直到異步任務執(zhí)行完

/**
 * Awaits completion or aborts on interrupt or timeout.
 *
 * @param timed true if use timed waits
 * @param nanos time to wait, if timed
 * @return state upon completion
 */
private int awaitDone(boolean timed, long nanos)
    throws InterruptedException {
    final long deadline = timed ? System.nanoTime() + nanos : 0L; //明顯這里會是0
    WaitNode q = null;
    boolean queued = false;
    for (;;) {
        if (Thread.interrupted()) {
            removeWaiter(q);
            throw new InterruptedException();
        }

        int s = state;
        if (s > COMPLETING) {
            if (q != null)
                q.thread = null;
            return s;
        }
        else if (s == COMPLETING) // cannot time out yet
            Thread.yield();
        else if (q == null)
            q = new WaitNode();
        else if (!queued)
            queued = UNSAFE.compareAndSwapObject(this, waitersOffset,
                                                 q.next = waiters, q);
        else if (timed) {
            nanos = deadline - System.nanoTime();
            if (nanos <= 0L) {
                removeWaiter(q);
                return state;
            }
            LockSupport.parkNanos(this, nanos);
        }
        else
            LockSupport.park(this);
    }
}

初次進來會進入
else if (q == null)
q = new WaitNode();

創(chuàng)建了一個WaitNode幅恋。

第二次進來
else if (!queued)
queued = UNSAFE.compareAndSwapObject(this, waitersOffset,
q.next = waiters, q);

waitersOffset = UNSAFE.objectFieldOffset (k.getDeclaredField("waiters"));

第三次進來
會進入
LockSupport.park(this); 線程會掛起腺律,掛起這個主線程雌桑,這也就是為什么調用get的時候會等待

線程池調度可以看java.util.concurrent.ThreadPoolExecutor

?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市硅堆,隨后出現(xiàn)的幾起案子扭勉,更是在濱河造成了極大的恐慌皂冰,老刑警劉巖店展,帶你破解...
    沈念sama閱讀 218,941評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異秃流,居然都是意外死亡赂蕴,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,397評論 3 395
  • 文/潘曉璐 我一進店門舶胀,熙熙樓的掌柜王于貴愁眉苦臉地迎上來概说,“玉大人,你說我怎么就攤上這事嚣伐》粗啵” “怎么了策严?”我有些...
    開封第一講書人閱讀 165,345評論 0 356
  • 文/不壞的土叔 我叫張陵柄冲,是天一觀的道長烦感。 經(jīng)常有香客問我,道長船万,這世上最難降的妖魔是什么刻撒? 我笑而不...
    開封第一講書人閱讀 58,851評論 1 295
  • 正文 為了忘掉前任骨田,我火速辦了婚禮耿导,結果婚禮上,老公的妹妹穿的比我還像新娘态贤。我一直安慰自己舱呻,他們只是感情好,可當我...
    茶點故事閱讀 67,868評論 6 392
  • 文/花漫 我一把揭開白布悠汽。 她就那樣靜靜地躺著箱吕,像睡著了一般。 火紅的嫁衣襯著肌膚如雪柿冲。 梳的紋絲不亂的頭發(fā)上茬高,一...
    開封第一講書人閱讀 51,688評論 1 305
  • 那天,我揣著相機與錄音假抄,去河邊找鬼怎栽。 笑死丽猬,一個胖子當著我的面吹牛,可吹牛的內(nèi)容都是我干的熏瞄。 我是一名探鬼主播脚祟,決...
    沈念sama閱讀 40,414評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼强饮!你這毒婦竟也來了由桌?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 39,319評論 0 276
  • 序言:老撾萬榮一對情侶失蹤邮丰,失蹤者是張志新(化名)和其女友劉穎行您,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體剪廉,經(jīng)...
    沈念sama閱讀 45,775評論 1 315
  • 正文 獨居荒郊野嶺守林人離奇死亡邑雅,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,945評論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了妈经。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片淮野。...
    茶點故事閱讀 40,096評論 1 350
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖吹泡,靈堂內(nèi)的尸體忽然破棺而出骤星,到底是詐尸還是另有隱情,我是刑警寧澤爆哑,帶...
    沈念sama閱讀 35,789評論 5 346
  • 正文 年R本政府宣布洞难,位于F島的核電站,受9級特大地震影響揭朝,放射性物質發(fā)生泄漏队贱。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,437評論 3 331
  • 文/蒙蒙 一潭袱、第九天 我趴在偏房一處隱蔽的房頂上張望柱嫌。 院中可真熱鬧,春花似錦屯换、人聲如沸编丘。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,993評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽嘉抓。三九已至,卻和暖如春晕窑,著一層夾襖步出監(jiān)牢的瞬間抑片,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,107評論 1 271
  • 我被黑心中介騙來泰國打工杨赤, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留敞斋,地道東北人级遭。 一個月前我還...
    沈念sama閱讀 48,308評論 3 372
  • 正文 我出身青樓,卻偏偏與公主長得像渺尘,于是被迫代替她去往敵國和親挫鸽。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 45,037評論 2 355

推薦閱讀更多精彩內(nèi)容