Java8 CompletableFuture详解以及API part2

转载:CompletableFuture 使用详解

thenCombine 合并任务

thenCombine 会把 两个 CompletionStage 的任务都执行完成后,把两个任务的结果一块交给 thenCombine 来处理。

public <U,V> CompletionStage<V> thenCombine(CompletionStage<? extends U> other,BiFunction<? super T,? super U,? extends V> fn);
public <U,V> CompletionStage<V> thenCombineAsync(CompletionStage<? extends U> other,BiFunction<? super T,? super U,? extends V> fn);
public <U,V> CompletionStage<V> thenCombineAsync(CompletionStage<? extends U> other,BiFunction<? super T,? super U,? extends V> fn,Executor executor);

示例

private static void thenCombine() throws Exception {
    
    
    CompletableFuture<String> future1 = CompletableFuture.supplyAsync(new Supplier<String>() {
    
    
        @Override
        public String get() {
    
    
            return "hello";
        }
    });
    CompletableFuture<String> future2 = CompletableFuture.supplyAsync(new Supplier<String>() {
    
    
        @Override
        public String get() {
    
    
            return "hello";
        }
    });
    CompletableFuture<String> result = future1.thenCombine(future2, new BiFunction<String, String, String>() {
    
    
        @Override
        public String apply(String t, String u) {
    
    
            return t+" "+u;
        }
    });
    System.out.println(result.get());
}

thenAcceptBoth

当两个CompletionStage都执行完成后,把结果一块交给thenAcceptBoth来进行消耗

public <U> CompletionStage<Void> thenAcceptBoth(CompletionStage<? extends U> other,BiConsumer<? super T, ? super U> action);
public <U> CompletionStage<Void> thenAcceptBothAsync(CompletionStage<? extends U> other,BiConsumer<? super T, ? super U> action);
public <U> CompletionStage<Void> thenAcceptBothAsync(CompletionStage<? extends U> other,BiConsumer<? super T, ? super U> action,     Executor executor);

示例

private static void thenAcceptBoth() throws Exception {
    
    
    CompletableFuture<Integer> f1 = CompletableFuture.supplyAsync(new Supplier<Integer>() {
    
    
        @Override
        public Integer get() {
    
    
            int t = new Random().nextInt(3);
            try {
    
    
                TimeUnit.SECONDS.sleep(t);
            } catch (InterruptedException e) {
    
    
                e.printStackTrace();
            }
            System.out.println("f1="+t);
            return t;
        }
    });
        
    CompletableFuture<Integer> f2 = CompletableFuture.supplyAsync(new Supplier<Integer>() {
    
    
        @Override
        public Integer get() {
    
    
            int t = new Random().nextInt(3);
            try {
    
    
                TimeUnit.SECONDS.sleep(t);
            } catch (InterruptedException e) {
    
    
                e.printStackTrace();
            }
            System.out.println("f2="+t);
            return t;
        }
    });
    f1.thenAcceptBoth(f2, new BiConsumer<Integer, Integer>() {
    
    
        @Override
        public void accept(Integer t, Integer u) {
    
    
            System.out.println("f1="+t+";f2="+u+";");
        }
    });
}

applyToEither 方法

两个CompletionStage,谁执行返回的结果快,我就用那个CompletionStage的结果进行下一步的转化操作。

public <U> CompletionStage<U> applyToEither(CompletionStage<? extends T> other,Function<? super T, U> fn);
public <U> CompletionStage<U> applyToEitherAsync(CompletionStage<? extends T> other,Function<? super T, U> fn);
public <U> CompletionStage<U> applyToEitherAsync(CompletionStage<? extends T> other,Function<? super T, U> fn,Executor executor);

示例

private static void applyToEither() throws Exception {
    
    
    CompletableFuture<Integer> f1 = CompletableFuture.supplyAsync(new Supplier<Integer>() {
    
    
        @Override
        public Integer get() {
    
    
            int t = new Random().nextInt(3);
            try {
    
    
                TimeUnit.SECONDS.sleep(t);
            } catch (InterruptedException e) {
    
    
                e.printStackTrace();
            }
            System.out.println("f1="+t);
            return t;
        }
    });
    CompletableFuture<Integer> f2 = CompletableFuture.supplyAsync(new Supplier<Integer>() {
    
    
        @Override
        public Integer get() {
    
    
            int t = new Random().nextInt(3);
            try {
    
    
                TimeUnit.SECONDS.sleep(t);
            } catch (InterruptedException e) {
    
    
                e.printStackTrace();
            }
            System.out.println("f2="+t);
            return t;
        }
    });
    
    CompletableFuture<Integer> result = f1.applyToEither(f2, new Function<Integer, Integer>() {
    
    
        @Override
        public Integer apply(Integer t) {
    
    
            System.out.println(t);
            return t * 2;
        }
    });
System.out.println(result.get());
}

acceptEither 方法

两个CompletionStage,谁执行返回的结果快,我就用那个CompletionStage的结果进行下一步的消耗操作。

public CompletionStage<Void> acceptEither(CompletionStage<? extends T> other,Consumer<? super T> action);
public CompletionStage<Void> acceptEitherAsync(CompletionStage<? extends T> other,Consumer<? super T> action);
public CompletionStage<Void> acceptEitherAsync(CompletionStage<? extends T> other,Consumer<? super T> action,Executor executor);

示例

