CompletableFuture的get和getNow()的区别
时间:2020-03-23 13:59:04
收藏:0
阅读:726
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()阻塞获取结果
原文:https://www.cnblogs.com/lijiale/p/12551433.html
评论(0)