多线程之CompletableFuture(中)

来源:互联网 发布:丢书大作战 知乎 编辑:程序博客网 时间:2024/05/29 19:18

thenAcceptAsync

Consumer 接收上个阶段的返回值T,return Void;返回CompletableFuture<Void>

  • thenAccept
    该方法是同步的
public static void main(String[] args) {        CompletableFuture<Void> stringCompletableFuture = CompletableFuture.supplyAsync(() -> {            System.out.println(Thread.currentThread().getName() + "---------supplyAsync---------开始---");            System.out.println(Thread.currentThread().getName() + "---------supplyAsync---------结束---");            return "hello";        }).thenAccept(s -> {            System.out.println(Thread.currentThread().getName() + "---thenAcceptAsync---start---" + s);            try {                TimeUnit.SECONDS.sleep(5);            } catch (Exception e) {                e.printStackTrace();            }            System.out.println(Thread.currentThread().getName() + "---thenAcceptAsync----end---" + s);        });        try {            System.out.println("============================");            stringCompletableFuture.join();        } catch (Exception e) {            e.printStackTrace();        }    }

代码结果:这里写图片描述
thenAccept方法是mian线程执行的,阻塞了输出“============================”。

  • thenAcceptAsync
    以Async结尾代表是异步,将上述代码中thenAccept换成thenAcceptAsync,看输出结果:
    这里写图片描述

下面介绍的方法都会有以Async和没有Async结尾两种形式,都是类似的,以Async结尾代表异步,统一只介绍以Async结尾的方法。


thenRunAsync

Runnable 不接受上个阶段返回值和异常,return Void;返回CompletableFuture<Void>

public static void main(String[] args) {        CompletableFuture<Void> voidCompletableFuture = CompletableFuture.supplyAsync(() -> {            System.out.println(Thread.currentThread().getName() + "---------supplyAsync---------开始---");            try {                TimeUnit.SECONDS.sleep(3);            } catch (Exception e) {                e.printStackTrace();            }            System.out.println(Thread.currentThread().getName() + "---------supplyAsync---------结束---");            return "hello";        }).thenRunAsync(() -> {            System.out.println(Thread.currentThread().getName() + "---thenRunAsync---start---");            try {                TimeUnit.SECONDS.sleep(5);            } catch (Exception e) {                e.printStackTrace();            }            System.out.println(Thread.currentThread().getName() + "---thenRunAsync----end---");        });        try {            System.out.println(voidCompletableFuture.join());        } catch (Exception e) {            e.printStackTrace();        }    }

这里写图片描述


thenapplyAsync

Function<T, R>接收上个阶段的返回值T,return R ; 返回CompletableFuture<R>

public static void main(String[] args) {        CompletableFuture<Integer> completableFuture = CompletableFuture.supplyAsync(() -> {            return "hello";        }).thenApplyAsync(v -> {            System.out.println(Thread.currentThread().getName() + "---thenApplyAsync---start---" + v);            try {                TimeUnit.SECONDS.sleep(5);            } catch (Exception e) {                e.printStackTrace();            }            System.out.println(Thread.currentThread().getName() + "---thenApplyAsync----end---" + v);            return v.length();        });        try {            System.out.println(completableFuture.get());        } catch (Exception e) {            e.printStackTrace();        }    }

这里写图片描述


whencompleteAsync

BiConsumer<T, U> 接收上个阶段的返回值T和异常U,return Void;返回上个阶段返回值CompletableFuture<T>

public static void main(String[] args) {        CompletableFuture<String> stringCompletableFuture = CompletableFuture.supplyAsync(() -> {            System.out.println(Thread.currentThread().getName() + "---------supplyAsync-----------");            return "hello";        }).whenCompleteAsync((V, T) -> {            System.out.println(Thread.currentThread().getName() + "---whenCompleteAsync---start---" + V);            try {                TimeUnit.SECONDS.sleep(5);            } catch (Exception e) {                e.printStackTrace();            }            System.out.println(Thread.currentThread().getName() + "---whenCompleteAsync----end---" + V);        });        try {            System.out.println(stringCompletableFuture.get());        } catch (Exception e) {            e.printStackTrace();        }    }

这里写图片描述


handleAsync

BiFunction<T, U, R> 接收上个阶段的返回值T和异常U,return R;返回CompletableFuture<R>

public static void main(String[] args) {        CompletableFuture<Integer> completableFuture = CompletableFuture.supplyAsync(() -> {            System.out.println(Thread.currentThread().getName() + "---------supplyAsync---------开始---");            try {                TimeUnit.SECONDS.sleep(1);            } catch (Exception e) {                e.printStackTrace();            }            System.out.println(Thread.currentThread().getName() + "---------supplyAsync---------结束---");            int e = 2/0;            return "hello";        }).handleAsync((s, v) -> {            System.out.println(Thread.currentThread().getName() + "---handle---start---" + s);            try {                TimeUnit.SECONDS.sleep(5);            } catch (Exception e) {                e.printStackTrace();            }            System.out.println(Thread.currentThread().getName() + "---handle----end---" + s);            System.out.println("捕获的异常:"+ v);            return s == null ? 0 : s.length();        });        try {            System.out.println(completableFuture.get());        } catch (Exception e) {            e.printStackTrace();        }    }

这里写图片描述

原创粉丝点击