多线程之CompletableFuture(下)

thenAcceptBothAsync

thenAcceptBothAsync(CompletionStage<? extends U> other,
        BiConsumer<? super T, ? super U> action)

同时执行两个线程,获取两个返回结果T,U作为参数,进入BiConsumer<T, U>,retrun Void,返回 CompletableFuture<Void>

public static void main(String[] args) {
        CompletableFuture<Void> voidCompletableFuture = CompletableFuture.supplyAsync(() -> {
            System.out.println(Thread.currentThread().getName() + "---------supplyAsync1---------开始---");
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (Exception e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName() + "---------supplyAsync1---------结束---");
            return "hello";
        }).thenAcceptBothAsync(CompletableFuture.supplyAsync(() -> {
            System.out.println(Thread.currentThread().getName() + "---------supplyAsync2---------开始---");
            System.out.println(Thread.currentThread().getName() + "---------supplyAsync2---------结束---");
            return "hello".length();
        }), (t, v) -> {
            try {
                TimeUnit.SECONDS.sleep(5);
            } catch (Exception e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName() + "........." + t + "---" + v);
        });
        System.out.println("---------------------------");
        voidCompletableFuture.join();
    }

这里写图片描述


acceptEitherAsync

acceptEitherAsync(
        CompletionStage<? extends T> other, Consumer<? super T> action)

获取快的线程的结果(两个线程返回类型需一致)T,T作为参数,进入Consumer,return void,返回CompletableFuture<Void>

public static void main(String[] args) {
        CompletableFuture<Void> voidCompletableFuture = CompletableFuture.supplyAsync(() -> {
            System.out.println(Thread.currentThread().getName() + "---------supplyAsync1---------开始---");
            System.out.println(Thread.currentThread().getName() + "---------supplyAsync1---------结束---");
            return "hello1";
        }).acceptEitherAsync(CompletableFuture.supplyAsync(() -> {
            System.out.println(Thread.currentThread().getName() + "---------supplyAsync2---------开始---");
            System.out.println(Thread.currentThread().getName() + "---------supplyAsync2---------结束---");
            return "hello2";
        }), t -> {
            try {
                TimeUnit.SECONDS.sleep(5);
            } catch (Exception e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName() + "........." + t);
        });

        System.out.println("---------------------------");
        voidCompletableFuture.join();
    }

这里写图片描述


runAfterBothAsync

runAfterBothAsync(CompletionStage<?> other, Runnable action)

两个线程执行完毕后,不关心结果,进入Runnable ,return void,返回CompletableFuture<Void>

public static void main(String[] args) {
        CompletableFuture<Void> voidCompletableFuture = CompletableFuture.supplyAsync(() -> {
            System.out.println(Thread.currentThread().getName() + "---------supplyAsync1---------开始---");
            System.out.println(Thread.currentThread().getName() + "---------supplyAsync1---------结束---");
            return "hello1";
        }).runAfterBothAsync(CompletableFuture.supplyAsync(() -> {
            System.out.println(Thread.currentThread().getName() + "---------supplyAsync2---------开始---");
            try {
                TimeUnit.SECONDS.sleep(5);
            } catch (Exception e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName() + "---------supplyAsync2---------结束---");
            return "hello2".length();
        }), () -> {
            System.out.println(Thread.currentThread().getName() + ".........");
            try {
                TimeUnit.SECONDS.sleep(5);
            } catch (Exception e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName() + ".........");
        });

        System.out.println("---------------------------");
        voidCompletableFuture.join();
    }

这里写图片描述


runAfterEitherAsync

CompletableFuture<Void> runAfterEitherAsync(CompletionStage<?> other,
                                                       Runnable action)

有线程结束后,不关心结果,进入Runnable ,return void,返回CompletableFuture<Void>

public static void main(String[] args) {
        CompletableFuture<Void> voidCompletableFuture = CompletableFuture.supplyAsync(() -> {
            System.out.println(Thread.currentThread().getName() + "---------supplyAsync1---------开始---");
            try {
                TimeUnit.SECONDS.sleep(5);
            } catch (Exception e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName() + "---------supplyAsync1---------结束---");
            return "hello1";
        }).runAfterEitherAsync(CompletableFuture.supplyAsync(() -> {
            System.out.println(Thread.currentThread().getName() + "---------supplyAsync2---------开始---");
            System.out.println(Thread.currentThread().getName() + "---------supplyAsync2---------结束---");
            return "hello2".length();
        }), () -> {
            System.out.println(Thread.currentThread().getName() + ".........");
            try {
                TimeUnit.SECONDS.sleep(3);
            } catch (Exception e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName() + ".........");
        });

        System.out.println("---------------------------");
        voidCompletableFuture.join();
    }

