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


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM