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

相关文章:

  • 2021-05-12
  • 2022-01-23
  • 2021-05-15
猜你喜欢
  • 2021-12-10
  • 2022-12-23
  • 2022-12-23
  • 2021-04-04
  • 2021-05-25
  • 2021-09-09
相关资源
相似解决方案