private static void acceptEither() throws Exception {
    
    
    CompletableFuture<Integer> f1 = CompletableFuture.supplyAsync(new Supplier<Integer>() {
    
    
        @Override
        public Integer get() {
    
    
            int t = new Random().nextInt(3);
            try {
    
    
                TimeUnit.SECONDS.sleep(t);
            } catch (InterruptedException e) {
    
    
                e.printStackTrace();
            }
            System.out.println("f1="+t);
            return t;
        }
    });
        
    CompletableFuture<Integer> f2 = CompletableFuture.supplyAsync(new Supplier<Integer>() {
    
    
        @Override
        public Integer get() {
    
    
            int t = new Random().nextInt(3);
            try {
    
    
                TimeUnit.SECONDS.sleep(t);
            } catch (InterruptedException e) {
    
    
                e.printStackTrace();
            }
            System.out.println("f2="+t);
            return t;
        }
    });
    f1.acceptEither(f2, new Consumer<Integer>() {
    
    
        @Override
        public void accept(Integer t) {
    
    
            System.out.println(t);
        }
    });
}

runAfterEither 方法

两个CompletionStage,任何一个完成了都会执行下一步的操作(Runnable)

public CompletionStage<Void> runAfterEither(CompletionStage<?> other,Runnable action);
public CompletionStage<Void> runAfterEitherAsync(CompletionStage<?> other,Runnable action);
public CompletionStage<Void> runAfterEitherAsync(CompletionStage<?> other,Runnable action,Executor executor);

示例

private static void runAfterEither() throws Exception {
    
    
    CompletableFuture<Integer> f1 = CompletableFuture.supplyAsync(new Supplier<Integer>() {
    
    
        @Override
        public Integer get() {
    
    
            int t = new Random().nextInt(3);
            try {
    
    
                TimeUnit.SECONDS.sleep(t);
            } catch (InterruptedException e) {
    
    
                e.printStackTrace();
            }
            System.out.println("f1="+t);
            return t;
        }
    });
        
    CompletableFuture<Integer> f2 = CompletableFuture.supplyAsync(new Supplier<Integer>() {
    
    
        @Override
        public Integer get() {
    
    
            int t = new Random().nextInt(3);
            try {
    
    
                TimeUnit.SECONDS.sleep(t);
            } catch (InterruptedException e) {
    
    
                e.printStackTrace();
            }
            System.out.println("f2="+t);
            return t;
        }
    });
    f1.runAfterEither(f2, new Runnable() {
    
    
        
        @Override
        public void run() {
    
    
            System.out.println("上面有一个已经完成了。");
        }
    });
}

runAfterBoth

两个CompletionStage,都完成了计算才会执行下一步的操作(Runnable)

public CompletionStage<Void> runAfterBoth(CompletionStage<?> other,Runnable action);
public CompletionStage<Void> runAfterBothAsync(CompletionStage<?> other,Runnable action);
public CompletionStage<Void> runAfterBothAsync(CompletionStage<?> other,Runnable action,Executor executor);

示例

private static void runAfterBoth() throws Exception {
    
    
    CompletableFuture<Integer> f1 = CompletableFuture.supplyAsync(new Supplier<Integer>() {
    
    
        @Override
        public Integer get() {
    
    
            int t = new Random().nextInt(3);
            try {
    
    
                TimeUnit.SECONDS.sleep(t);
            } catch (InterruptedException e) {
    
    
                e.printStackTrace();
            }
            System.out.println("f1="+t);
            return t;
        }
    });
        
    CompletableFuture<Integer> f2 = CompletableFuture.supplyAsync(new Supplier<Integer>() {
    
    
        @Override
        public Integer get() {
    
    
            int t = new Random().nextInt(3);
            try {
    
    
                TimeUnit.SECONDS.sleep(t);
            } catch (InterruptedException e) {
    
    
                e.printStackTrace();
            }
            System.out.println("f2="+t);
            return t;
        }
    });
    f1.runAfterBoth(f2, new Runnable() {
    
    
        
        @Override
        public void run() {
    
    
            System.out.println("上面两个任务都执行完成了。");
        }
    });
}

thenCompose 方法

thenCompose 方法允许你对两个 CompletionStage 进行流水线操作,第一个操作完成时,将其结果作为参数传递给第二个操作。

public <U> CompletableFuture<U> thenCompose(Function<? super T, ? extends CompletionStage<U>> fn);
public <U> CompletableFuture<U> thenComposeAsync(Function<? super T, ? extends CompletionStage<U>> fn) ;
public <U> CompletableFuture<U> thenComposeAsync(Function<? super T, ? extends CompletionStage<U>> fn, Executor executor) ;

示例

private static void thenCompose() throws Exception {
    
    
        CompletableFuture<Integer> f = CompletableFuture.supplyAsync(new Supplier<Integer>() {
    
    
            @Override
            public Integer get() {
    
    
                int t = new Random().nextInt(3);
                System.out.println("t1="+t);
                return t;
            }
        }).thenCompose(new Function<Integer, CompletionStage<Integer>>() {
    
    
            @Override
            public CompletionStage<Integer> apply(Integer param) {
    
    
                return CompletableFuture.supplyAsync(new Supplier<Integer>() {
    
    
                    @Override
                    public Integer get() {
    
    
                        int t = param *2;
                        System.out.println("t2="+t);
                        return t;
                    }
                });
            }
            
        });
        System.out.println("thenCompose result : "+f.get());
    }

猜你喜欢

转载自blog.csdn.net/weixin_38370441/article/details/112330655