当前位置:   article > 正文

CompletableFuture使用详情_completablefuture runasync allof会阻塞线程等待异步完成么

completablefuture runasync allof会阻塞线程等待异步完成么

一、runAsync

(1)runAsync(Runnable)

(2)runAsync(Runnable, Executor)

二、supplyAsync

(1)supplyAsync(Supplier)

(2)supplyAsync(Supplier , Executor)

三、CompletableFuture中 get 与 join的区别

四、thenApply方法

(1)thenApply(Function)

五、handle方法

(1)handle(BiFunction fn),>

(2)handleAsync(BiFunction fn,Executor),>

(1)thenCombine

(2)thenCombineAsync

六、thenCompose方法

(1)thenCompose

七、allOf / anyOf


前言

普通情况下,我们的接口逻辑都是串行化的,有时候在我们方法中可能存在着非常耗时的操作这样就会造成代码阻塞,但是呢,为了用户的体验,我们可能需要将一些复杂的数据开启线程进行异步处理。

所谓异步,其实就是实现一个可无需等待被调用函数的返回值而让操作继续运行的方法,简单的讲就是另启一个线程来完成调用中的部分计算,使调用继续运行或返回,而不需要等待计算结果。

Java8 提供的CompletableFuture 可以自定义线程池或使用默认线程池对数据进行异步处理,且可以根据需求选择是否返回异步结果!灵活的使用CompletableFuture可以让我们感受java8的异步编程之美!

以下,便是CompletableFuture 的异步操作创建方式示例。

CompletableFuture的思想是,当被调用时,它们会立即被安排启动开始执行异步任务(与流式操作中的延迟计算有着明显区别)。

一、runAsync

(1)runAsync(Runnable)

使用ForkJoinPool.commonPool()作为它的线程池执行异步代码,

  1. public class AsyncDemo01 {
  2. public static void main(String[] args) {
  3. //当前调用者线程为:main
  4. System.out.println("当前调用者线程为:" + Thread.currentThread().getName());
  5. CompletableFuture.runAsync(() -> {
  6. // 异步方法内当前执行线程为:ForkJoinPool.commonPool-worker-1
  7. System.out.println("异步方法内当前执行线程为:" + Thread.currentThread().getName());
  8. System.out.println(111);
  9. });
  10. }
  11. }
(2)runAsync(Runnable, Executor)

使用指定的线程池执行异步代码。此异步方法无法返回值

  1. public class AsyncDemo02 {
  2. public static void main(String[] args) {
  3. //当前调用者线程为:main
  4. System.out.println("当前调用者线程为:" + Thread.currentThread().getName());
  5. // fixme 根据阿里规约 建议真实开发时使用 ThreadPoolExecutor 定义线程池
  6. ExecutorService threadPool = Executors.newFixedThreadPool(10);
  7. CompletableFuture.runAsync(() -> {
  8. // 异步方法内当前执行线程为:pool-1-thread-1
  9. System.out.println("异步方法内当前执行线程为:" + Thread.currentThread().getName());
  10. System.out.println(111);
  11. }, threadPool);
  12. // 演示代码,所以选择执行完后关闭线程池
  13. threadPool.shutdown();
  14. }
  15. }

二、supplyAsync

(1)supplyAsync(Supplier)

使用ForkJoinPool.commonPool()作为它的线程池执行异步代码,异步操作有返回值

  1. public class SupplyDemo01 {
  2. public static void main(String[] args) {
  3. CompletableFuture<String> supplyAsync = CompletableFuture.supplyAsync(() -> {
  4. // 异步方法内当前执行线程为:ForkJoinPool.commonPool-worker-1
  5. System.out.println("异步方法内当前执行线程为:" + Thread.currentThread().getName());
  6. // 模拟返回值
  7. return "hello,world";
  8. });
  9. // 获取异步线程执行结果
  10. System.out.println(supplyAsync.join());
  11. }
  12. }
(2)supplyAsync(Supplier , Executor)

使用指定线程池 来执行可获取返回值的异步任务

  1. public class SupplyDemo02 {
  2. public static void main(String[] args) {
  3. // fixme 根据阿里规约 建议真实开发时使用 ThreadPoolExecutor 定义线程池
  4. ExecutorService threadPool = Executors.newFixedThreadPool(10);
  5. CompletableFuture<String> supplyAsync = CompletableFuture.supplyAsync(() -> {
  6. // 异步方法内当前执行线程为:pool-1-thread-1
  7. System.out.println("异步方法内当前执行线程为:" + Thread.currentThread().getName());
  8. // 模拟耗时与返回结果
  9. try {
  10. Thread.sleep(2000);
  11. } catch (InterruptedException e) {
  12. e.printStackTrace();
  13. }
  14. return "hello,world";
  15. },threadPool);
  16. // 获取异步线程执行结果
  17. System.out.println(supplyAsync.join());
  18. }
  19. }

