CompletableFuture

CompletableFuture


簡介

擴(kuò)展 Future 功能舱权,通過 CompletionStage 提供函數(shù)式編程的能力,簡化異步編程

基本用法

  • CompletableFuture ,實現(xiàn)了 Future<T>, CompletionStage<T> 兩個接口。
  • Executor 參數(shù)可以手動指定線程池,否則默認(rèn) ForkJoinPool.commonPool() 系統(tǒng)級公共線程池。這些線程都是守護(hù)線程,主線程結(jié)束守護(hù)線程不結(jié)束势就,只有JVM關(guān)閉時,生命周期終止脉漏。
  • 以 Async 結(jié)尾的方法都是異步執(zhí)行苞冯,即到線程池取個新線程執(zhí)行

創(chuàng)建

  • supplyAsync
    創(chuàng)建有返回值的任務(wù) 參數(shù) Supplier
  • runAsync
    建沒有返回值的任務(wù) 參數(shù) Runnable
  • completedFuture
    創(chuàng)建已完成的任務(wù) 參數(shù)為值

消費(fèi)

  • whenComplete whenCompleteAsync
    消費(fèi)任務(wù)結(jié)果 參數(shù) BiConsumer
  • exceptionally
    消費(fèi)任務(wù)異常 參數(shù) Function
  • handle handleAsync
    消費(fèi)任務(wù)結(jié)果和異常 參數(shù) BiFunction

變換

  • thenApply thenApplyAsync
    有入?yún)⒂蟹祷刂?參數(shù) Function

  • thenAccept thenAcceptAsync
    有入?yún)o返回值 參數(shù) Consumer

  • thenRun thenRunAsync
    無入?yún)o返回值 參數(shù) Runnable

  • thenCompose thenComposeAsync
    構(gòu)成新任務(wù) 參數(shù) Function

組合

  • thenCombine thenCombineAsync
    雙線有入?yún)⒂蟹祷刂?參數(shù) CompletionStage BiFunction

  • thenAcceptBoth thenAcceptBothAsync
    雙線有入?yún)o返回值 參數(shù) CompletionStage BiConsumer

  • runAfterBoth runAfterBothAsync
    雙線無入?yún)o返回值 參數(shù) CompletionStage Runnable

  • applyToEither applyToEitherAsync
    二選快有入?yún)⒂蟹祷刂?參數(shù) CompletionStage Function

  • acceptEither acceptEitherAsync
    二選快有入?yún)o返回值 參數(shù) CompletionStage Consumer

  • runAfterEither runAfterEitherAsync
    二選快無入?yún)o返回值 參數(shù) CompletionStage Runnable

群體

  • anyOf
    只要有完成即觸發(fā)有返回值 參數(shù) CompletableFuture...
  • allOf
    全部完成才觸發(fā)無返回值,可接收異常 參數(shù) CompletableFuture...
fun testAnyOfAndAllOf() {
    val f1: CompletableFuture<String> = CompletableFuture.supplyAsync {
        TimeUnit.SECONDS.sleep(2)
        return@supplyAsync Thread.currentThread().name
    }.whenComplete { t, u ->
        println("t1: ${System.currentTimeMillis()}: ${Thread.currentThread().name}: $t $u")
    }

    val f2: CompletableFuture<String> = CompletableFuture.supplyAsync {
        TimeUnit.SECONDS.sleep(1)
        return@supplyAsync Thread.currentThread().name
    }.whenCompleteAsync { t, u ->
        println("t2: ${System.currentTimeMillis()}: ${Thread.currentThread().name}: $t $u")
    }

    CompletableFuture.anyOf(f1, f2)
        .whenComplete { t, u ->
            println("anyOf: ${System.currentTimeMillis()}: ${Thread.currentThread().name}: $t $u")
        }

    CompletableFuture.allOf(f1, f2)
        .whenComplete { t, u ->
            println("allOf: ${System.currentTimeMillis()}: ${Thread.currentThread().name}: $t $u")
        }.join()
}
t2: 1550851312825: ForkJoinPool.commonPool-worker-2: ForkJoinPool.commonPool-worker-2 null
anyOf: 1550851312825: ForkJoinPool.commonPool-worker-2: ForkJoinPool.commonPool-worker-2 null
t1: 1550851313822: ForkJoinPool.commonPool-worker-1: ForkJoinPool.commonPool-worker-1 null
allOf: 1550851313822: ForkJoinPool.commonPool-worker-1: null null

完成

  • complete completeExceptionally cancel
    觸發(fā)完成
  • isDone isCompletedExceptionally isCancelled
    判斷任務(wù)是否已經(jīng)結(jié)束

獲取結(jié)果

  • get
    阻塞等待鸠删,獲取任務(wù)結(jié)果抱完,會拋出 ExecutionException InterruptedException CancellationException
  • join
    阻塞等待,獲取任務(wù)結(jié)果刃泡,異常 CompletionException CancellationException
  • getNow
    立即取到結(jié)果巧娱,任務(wù)未結(jié)束則返回傳入?yún)?shù)碉怔,異常 CompletionException CancellationException

