可以看到supplyAsync方法执行速度慢的话thenApply方法执行线程和supplyAsync执行线程相同,如果supplyAsync方法执行速度快的话,那么thenApply方法执行线程和Main方法执行线程相同 。
组合CompletableFuture将两个CompletableFuture组合到一起有两个方法
- thenCompose():当第一个任务完成时才会执行第二个操作
- thenCombine():两个异步任务全部完成时才会执行某些操作
public static CompletableFuture<String> getTastOne(){ return CompletableFuture.supplyAsync(()-> "topOne");}public static CompletableFuture<String> getTastTwo(String s){ return CompletableFuture.supplyAsync(()-> s + " topTwo");}我们利用thenCompose()方法进行编写CompletableFuture<String> thenComposeComplet = getTastOne().thenCompose(s -> getTastTwo(s));System.out.println(thenComposeComplet.get());输出就是topOne topTwo如果还记得前面的thenApply()方法的话,应该会想这个利用thenApply()方法也是能够实现类似的功能的 。//thenApplyCompletableFuture<CompletableFuture<String>> thenApply = getTastOne() .thenApply(s -> getTastTwo(s));System.out.println(thenApply.get().get());但是我们发现返回值是嵌套返回的一个类型,而想要获得最终的返回值需要调用两次get()thenCombine() 用法例如我们此时需要计算两个异步方法返回值的和 。求和这个操作是必须是两个异步方法得出来值的情况下才能进行计算,因此我们可以用thenCombine()方法进行计算 。
CompletableFuture<Integer> thenComposeone = CompletableFuture.supplyAsync(() -> 192);CompletableFuture<Integer> thenComposeTwo = CompletableFuture.supplyAsync(() -> 196);CompletableFuture<Integer> thenComposeCount = thenComposeOne .thenCombine(thenComposeTwo, (s, y) -> s + y);System.out.println(thenComposeCount.get());此时thenComposeOne和thenComposeTwo都完成时才会调用传给thenCombine方法的回调函数 。组合多个CompletableFuture在上面我们用thenCompose()和thenCombine()两个方法将两个CompletableFuture组装起来,如果我们想要将任意数量的CompletableFuture组合起来呢?可以使用下面两个方法进行组合 。
- allOf():等待所有CompletableFuture完后以后才会运行回调函数
- anyOf():只要其中一个CompletableFuture完成,那么就会执行回调函数 。注意此时其他的任务也就不执行了 。
//allOf()CompletableFuture<Integer> one = CompletableFuture.supplyAsync(() -> 1);CompletableFuture<Integer> two = CompletableFuture.supplyAsync(() -> 2);CompletableFuture<Integer> three = CompletableFuture.supplyAsync(() -> 3);CompletableFuture<Integer> four = CompletableFuture.supplyAsync(() -> 4);CompletableFuture<Integer> five = CompletableFuture.supplyAsync(() -> 5);CompletableFuture<Integer> six = CompletableFuture.supplyAsync(() -> 6);CompletableFuture<Void> voidCompletableFuture = CompletableFuture.allOf(one, two, three, four, five, six);voidCompletableFuture.thenApply(v->{ return Stream.of(one,two,three,four, five, six) .map(CompletableFuture::join) .collect(Collectors.toList());}).thenAccept(System.out::println);CompletableFuture<Void> voidCompletableFuture1 = CompletableFuture.runAsync(() -> { try { Thread.sleep(1000); } catch (Exception e) { } System.out.println("1");});我们定义了6个CompletableFuture等待所有的CompletableFuture等待所有任务完成以后然后将其值输出 。anyOf()的用法
CompletableFuture<Void> voidCompletableFuture1 = CompletableFuture.runAsync(() -> {try { Thread.sleep(1000);} catch (Exception e) {}System.out.println("voidCompletableFuture1");});CompletableFuture<Void> voidCompletableFutur2 = CompletableFuture.runAsync(() -> {try { Thread.sleep(2000);} catch (Exception e) {}System.out.println("voidCompletableFutur2");});CompletableFuture<Void> voidCompletableFuture3 = CompletableFuture.runAsync(() -> {try { Thread.sleep(3000);} catch (Exception e) {}System.out.println("voidCompletableFuture3");});CompletableFuture<Object> objectCompletableFuture = CompletableFuture .anyOf(voidCompletableFuture1, voidCompletableFutur2, voidCompletableFuture3);objectCompletableFuture.get();
推荐阅读
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- 开源JavaScript实用日期处理库——date-fns
- 在浏览器中异步下载文件监听下载进度
- SpringMVC:进阶
- EditPlus——一款小巧功能强大的老牌代码文本编辑器
- 程序员必备技能:设计模式之——组合模式
- CSS样式更改——字体设置Font&边框Border
- 一文全解析——APP版本管理基本知识
- CSS样式更改——文本Content
- 一个漂亮的开源HTML5音乐播放器——APlayer
- 万兆电口网卡体验——安装篇