三、CompletableFuture中 get 与 join的区别

CompletableFuture 使用 supplyAsync 来执行异步任务的话,可通过调用 get 或 join方法便可获取异步线程的执行结果。

不同:get方法返回结果,抛出的是检查异常,必须用户throw或者try/catch处理,join返回结果,抛出未检查异常。

相同:join和get方法都是依赖于完成信号并返回结果T的阻塞方法。(阻塞调用者线程,等待异步线程返回结果)。

join: 

get:

 需要注意的是, completableFuture 的get 方法 有重载,还有一个可传入获取结果等待时间的get方法

如果超过等待时间,异步线程还未返回结果,那么get 调用者线程则会抛出TimeoutException 异常

 

四、thenApply方法

当我们第二个任务依赖第一个任务的结果的时候,可以使用 thenApply相关方法来把这两个线程串行化,参数是一个Function(代表着我们需要传入一个转换的函数 具体可参考JAVA8 函数式接口)thenApply 只可以执行正常的任务,任务出现异常则不会执行 thenApply 方法;如果当第一个任务出现异常时仍要执行第二个任务,可以使用下方的Handle方法

  1. public <U> CompletableFuture<U> thenApply(Function<? super T,? extends U> fn)
  2. public <U> CompletableFuture<U> thenApplyAsync(Function<? super T,? extends U> fn)
  3. public <U> CompletableFuture<U> thenApplyAsync(Function<? super T,? extends U> fn, Executor executor)

T:上一个异步任务返回值

U: 当前执行最后返回值

(1)thenApply(Function)

thenApplythenApply中的线程为调用者线程,与CompletableFuture 底层默认所用的 ForkJoin无关!

示例:

  1. public class SupplyDemo03 {
  2. public static void main(String[] args) {
  3. CompletableFuture<String> future = CompletableFuture
  4. .supplyAsync(() -> {
  5. System.out.println(Thread.currentThread().getName());
  6. return "hello";
  7. }).thenApply(e -> {
  8. System.out.println(Thread.currentThread().getName());
  9. return e + ",";
  10. }).thenApply(e -> {
  11. System.out.println(Thread.currentThread().getName());
  12. return (e + "world").toUpperCase();
  13. });
  14. System.out.println(future.join());
  15. }
  16. }

程序执行情况:

最后JOIN 获取结果为 HELLO,WORLD

三个线程分别是 ForkJoinPool.commonPool-worker-1,main,main(2)thenApplyAsync(Function)
thenApplyAsync:当我们第二个任务依赖第一个任务的结果的时候,且第二个任务也想采用异步的方式,则可以使用 thenApplyAsync(Function)

  1. public static void main(String[] args) {
  2. CompletableFuture<String> future = CompletableFuture
  3. .supplyAsync(() -> {
  4. System.out.println(Thread.currentThread().getName());
  5. return "hello";
  6. }).thenApplyAsync(e -> {
  7. System.out.println(Thread.currentThread().getName());
  8. return e + ",";
  9. }).thenApplyAsync(e -> {
  10. System.out.println(Thread.currentThread().getName());
  11. return (e + "world").toUpperCase();
  12. });
  13. System.out.println(future.join());
  14. }

程序执行情况:

最后JOIN 获取结果为 HELLO,WORLD

三个线程均是 是 ForkJoinPool.commonPool-worker-xxxx

当然,我们也可以使用 thenApplyAsync(Function, Executor) 来使用自定义线程池执行我们的异步任务

五、handle方法

handle 是执行任务完成时对结果的处理, handle 方法和 thenApply 方法处理方式大致一样,不同的是 handle 是在任务完成后再执行且Handle可以根据可以根据任务是否有异常来进行做相应的后续处理操作。

  1. public <U> CompletionStage<U> handle(BiFunction<? super T, Throwable, ? extends U> fn);
  2. public <U> CompletionStage<U> handleAsync(BiFunction<? super T, Throwable, ? extends U> fn);
  3. public <U> CompletionStage<U> handleAsync(BiFunction<? super T, Throwable, ? extends U> fn,Executor executor);

(1)handle(BiFunction<T, Throwable, U> fn)