切換線程

使用 Executor 切換執(zhí)行任務(wù)線程,例如在 Android 中在主線程和子線程切換

fun testExecutor() {
    CompletableFuture.supplyAsync(Supplier {
        TimeUnit.SECONDS.sleep(2)
        println("t1: ${Thread.currentThread().name}") // t1: pool-1-thread-1
        return@Supplier "t1"
    }, AppExecutors.io).thenApplyAsync(Function<String, String> {
        println("t2: ${Thread.currentThread().name}") // t2: main
        return@Function "$it.t2"
    }, AppExecutors.main).thenApplyAsync(Function<String, String> {
        println("t3: ${Thread.currentThread().name}") // t3: pool-2-thread-1
        return@Function "$it.t3"
    }, AppExecutors.single).whenCompleteAsync(BiConsumer { t, u ->
        println("t4: ${Thread.currentThread().name}: $t $u") // t4: main: t1.t2.t3 null
    }, AppExecutors.main)
}

object AppExecutors {
    val io: ExecutorService by lazy {
        Executors.newCachedThreadPool()
    }

    val compute: ExecutorService by lazy {
        val threadCount = 3
        Executors.newFixedThreadPool(threadCount)
    }

    val single: ExecutorService by lazy {
        Executors.newSingleThreadExecutor()
    }

    val main: Executor by lazy {
        MainThreadExecutor()
    }

    private class MainThreadExecutor : Executor {
        private val mainThreadHandler: Handler = Handler(Looper.getMainLooper())

        override fun execute(command: Runnable?) {
            mainThreadHandler.post(command);
        }
    }
}

參考

CompletableFuture
Java8新特性整理之CompletableFuture:組合式禁添、異步編程(七)
CompletableFuture 使用詳解
個人愚見:分布式框架中使用CompletableFuture提高效率
Java并發(fā)編程系列一:Future和CompletableFuture解析與使用

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末撮胧,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子老翘,更是在濱河造成了極大的恐慌芹啥,老刑警劉巖,帶你破解...
    沈念sama閱讀 211,743評論 6 492
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件铺峭,死亡現(xiàn)場離奇詭異墓怀,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)卫键,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,296評論 3 385
  • 文/潘曉璐 我一進(jìn)店門傀履,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人莉炉,你說我怎么就攤上這事钓账。” “怎么了絮宁?”我有些...
    開封第一講書人閱讀 157,285評論 0 348
  • 文/不壞的土叔 我叫張陵梆暮,是天一觀的道長。 經(jīng)常有香客問我绍昂,道長啦粹,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 56,485評論 1 283
  • 正文 為了忘掉前任治专,我火速辦了婚禮卖陵,結(jié)果婚禮上遭顶,老公的妹妹穿的比我還像新娘张峰。我一直安慰自己,他們只是感情好棒旗,可當(dāng)我...
    茶點(diǎn)故事閱讀 65,581評論 6 386
  • 文/花漫 我一把揭開白布喘批。 她就那樣靜靜地躺著,像睡著了一般铣揉。 火紅的嫁衣襯著肌膚如雪饶深。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 49,821評論 1 290
  • 那天逛拱,我揣著相機(jī)與錄音敌厘,去河邊找鬼。 笑死朽合,一個胖子當(dāng)著我的面吹牛俱两,可吹牛的內(nèi)容都是我干的饱狂。 我是一名探鬼主播,決...
    沈念sama閱讀 38,960評論 3 408
  • 文/蒼蘭香墨 我猛地睜開眼宪彩,長吁一口氣:“原來是場噩夢啊……” “哼休讳!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起尿孔,我...
    開封第一講書人閱讀 37,719評論 0 266
  • 序言:老撾萬榮一對情侶失蹤俊柔,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后活合,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體雏婶,經(jīng)...
    沈念sama閱讀 44,186評論 1 303
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,516評論 2 327
  • 正文 我和宋清朗相戀三年白指,在試婚紗的時候發(fā)現(xiàn)自己被綠了尚骄。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 38,650評論 1 340
  • 序言:一個原本活蹦亂跳的男人離奇死亡侵续,死狀恐怖倔丈,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情状蜗,我是刑警寧澤需五,帶...
    沈念sama閱讀 34,329評論 4 330
  • 正文 年R本政府宣布,位于F島的核電站轧坎,受9級特大地震影響宏邮,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜缸血,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,936評論 3 313
  • 文/蒙蒙 一蜜氨、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧捎泻,春花似錦飒炎、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,757評論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至闯狱,卻和暖如春煞赢,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背哄孤。 一陣腳步聲響...
    開封第一講書人閱讀 31,991評論 1 266
  • 我被黑心中介騙來泰國打工照筑, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 46,370評論 2 360
  • 正文 我出身青樓凝危,卻偏偏與公主長得像饭弓,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子媒抠,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 43,527評論 2 349

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