Either
- public CompletableFuture<Void> acceptEither(CompletionStage<? extends T> other, Consumer<? super T> action)
- public CompletableFuture<Void> acceptEitherAsync(CompletionStage<? extends T> other, Consumer<? super T> action)
- public CompletableFuture<Void> acceptEitherAsync(CompletionStage<? extends T> other, Consumer<? super T> action, Executor executor)
- public <U> CompletableFuture<U> applyToEither(CompletionStage<? extends T> other, Function<? super T,U> fn)
- public <U> CompletableFuture<U> applyToEitherAsync(CompletionStage<? extends T> other, Function<? super T,U> fn)
- public <U> CompletableFuture<U> applyToEitherAsync(CompletionStage<? extends T> other, Function<? super T,U> fn, Executor executor)
acceptEither办法是当随便率性一?CompletionStage完成的时刻,action这个花费者就会被履行。这个办法返回CompletableFuture<Void>
applyToEither办法是当随便率性一?CompletionStage完成的时刻,fn会被履行,它的返回值会算作新的CompletableFuture<U>的计算结不雅。
下面这个例子有时会输出100,有时刻会输出200,哪个Future先完成就会根据它的结不雅计算。
- Random rand = new Random();
- CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
- try {
- Thread.sleep(10000 + rand.nextInt(1000));
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- return 100;
- });
- CompletableFuture<Integer> future2 = CompletableFuture.supplyAsync(() -> {
- try {
- Thread.sleep(10000 + rand.nextInt(1000));
- } catch (InterruptedException e) {
推荐阅读
【限时免费】岁尾最强一次云计算大年夜会,看传统、社区、互联网企业若何碰撞?【编辑推荐】2017 Android和iOS几回再三被曝Bug,它们都怎么了?谷歌苹不雅竟齐翻车!谈谈安卓8/iOS11那些BugiOS消息转发>>>详细阅读
地址:http://www.17bianji.com/lsqh/40058.html
1/2 1

网友点评
精彩导读
科技快报
品牌展示