使用Handler方法 预估异常情况 进行逻辑处理 默认handle中使用的线程为调用者线程

  1. public class HandleDemo01 {
  2. public static void main(String[] args) {
  3. CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
  4. System.out.println("当前supplyAsync 执行线程:" + Thread.currentThread().getName());
  5. // 模拟异常
  6. int a = 1 / 0;
  7. return "hello";
  8. }).handle((x, t) -> {
  9. System.out.println("当前handle 执行线程:" + Thread.currentThread().getName());
  10. if (t != null) {
  11. // 出现异常 打印异常信息 或者doSomething
  12. System.out.println("发现上一个异步任务出异常了" + t.getMessage());
  13. } else {
  14. // 未出异常 doSomething
  15. return x;
  16. }
  17. // 设置默认结果
  18. return "error";
  19. });
  20. System.out.println(future.join());
  21. }
  22. }

程序执行情况:

当前supplyAsync 执行线程:ForkJoinPool.commonPool-worker-1
当前handle 执行线程:main
发现上一个异步任务出异常了java.lang.ArithmeticException: / by zero
error

(2)handleAsync(BiFunction<T, Throwable, U> fn,Executor)

使用自定义的线程 (或者使用默认的ForkJoin)进行异步处理第二个线程的任务

  1. public class HandleDemo01 {
  2. public static void main(String[] args) {
  3. ExecutorService threadPool = Executors.newFixedThreadPool(2);
  4. CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
  5. System.out.println("当前supplyAsync 执行线程:" + Thread.currentThread().getName());
  6. // 模拟异常
  7. int a = 1 / 0;
  8. return "hello";
  9. },threadPool).handleAsync((x, t) -> {
  10. System.out.println("当前handle 执行线程:" + Thread.currentThread().getName());
  11. if (t != null) {
  12. // 出现异常 打印异常信息 或者doSomething
  13. System.out.println("发现上一个异步任务出异常了" + t.getMessage());
  14. } else {
  15. // 未出异常 doSomething
  16. return x;
  17. }
  18. // 设置默认结果
  19. return "error";
  20. },threadPool);
  21. System.out.println(future.join());
  22. }
  23. }

程序执行情况:

当前supplyAsync 执行线程:pool-1-thread-1
当前handle 执行线程:pool-1-thread-2
发现上一个异步任务出异常了java.lang.ArithmeticException: / by zero
error

七、thenCombine方法
thenCombine 会在两个CompletableFuture任务都执行完成后,把两个任务的结果一块处理。

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

thenCombine 会在两个CompletableFuture任务都执行完成后,调用者线程会把两个异步任务的结果一块处理

  1. public class ThenCombineDemo01 {
  2. public static void main(String[] args) {
  3. CompletableFuture<String> helloAsync = CompletableFuture.supplyAsync(() -> {
  4. System.out.println("hello 执行线程:" + Thread.currentThread().getName());
  5. return "hello";
  6. });
  7. CompletableFuture<String> worldAsync = CompletableFuture.supplyAsync(() -> {
  8. System.out.println("world 执行线程:" + Thread.currentThread().getName());
  9. return "world";
  10. });
  11. CompletableFuture<String> result = worldAsync.thenCombine(helloAsync, (hello, world) -> {
  12. System.out.println("result 执行线程:" + Thread.currentThread().getName());
  13. return (hello + "," + world).toUpperCase();
  14. });
  15. System.out.println("获取结果 执行线程:" + Thread.currentThread().getName());
  16. System.out.println("两个异步任务合并结果:" + result.join());
  17. }
  18. }

程序执行结果:

hello 执行线程:ForkJoinPool.commonPool-worker-1
world 执行线程:ForkJoinPool.commonPool-worker-1
result 执行线程:main
获取结果 执行线程:main
两个异步任务合并结果:WORLD,HELLO

(2)thenCombineAsync

thenCombineAsync 会在两个CompletableFuture任务都执行完成后,再用一个异步线程把两个任务的结果一块处理

六、thenCompose方法

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

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

(1)thenCompose

thenCompose当第一个异步任务操作完成时,会将其结果作为参数传递给第二个任务(第二个任务为串行化操作,由调用者线程执行)

  1. public class ThenComposeDemo01 {
  2. public static void main(String[] args) {
  3. CompletableFuture<String> result = CompletableFuture.supplyAsync(() -> {
  4. System.out.println("hello 执行线程:" + Thread.currentThread().getName());
  5. return "hello";
  6. }).thenCompose((hello -> {
  7. System.out.println("thenCompose 执行线程:" + Thread.currentThread().getName());
  8. return CompletableFuture.supplyAsync((hello + "world")::toUpperCase);
  9. }));
  10. System.out.println("获取结果 执行线程:" + Thread.currentThread().getName());
  11. System.out.println("两个异步任务流水线执行结果:" + result.join());
  12. }
  13. }

程序执行情况:

hello 执行线程:ForkJoinPool.commonPool-worker-1
thenCompose 执行线程:main
获取结果 执行线程:main
两个异步任务流水线执行结果:HELLOWORLD(2)thenComposeAsync

