java线程之异步回调

java线程之异步回调,第1张

  public static void main(String[] args) throws ExecutionException, InterruptedException {
        //    异步调用,没有返回值
        CompletableFuture completableFuture1 = CompletableFuture.runAsync(() -> {
            System.out.println(Thread.currentThread().getName()+"completableFuture1");
        });

    //异步调用有返回值
        CompletableFuture completableFuture2=CompletableFuture.supplyAsync(()->{
            System.out.println(Thread.currentThread().getName()+"completableFuture2");
            return 12306;
        });
        System.out.println(completableFuture2.get());
        
        CompletableFuture completableFuture3=CompletableFuture.supplyAsync(()->{
            System.out.println(Thread.currentThread().getName()+"completableFuture3");
            int a=1/0;
            return 12306;
        });
        Integer integer = completableFuture3.whenComplete((num, error) -> {
            System.out.println("num:" + num);
            System.out.println("error:" + error);
        }).get();
        System.out.println(integer);
    }

 

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/920150.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-16
下一篇 2022-05-16

发表评论

登录后才能评论

评论列表(0条)

保存