CompletableFuture详解~thenApply
生活随笔
收集整理的這篇文章主要介紹了
CompletableFuture详解~thenApply
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
在前一個(gè)階段上應(yīng)用函數(shù)
下面這個(gè)例子使用前面 #1 的完成的CompletableFuture, #1返回結(jié)果為字符串message,然后應(yīng)用一個(gè)函數(shù)把它變成大寫(xiě)字母。
static void thenApplyExample() {CompletableFuture cf = CompletableFuture.completedFuture("message").thenApply(s -> {assertFalse(Thread.currentThread().isDaemon());return s.toUpperCase();});assertEquals("MESSAGE", cf.getNow(null)); }注意thenApply方法名稱代表的行為。
then意味著這個(gè)階段的動(dòng)作發(fā)生當(dāng)前的階段正常完成之后。本例中,當(dāng)前節(jié)點(diǎn)完成,返回字符串message。
Apply意味著返回的階段將會(huì)對(duì)結(jié)果前一階段的結(jié)果應(yīng)用一個(gè)函數(shù)。
函數(shù)的執(zhí)行會(huì)被阻塞,這意味著getNow()只有大寫(xiě)操作被完成后才返回。
總結(jié)
以上是生活随笔為你收集整理的CompletableFuture详解~thenApply的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: linux python 永久添加自己的
- 下一篇: CompletableFuture详解~