• 异步任务 CompletableFuture异步编排


    当异步任务A和异步任务B存在关系,比如B需要A的结果数据,就需要编排控制。

    1. import java.util.concurrent.*;
    2. /**
    3. * future1没有返回值 future2有异步返回结果
    4. */
    5. public class FutureTest {
    6. public static ExecutorService executor = Executors.newFixedThreadPool(10);
    7. public static void main(String[] args) throws ExecutionException, InterruptedException {
    8. //异步对象
    9. System.out.println("main()-----start");
    10. CompletableFuture<Void> future1 = CompletableFuture.runAsync(() -> {
    11. System.out.println("future1无返回值异步任务测试哦");
    12. }, executor);
    13. CompletableFuture<Integer> future2 = CompletableFuture
    14. .supplyAsync(() -> {
    15. return 10 / 2;
    16. //return 10 / 0; //模拟算数异常
    17. }, executor)
    18. .whenComplete((result, exception) -> {
    19. System.out.println("future2链式获取结果是:" + result);
    20. if (exception != null) {
    21. System.out.println("future2链式获取到异常:" + exception);
    22. }
    23. })
    24. .exceptionally(throwable -> {
    25. System.out.println("future2处理异常,返回默认值");
    26. return 10;
    27. });
    28. System.out.println("future2结果是:" + future2.get());
    29. CompletableFuture<Integer> future3 = CompletableFuture
    30. .supplyAsync(() -> {
    31. return 10 / 2;
    32. }, executor)
    33. .handle((result, exception) -> {
    34. if (result != null) {
    35. System.out.println("handle处理返回结果");
    36. return result * 10;
    37. }
    38. if (exception != null) {
    39. return 0;
    40. }
    41. return 0;
    42. });
    43. System.out.println("future3结果是:" + future3.get());
    44. //run...是没有返回值的
    45. /**
    46. * 线程串行化
    47. * 1.thenRun.. 不能获取到上一步的执行结果
    48. * 2.thenAccept.. 能获取到上一步的返回值,但没有返回值
    49. * 3.thenApply.. 既能获取到上一步的返回值,有返回值
    50. */
    51. CompletableFuture<Void> future4 = CompletableFuture
    52. .supplyAsync(() -> {
    53. return 10 / 2;
    54. }, executor)
    55. .thenRunAsync(() -> {
    56. System.out.println("future4任务2启动了,无返回值");
    57. }, executor);
    58. CompletableFuture<Void> future5 = CompletableFuture
    59. .supplyAsync(() -> {
    60. return 10 / 2;
    61. }, executor)
    62. .thenAcceptAsync((res) -> {
    63. System.out.println("future5任务2启动了,能获取上一步值,但无返回值:" + res);
    64. });
    65. CompletableFuture<Integer> future6 = CompletableFuture
    66. .supplyAsync(() -> {
    67. return 10 / 2;
    68. }, executor)
    69. .thenApplyAsync(res -> {
    70. System.out.println("future6任务2启动了,有返回值:" + res);
    71. return res / 5;
    72. }, executor);
    73. System.out.println("future6最终返回值:" + future6.get());
    74. System.out.println("main()-----end");
    75. }
    76. }

  • 相关阅读:
    两大产品上线“粤复用”,赋能大数据智能行业发展
    【Linux】 ubuntu内存清理
    DGIOT云云对接——第三方平台对接dgiot基本接口介绍
    【HTML】【休闲益智】还有9块月饼并未获得!请及时出战!(解锁月饼小游戏
    35.cuBLAS开发指南中文版--cuBLAS中的Level-2函数hbmv()
    JSP注释(多种注释详解)
    几道毒瘤微积分题
    A-Level经济真题(8)
    Ansible 多机自动化工具 初学笔记
    递推+模拟---想好如何存储?
  • 原文地址:https://blog.csdn.net/dj1955/article/details/126857851