这里写图片描述


thenCombineAsync

CompletableFuture<V> thenCombineAsync(
        CompletionStage<? extends U> other,
        BiFunction<? super T,? super U,? extends V>

同时执行两个线程,获取两个返回结果T,U作为参数,进入BiFunction,return v , CompletableFuture<V>

public static void main(String[] args) {
        CompletableFuture<String> completableFuture = CompletableFuture.supplyAsync(() -> {
            System.out.println(Thread.currentThread().getName() + "---------supplyAsync1---------开始---");
            System.out.println(Thread.currentThread().getName() + "---------supplyAsync1---------结束---");
            return "hello1";
        }).thenCombineAsync(CompletableFuture.supplyAsync(() -> {
            System.out.println(Thread.currentThread().getName() + "---------supplyAsync2---------开始---");
            System.out.println(Thread.currentThread().getName() + "---------supplyAsync2---------结束---");
            return 100;
        }), (t, u) -> {
            System.out.println(Thread.currentThread().getName() + "........." + t);
            try {
                TimeUnit.SECONDS.sleep(3);
            } catch (Exception e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName() + "........." + u);
            return "=thenCombine=";
        });

        try {
            System.out.println("---------------------------" + completableFuture.get());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

这里写图片描述


thenComposeAsync

CompletableFuture<U> thenComposeAsync(
        Function<? super T, ? extends CompletionStage<U>> fn)
public static void main(String[] args) {
        CompletableFuture<Integer> completableFuture = CompletableFuture.supplyAsync(() -> {
            System.out.println(Thread.currentThread().getName() + "---------supplyAsync1---------开始---");
            try {
                TimeUnit.SECONDS.sleep(3);
            } catch (Exception e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName() + "---------supplyAsync1---------结束---");
            return "hello1";
        }).thenComposeAsync(t -> {
            CompletableFuture<Integer> integerCompletableFuture = CompletableFuture.supplyAsync(() -> {
                System.out.println(Thread.currentThread().getName() + "---------supplyAsync2---------开始---" + t);
                try {
                    TimeUnit.SECONDS.sleep(3);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName() + "---------supplyAsync2---------结束---" + t);
                return 100;
            });
            return integerCompletableFuture;
        });

        try {
            System.out.println("=----------------------------=");
            System.out.println("---------------------------" + completableFuture.get());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

这里写图片描述



其它方法

  • getNow(T valueIfAbsent):线程执行完了,获取返回的值,如果没有执行完毕,返回指定值。不阻塞。
public static void main(String[] args) {
        CompletableFuture<String> stringCompletableFuture = CompletableFuture.supplyAsync(() -> {
            System.out.println(Thread.currentThread().getName() + "---------supplyAsync----begin-------"+ System.currentTimeMillis());
            try {
                TimeUnit.SECONDS.sleep(3);
            } catch (Exception e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName() + "---------supplyAsync-----end------"+ System.currentTimeMillis());
            return "hello";
        });
        //执行完了 获取返回的值,如果没有执行完毕,返回指定值
        String now = stringCompletableFuture.getNow("i am getNow value");
        System.err.println(Thread.currentThread().getName() + "=====getNow值:" + now + " ====时间: " + System.currentTimeMillis());
        try {
            System.err.println(Thread.currentThread().getName() + "====get值:" + stringCompletableFuture.get() + "===时间: " + System.currentTimeMillis());

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

这里写图片描述


  • complete(T value):若完成,返回false,get()时获取返回值; 若未完成,返回true,get()时获取指定值。
public static void main(String[] args) {
        CompletableFuture<String> stringCompletableFuture = CompletableFuture.supplyAsync(() -> {
            System.out.println(Thread.currentThread().getName() + "---------supplyAsync----begin-------"+ System.currentTimeMillis());
            try {
                TimeUnit.SECONDS.sleep(4);
            } catch (Exception e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName() + "---------supplyAsync-----end------"+ System.currentTimeMillis());
            return "hello";
        });
        try {
            TimeUnit.SECONDS.sleep(5);
        } catch (Exception e) {
            e.printStackTrace();
        }
        //若完成,返回false,get()时获取返回值; 若未完成,返回true,get()时获取指定值
        boolean complete = stringCompletableFuture.complete("WWWWW");
        System.out.println(complete);
        try {
            System.out.println(Thread.currentThread().getName() + "  get值:" + stringCompletableFuture.get() + " 时间: " + System.currentTimeMillis());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

这里写图片描述


  • completeExceptionally(Throwable ex)://若完成,返回false,get()时获取返回值; 若未完成,返回true,get()时抛出指定异常
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值