CompletableFuture的join和get对比
get()方法
public class CompeltableFutureBuildDemo { public static void main(String[] args) throws ExecutionException, InterruptedException { CompletableFuture<String> voidCompletableFuture = CompletableFuture.supplyAsync(() -> { System.out.println(Thread.currentThread().getName()); try {TimeUnit.MILLISECONDS.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();} return "hello supplyAsync"; }); System.out.println(voidCompletableFuture.get()); } }
join方法
public class CompeltableFutureBuildDemo { public static void main(String[] args) { CompletableFuture<String> voidCompletableFuture = CompletableFuture.supplyAsync(() -> { System.out.println(Thread.currentThread().getName()); try {TimeUnit.MILLISECONDS.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();} return "hello supplyAsync"; }); System.out.println(voidCompletableFuture.join()); } }
get方法和join方法功能一样,就是没有编译验证,不需要抛出异常。