thenComposeAsync当第一个异步任务操作完成时,会将其结果作为参数传递给第二个任务(第二个任务仍为异步线程执行操作,可由默认ForkJoin线程池执行,也可使用自定义线程池)

  1. public class ThenComposeDemo02 {
  2. public static void main(String[] args) {
  3. ExecutorService threadPool = Executors.newFixedThreadPool(2);
  4. CompletableFuture<String> result = CompletableFuture.supplyAsync(() -> {
  5. System.out.println("hello 执行线程:" + Thread.currentThread().getName());
  6. return "hello";
  7. },threadPool).thenComposeAsync((hello -> {
  8. System.out.println("thenCompose 执行线程:" + Thread.currentThread().getName());
  9. return CompletableFuture.supplyAsync((hello + "world")::toUpperCase);
  10. }),threadPool);
  11. System.out.println("获取结果 执行线程:" + Thread.currentThread().getName());
  12. System.out.println("两个异步任务流水线执行结果:" + result.join());
  13. }
  14. }

程序执行情况:

hello 执行线程:pool-1-thread-1
获取结果 执行线程:main
thenCompose 执行线程:pool-1-thread-2
两个异步任务流水线执行结果:HELLOWORLD

七、allOf / anyOf

allOf:CompletableFuture是多个任务都执行完成后才会执行,只有有一个任务执行异常,则返回的CompletableFuture执行get方法时会抛出异常,如果都是正常执行,则get返回null。

anyOf :CompletableFuture是多个任务只要有一个任务执行完成,则返回的CompletableFuture执行get方法时会抛出异常,如果都是正常执行,则get返回执行完成任务的结果。

测试代码:

  1. public static void main(String[] args) throws ExecutionException, InterruptedException {
  2. CompletableFuture<String> cf1 = CompletableFuture.supplyAsync(() -> {
  3. try {
  4. System.out.println(Thread.currentThread() + " cf1 do something....");
  5. Thread.sleep(2000);
  6. } catch (InterruptedException e) {
  7. e.printStackTrace();
  8. }
  9. System.out.println("cf1 任务完成");
  10. return "cf1 任务完成";
  11. });
  12. CompletableFuture<String> cf2 = CompletableFuture.supplyAsync(() -> {
  13. try {
  14. System.out.println(Thread.currentThread() + " cf2 do something....");
  15. int a = 1/0;
  16. Thread.sleep(5000);
  17. } catch (InterruptedException e) {
  18. e.printStackTrace();
  19. }
  20. System.out.println("cf2 任务完成");
  21. return "cf2 任务完成";
  22. });
  23. CompletableFuture<String> cf3 = CompletableFuture.supplyAsync(() -> {
  24. try {
  25. System.out.println(Thread.currentThread() + " cf2 do something....");
  26. Thread.sleep(3000);
  27. } catch (InterruptedException e) {
  28. e.printStackTrace();
  29. }
  30. System.out.println("cf3 任务完成");
  31. return "cf3 任务完成";
  32. });
  33. CompletableFuture<Void> cfAll = CompletableFuture.allOf(cf1, cf2, cf3);
  34. System.out.println("cfAll结果->" + cfAll.get());
  35. }
  36. public static void main(String[] args) throws ExecutionException, InterruptedException {
  37. CompletableFuture<String> cf1 = CompletableFuture.supplyAsync(() -> {
  38. try {
  39. System.out.println(Thread.currentThread() + " cf1 do something....");
  40. Thread.sleep(2000);
  41. } catch (InterruptedException e) {
  42. e.printStackTrace();
  43. }
  44. System.out.println("cf1 任务完成");
  45. return "cf1 任务完成";
  46. });
  47. CompletableFuture<String> cf2 = CompletableFuture.supplyAsync(() -> {
  48. try {
  49. System.out.println(Thread.currentThread() + " cf2 do something....");
  50. Thread.sleep(5000);
  51. } catch (InterruptedException e) {
  52. e.printStackTrace();
  53. }
  54. System.out.println("cf2 任务完成");
  55. return "cf2 任务完成";
  56. });
  57. CompletableFuture<String> cf3 = CompletableFuture.supplyAsync(() -> {
  58. try {
  59. System.out.println(Thread.currentThread() + " cf2 do something....");
  60. Thread.sleep(3000);
  61. } catch (InterruptedException e) {
  62. e.printStackTrace();
  63. }
  64. System.out.println("cf3 任务完成");
  65. return "cf3 任务完成";
  66. });
  67. CompletableFuture<Object> cfAll = CompletableFuture.anyOf(cf1, cf2, cf3);
  68. System.out.println("cfAll结果->" + cfAll.get());
  69. }
本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/黑客灵魂/article/detail/824179
推荐阅读
相关标签
  

闽ICP备14008679号