码迷,mamicode.com
首页 > 其他好文 > 详细

CompletableFuture的get和getNow()的区别

时间:2020-03-23 12:38:12      阅读:288      评论:0      收藏:0      [点我收藏+]

标签:try   null   pre   system   seconds   span   col   time   unit   

 CompletableFuture<Integer> ad = null;
        if (true) {
            ad = CompletableFuture.supplyAsync(() -> {
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                return 1;
            });
        }
        System.out.println(ad);
        if (ad != null) {
            try {
                System.out.println(ad.getNow(0));
                int dd  = ad.get();
                System.out.println(dd);
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (ExecutionException e) {
                e.printStackTrace();
            }
        }

result: 0, 1

getNow()不会阻塞

get()阻塞获取结果

CompletableFuture的get和getNow()的区别

标签:try   null   pre   system   seconds   span   col   time   unit   

原文地址:https://www.cnblogs.com/lijiale/p/12551433.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!