zoukankan      html  css  js  c++  java
  • 常用之juc

    // 没有返回值的异步回调 CompletableFuture.runAsync
    // get方法会阻塞
    CompletableFuture<Void> completableFuture=CompletableFuture.runAsync(()->{
    try {
    TimeUnit.SECONDS.sleep(2);
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    System.out.println(Thread.currentThread().getName()+"runAsync->Void");
    });

    System.out.println("Demo1.main 11111");
    completableFuture.get();
    System.out.println("Demo1.main 22222");

    // 有返回值得异步回调 CompletableFuture.supplyAsync
    // whenComplete 编译成功后的处理
    // exceptionally 异常后的处理
    // get方法会阻塞
    CompletableFuture<Integer> uCompletableFuture = CompletableFuture.supplyAsync(() -> {
    System.out.println(Thread.currentThread().getName() + "supplyAsync->Integer");
    int i=10/0;
    return 1024;
    });
    System.out.println("Demo1.main 11111");
    Integer integer = uCompletableFuture.whenComplete((t, u) -> {
    System.out.println("t=>"+t); // 正常情况下,返回结果
    System.out.println("u=>"+u);
    }).exceptionally((e) -> { //异常情况下的处理
    System.out.println(e.getMessage());
    return 233;
    }).get();
    System.out.println(integer);
    System.out.println("Demo1.main 22222");

  • 相关阅读:
    docker安装
    [golang grpc] 框架介绍
    docker介绍
    Visual Studio Code常用设置
    eclipse常用设置
    [golang note] 网络编程
    [golang note] 工程组织
    [golang note] 协程通信
    [golang note] 协程基础
    [golang note] 接口使用
  • 原文地址:https://www.cnblogs.com/windy13/p/13463641.html
Copyright © 2011-2022 走看看