CompletableFuture的get和getNow()的区别

 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()阻塞获取结果

posted @ 2020-03-23 12:26  java渣渣  阅读(3684)  评论(0编辑  收藏  举报