CompletableFuture的join和get对比

简介: CompletableFuture

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方法功能一样,就是没有编译验证,不需要抛出异常。

目录
相关文章
|
1月前
|
Python
thread.join()
thread.join()
18 1
|
7月前
CompletableFuture
CompletableFuture
26 0
|
8月前
|
Java 数据处理 数据库
CompletableFuture 使用
CompletableFuture 使用
42 0
|
9月前
|
消息中间件 Java 中间件
Future and CompletableFuture
Future代表异步执行的结果,也就是说异步执行完毕后,结果保存在Future里, 我们在使用线程池submit()时需要传入Callable接口,线程池的返回值为一个Future,而Future则保存了执行的结果 ,可通过Future的get()方法取出结果,如果线程池使用的是execute(),则传入的是Runnable接口 无返回值。
39 0
|
10月前
|
Java Linux
JUC--CompletableFuture上
Future接口(FutureTask实现类)定义了操作异步任务执行一些方法,如获取异步任务的执行结果,取消任务带带执行,判断任务是否被取消,判断任务执行是否完毕等。
|
Java 测试技术
CompletableFuture使用详解
CompletableFuture是jdk8的新特性。CompletableFuture实现了CompletionStage接口和Future接口
234 0
CompletableFuture使用详解
Zp
【CompletableFuture】CompletableFuture中join()和get()方法的区别
【CompletableFuture】CompletableFuture中join()和get()方法的区别
Zp
595 0
|
Java 测试技术 Linux
CompletableFuture调用OpenFegin,谁用谁傻
当调用FeignBlockdingloadBalancerClient使用并行流(多线程)会有问题。
278 0
CompletableFuture调用OpenFegin,谁用谁傻
|
Java
【多线程:join】join再理解
【多线程:join】join再理解
130 0
【多线程: join 方法详解】
【多线程: join 方法详解】
255 0