目录
1. runAsync 和 supplyAsync方法
2. whenComplete、whenCompleteAsync、exceptionally
3. thenApply 、 handle
thenApply
handle
4.thenAccept 、thenRun 方法 消费处理结果
thenAccept
thenRun
5. thenCombine 、 thenAcceptBoth
thenCombine
thenAcceptBoth
6.applyToEither 、 acceptEither
applyToEither
acceptEither
7. runAfterEither 、 runAfterBoth
runAfterEither
runAfterBoth
8.complete
9.allOf 多实例同时返回 、anyOf 多实例一个执行完成返回
allOf
anyOf
1. runAsync 和 supplyAsync方法
- runAsync方法不支持返回值。
- supplyAsync可以支持返回值。
public static CompletableFuture<Void> runAsync(Runnable runnable)
public static CompletableFuture<Void> runAsync(Runnable runnable, Executor executor)
public static <U> CompletableFuture<U> supplyAsync(Supplier<U> supplier)
public static <U> CompletableFuture<U> supplyAsync(Supplier<U> supplier, Executor executor)
默认使用 ForkJoinPool.commonPool() 线程池执行,
runAsync使用(包含lambda调用方法)
public static void runAsync() throws Exception {CompletableFuture<Void> future = CompletableFuture.runAsync(() -> {try {TimeUnit.SECONDS.sleep(1);} catch (InterruptedException e) {e.printStackTrace();}System.out.println("执行完成");});future.get();}public static void runAsync2() throws Exception {CompletableFuture<Void> future = CompletableFuture.runAsync(new Runnable() {@Overridepublic void run() {try {TimeUnit.SECONDS.sleep(1);} catch (InterruptedException e) {e.printStackTrace();}System.out.println("执行完成");}});future.get();}
supplyAsync使用
public static void supplyAsync() throws Exception { CompletableFuture<Long> future = CompletableFuture.supplyAsync(() -> {try {TimeUnit.SECONDS.sleep(2);} catch (InterruptedException e) {e.printStackTrace();}System.out.println("执行完成");return System.currentTimeMillis();});long time = future.get();System.out.println("time = "+time);}public static void supplyAsync2() throws Exception { CompletableFuture<Long> future = CompletableFuture.supplyAsync(new Supplier<Long>() {@Overridepublic Long get() {try {TimeUnit.SECONDS.sleep(2);} catch (InterruptedException e) {e.printStackTrace();}System.out.println("执行完成");return System.currentTimeMillis();}});long time = future.get();System.out.println("time = "+time);}
2. whenComplete、whenCompleteAsync、exceptionally
计算完成后执行的回调方法
public CompletableFuture<T> whenComplete(BiConsumer<? super T,? super Throwable> action)
public CompletableFuture<T> whenCompleteAsync(BiConsumer<? super T,? super Throwable> action)
public CompletableFuture<T> whenCompleteAsync(BiConsumer<? super T,? super Throwable> action, Executor executor)
public CompletableFuture<T> exceptionally(Function<Throwable,? extends T> fn)
whenComplete 和 whenCompleteAsync 的区别:
whenComplete:是执行当前任务的线程执行继续执行 whenComplete 的任务。
whenCompleteAsync:是执行把 whenCompleteAsync 这个任务继续提交给线程池来进行执行。
exceptionally:异常处理方法
public static void whenComplete() throws Exception {CompletableFuture<Void> future = CompletableFuture.runAsync(() -> {try {TimeUnit.SECONDS.sleep(1);} catch (InterruptedException e) {}if(new Random().nextInt()%2>0) {int i = 1/0;}});future.whenComplete(new BiConsumer<Void, Throwable>() {@Overridepublic void accept(Void t, Throwable action) {System.out.println("执行完成!");}});future.exceptionally(new Function<Throwable, Void>() {@Overridepublic Void apply(Throwable t) {System.out.println("执行失败!"+t.getMessage());return null;}});future.get();}public static void whenComplete2() throws Exception {String string = CompletableFuture.supplyAsync(() -> {try {TimeUnit.SECONDS.sleep(1);} catch (InterruptedException e) {}if(new Random().nextInt()%2>0) {int i = 1/0;}return "返回结果";}).whenComplete((value,throwable)->{if(throwable!=null) {System.out.println("异常:"+throwable);}else {System.out.println("value:"+value);}}).get();System.out.println("执行完:"+string);
// future.exceptionally(new Function<Throwable, Void>() {
// @Override
// public Void apply(Throwable t) {
// System.out.println("执行失败!"+t.getMessage());
// return null;
// }
// });}
3. thenApply 、 handle
thenApply
串行执行,第二次执行需要依赖第一次的执行结果
private static void thenApply() throws Exception {CompletableFuture<Integer> future = CompletableFuture.supplyAsync(new Supplier<Integer>() {@Overridepublic Integer get() {try {TimeUnit.SECONDS.sleep(1);} catch (InterruptedException e) {e.printStackTrace();}if(new Random().nextInt()%2>0) {int i = 1/0;}return 10;}}).thenApply(new Function<Integer, Integer>() {@Overridepublic Integer apply(Integer value) {System.out.println("上一步结果:"+value);System.out.println("返回结果:"+20);return 20;}}).exceptionally(new Function<Throwable, Integer>() {@Overridepublic Integer apply(Throwable t) {System.out.println("异常返回:"+ t);return -1;}});long result = future.get();System.out.println("最终结果:"+result);}private static void thenApply2() throws Exception {Integer integer = CompletableFuture.supplyAsync(()->{try {TimeUnit.SECONDS.sleep(1);} catch (InterruptedException e) {}if(new Random().nextInt()%2>0) {int i = 1/0;}return 10;}).thenApply((value)->{System.out.println("上一步结果:"+value);System.out.println("返回结果:"+20);return 20;}).exceptionally((throwable)->{System.out.println("异常返回:"+throwable);return -1;}).get();System.out.println("最终结果:"+integer);}
handle
handle 是执行任务完成时对结果的处理。
handle 方法和 thenApply 方法处理方式基本一样。不同的是 handle 是在任务完成后再执行,还可以处理异常的任务。thenApply 只可以执行正常的任务,任务出现异常则不执行 thenApply 方法。
public <U> CompletionStage<U> handle(BiFunction<? super T, Throwable, ? extends U> fn);
public <U> CompletionStage<U> handleAsync(BiFunction<? super T, Throwable, ? extends U> fn);
public <U> CompletionStage<U> handleAsync(BiFunction<? super T, Throwable, ? extends U> fn,Executor executor);
public static void handle() throws Exception{CompletableFuture<Integer> future = CompletableFuture.supplyAsync(new Supplier<Integer>() {@Overridepublic Integer get() {int i= 10/0;return new Random().nextInt(10);}}).handle(new BiFunction<Integer, Throwable, Integer>() {@Overridepublic Integer apply(Integer param, Throwable throwable) {int result = -1;if(throwable==null){result = param * 2;}else{System.out.println(throwable.getMessage());}return result;}});System.out.println(future.get());}public static void handle2() throws Exception{CompletableFuture<Integer> future = CompletableFuture.supplyAsync(()->{int i= 10/0;return new Random().nextInt(10);}).handle((value,throwable)->{int result = -1;if(throwable==null){result = value * 2;}else{System.out.println(throwable.getMessage());}return result;});System.out.println(future.get());}
4.thenAccept 、thenRun 方法 消费处理结果
thenAccept
接收任务的处理结果,并消费处理,无返回结果。
public CompletionStage<Void> thenAccept(Consumer<? super T> action);
public CompletionStage<Void> thenAcceptAsync(Consumer<? super T> action);
public CompletionStage<Void> thenAcceptAsync(Consumer<? super T> action,Executor executor);
public static void thenAccept() throws Exception{CompletableFuture<Void> future = CompletableFuture.supplyAsync(()->{return new Random().nextInt(10);}).thenAccept(integer -> {System.out.println(integer);});future.get();}public static void thenAccept2() throws Exception{CompletableFuture<Void> future = CompletableFuture.supplyAsync(new Supplier<Integer>() {@Overridepublic Integer get() {return new Random().nextInt(10);}}).thenAccept(new Consumer<Integer>() {@Overridepublic void accept(Integer t) {System.out.println(t);}});future.get();}
thenRun
该方法同 thenAccept 方法类似。不同的是上个任务处理完成后,并不会把计算的结果传给 thenRun 方法。只是处理完任务后,执行 thenAccept 的后续操作。
public CompletionStage<Void> thenRun(Runnable action);
public CompletionStage<Void> thenRunAsync(Runnable action);
public CompletionStage<Void> thenRunAsync(Runnable action,Executor executor);
public static void thenRun() throws Exception{CompletableFuture<Void> future = CompletableFuture.supplyAsync(new Supplier<String>() {@Overridepublic String get() {return "第一步完成";}}).thenRun(new Runnable() {@Overridepublic void run() {System.out.println("执行完成");}});future.get();}public static void thenRun2() throws Exception{CompletableFuture<Void> future = CompletableFuture.supplyAsync(()->{return "第一步完成";}).thenRun(() -> {System.out.println("执行完成");});future.get();}
5. thenCombine 、 thenAcceptBoth
合并两个任务执行结果,进行操作
thenCombine
thenCombine 会把 两个 CompletionStage 的任务都执行完成后,把两个任务的结果一块交给 thenCombine 来处理。
public <U,V> CompletionStage<V> thenCombine(CompletionStage<? extends U> other,BiFunction<? super T,? super U,? extends V> fn);
public <U,V> CompletionStage<V> thenCombineAsync(CompletionStage<? extends U> other,BiFunction<? super T,? super U,? extends V> fn);
public <U,V> CompletionStage<V> thenCombineAsync(CompletionStage<? extends U> other,BiFunction<? super T,? super U,? extends V> fn,Executor executor);
private static void thenCombine() throws Exception {CompletableFuture<String> future1 = CompletableFuture.supplyAsync(new Supplier<String>() {@Overridepublic String get() {try {TimeUnit.SECONDS.sleep(2);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}return "hello";}});CompletableFuture<String> future2 = CompletableFuture.supplyAsync(new Supplier<String>() {@Overridepublic String get() {return "world";}});CompletableFuture<String> result = future1.thenCombine(future2, new BiFunction<String, String, String>() {@Overridepublic String apply(String t, String u) {return t+" "+u;}});System.out.println(result.get());}private static void thenCombine2() throws Exception {CompletableFuture<String> future1 = CompletableFuture.supplyAsync(()->{try {TimeUnit.SECONDS.sleep(2);} catch (InterruptedException e) {e.printStackTrace();}return "hello";});CompletableFuture<String> future2 = CompletableFuture.supplyAsync(()->{return "world";});CompletableFuture<String> result = future1.thenCombine(future2, (v1,v2)->{return v1+" "+v2;});System.out.println(result.get());}
thenAcceptBoth
当两个CompletionStage都执行完成后,把结果一块交给thenAcceptBoth来进行消耗,
与 thenCombine 区别在于,thenAcceptBoth无返回值
public <U> CompletionStage<Void> thenAcceptBoth(CompletionStage<? extends U> other,BiConsumer<? super T, ? super U> action);
public <U> CompletionStage<Void> thenAcceptBothAsync(CompletionStage<? extends U> other,BiConsumer<? super T, ? super U> action);
public <U> CompletionStage<Void> thenAcceptBothAsync(CompletionStage<? extends U> other,BiConsumer<? super T, ? super U> action, Executor executor);
private static void thenAcceptBoth() throws Exception {CompletableFuture<String> future1 = CompletableFuture.supplyAsync(new Supplier<String>() {@Overridepublic String get() {try {TimeUnit.SECONDS.sleep(2);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}return "hello";}});CompletableFuture<String> future2 = CompletableFuture.supplyAsync(new Supplier<String>() {@Overridepublic String get() {return "world";}});future1.thenAcceptBoth(future2, new BiConsumer<String, String>() {@Overridepublic void accept(String t, String u) {System.out.println( t+" "+u);}});}private static void thenAcceptBoth2() throws Exception {CompletableFuture<String> future1 = CompletableFuture.supplyAsync(()->{try {TimeUnit.SECONDS.sleep(2);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}return "hello";});CompletableFuture<String> future2 = CompletableFuture.supplyAsync(()->{return "world";});future1.thenAcceptBoth(future2, (v1,v2)->{System.out.println( v1+" "+v2);});}
6.applyToEither 、 acceptEither
两个CompletionStage,谁执行返回的结果快,就用那个CompletionStage的结果进行下一步的转化操作。
applyToEither 有返回值,acceptEither 无返回值
applyToEither
public <U> CompletionStage<U> applyToEither(CompletionStage<? extends T> other,Function<? super T, U> fn);
public <U> CompletionStage<U> applyToEitherAsync(CompletionStage<? extends T> other,Function<? super T, U> fn);
public <U> CompletionStage<U> applyToEitherAsync(CompletionStage<? extends T> other,Function<? super T, U> fn,Executor executor);
private static void applyToEither() throws Exception {CompletableFuture<Integer> f1 = CompletableFuture.supplyAsync(new Supplier<Integer>() {@Overridepublic Integer get() {int t = new Random().nextInt(3);try {TimeUnit.SECONDS.sleep(t);} catch (InterruptedException e) {e.printStackTrace();}System.out.println("f1="+t);return t;}});CompletableFuture<Integer> f2 = CompletableFuture.supplyAsync(new Supplier<Integer>() {@Overridepublic Integer get() {int t = new Random().nextInt(3);try {TimeUnit.SECONDS.sleep(t);} catch (InterruptedException e) {e.printStackTrace();}System.out.println("f2="+t);return t;}});CompletableFuture<Integer> result = f1.applyToEither(f2, new Function<Integer, Integer>() {@Overridepublic Integer apply(Integer t) {System.out.println(t);return t * 2;}});System.out.println("结果:"+result.get());}private static void applyToEither2() throws Exception {CompletableFuture<Integer> f1 = CompletableFuture.supplyAsync(()->{int t = new Random().nextInt(3);try {TimeUnit.SECONDS.sleep(t);} catch (InterruptedException e) {e.printStackTrace();}System.out.println("f1="+t);return t;});CompletableFuture<Integer> f2 = CompletableFuture.supplyAsync(()->{int t = new Random().nextInt(3);try {TimeUnit.SECONDS.sleep(t);} catch (InterruptedException e) {e.printStackTrace();}System.out.println("f2="+t);return t;});CompletableFuture<Integer> result = f1.applyToEither(f2, (value)->{System.out.println(value);return value * 2;});System.out.println("结果:"+result.get());}
acceptEither
public CompletionStage<Void> acceptEither(CompletionStage<? extends T> other,Consumer<? super T> action);
public CompletionStage<Void> acceptEitherAsync(CompletionStage<? extends T> other,Consumer<? super T> action);
public CompletionStage<Void> acceptEitherAsync(CompletionStage<? extends T> other,Consumer<? super T> action,Executor executor);
private static void acceptEither() throws Exception {CompletableFuture<Integer> f1 = CompletableFuture.supplyAsync(new Supplier<Integer>() {@Overridepublic Integer get() {int t = new Random().nextInt(3);try {TimeUnit.SECONDS.sleep(t);} catch (InterruptedException e) {e.printStackTrace();}System.out.println("f1="+t);return t;}});CompletableFuture<Integer> f2 = CompletableFuture.supplyAsync(new Supplier<Integer>() {@Overridepublic Integer get() {int t = new Random().nextInt(3);try {TimeUnit.SECONDS.sleep(t);} catch (InterruptedException e) {e.printStackTrace();}System.out.println("f2="+t);return t;}});CompletableFuture<Void> acceptEither = f1.acceptEither(f2, new Consumer<Integer>() {@Overridepublic void accept(Integer t) {System.out.println("结果:"+t);}});}private static void acceptEither2() throws Exception {CompletableFuture<Integer> f1 = CompletableFuture.supplyAsync(()->{int t = new Random().nextInt(3);try {TimeUnit.SECONDS.sleep(t);} catch (InterruptedException e) {e.printStackTrace();}System.out.println("f1="+t);return t;});CompletableFuture<Integer> f2 = CompletableFuture.supplyAsync(()->{int t = new Random().nextInt(3);try {TimeUnit.SECONDS.sleep(t);} catch (InterruptedException e) {e.printStackTrace();}System.out.println("f2="+t);return t;});CompletableFuture<Void> acceptEither = f1.acceptEither(f2, (value)->{System.out.println("结果:"+value);});}
7. runAfterEither 、 runAfterBoth
runAfterEither
两个CompletionStage,任何一个完成了都会执行下一步的操作
public CompletionStage<Void> runAfterEither(CompletionStage<?> other,Runnable action);
public CompletionStage<Void> runAfterEitherAsync(CompletionStage<?> other,Runnable action);
public CompletionStage<Void> runAfterEitherAsync(CompletionStage<?> other,Runnable action,Executor executor);
private static void runAfterEither() throws Exception {CompletableFuture<Integer> f1 = CompletableFuture.supplyAsync(new Supplier<Integer>() {@Overridepublic Integer get() {int t = new Random().nextInt(3);try {TimeUnit.SECONDS.sleep(t);} catch (InterruptedException e) {e.printStackTrace();}System.out.println("f1="+t);return t;}});CompletableFuture<Integer> f2 = CompletableFuture.supplyAsync(new Supplier<Integer>() {@Overridepublic Integer get() {int t = new Random().nextInt(3);try {TimeUnit.SECONDS.sleep(t);} catch (InterruptedException e) {e.printStackTrace();}System.out.println("f2="+t);return t;}});f1.runAfterEither(f2, new Runnable() {@Overridepublic void run() {System.out.println("上面有一个已经完成了。");}}).get();}private static void runAfterEither1() throws Exception {CompletableFuture<Integer> f1 = CompletableFuture.supplyAsync(()->{int t = new Random().nextInt(3);try {TimeUnit.SECONDS.sleep(t);} catch (InterruptedException e) {e.printStackTrace();}System.out.println("f1="+t);return t;});CompletableFuture<Integer> f2 = CompletableFuture.supplyAsync(()->{int t = new Random().nextInt(3);try {TimeUnit.SECONDS.sleep(t);} catch (InterruptedException e) {e.printStackTrace();}System.out.println("f2="+t);return t;});f1.runAfterEither(f2, ()->{System.out.println("上面有一个已经完成了。");}).get();}
runAfterBoth
两个CompletionStage,都完成了计算才会执行下一步的操作
public CompletionStage<Void> runAfterBoth(CompletionStage<?> other,Runnable action);
public CompletionStage<Void> runAfterBothAsync(CompletionStage<?> other,Runnable action);
public CompletionStage<Void> runAfterBothAsync(CompletionStage<?> other,Runnable action,Executor executor);
private static void runAfterBoth() throws Exception {CompletableFuture<Integer> f1 = CompletableFuture.supplyAsync(new Supplier<Integer>() {@Overridepublic Integer get() {int t = new Random().nextInt(3);try {TimeUnit.SECONDS.sleep(t);} catch (InterruptedException e) {e.printStackTrace();}System.out.println("f1="+t);return t;}});CompletableFuture<Integer> f2 = CompletableFuture.supplyAsync(new Supplier<Integer>() {@Overridepublic Integer get() {int t = new Random().nextInt(3);try {TimeUnit.SECONDS.sleep(t);} catch (InterruptedException e) {e.printStackTrace();}System.out.println("f2="+t);return t;}});f1.runAfterBoth(f2, new Runnable() {@Overridepublic void run() {System.out.println("上面两个任务都执行完成了。");}}).get();}private static void runAfterBoth2() throws Exception {CompletableFuture<Integer> f1 = CompletableFuture.supplyAsync(()->{int t = new Random().nextInt(3);try {TimeUnit.SECONDS.sleep(t);} catch (InterruptedException e) {e.printStackTrace();}System.out.println("f1="+t);return t;});CompletableFuture<Integer> f2 = CompletableFuture.supplyAsync(()->{int t = new Random().nextInt(3);try {TimeUnit.SECONDS.sleep(t);} catch (InterruptedException e) {e.printStackTrace();}System.out.println("f2="+t);return t;});f1.runAfterBoth(f2, ()->{System.out.println("上面两个任务都执行完成了。");}).get();}
8.complete
runAsync 执行完成则以该结果为准;如果未执行完成时执行complete,则返回complete值
public static void complete() throws Exception {CompletableFuture<Integer> runAsync = CompletableFuture.supplyAsync(()->{try {TimeUnit.SECONDS.sleep(1);} catch (InterruptedException e) {e.printStackTrace();}System.out.println("run end ...");return 1;});TimeUnit.SECONDS.sleep(2);// runAsync 执行完成则以该结果为准;如果未执行完成时执行complete,则返回complete值runAsync.complete(-1);System.out.println("success:"+ runAsync.get());}
9.allOf 多实例同时返回 、anyOf 多实例一个执行完成返回
allOf
在这里我们可以将对各future实例添加到allOf方法中,然后通过future的get()方法获取future的状态。如果allOf里面的所有线程为执行完毕,主线程会阻塞,直到allOf里面的所有线程都执行,线程就会被唤醒。
CompletableFuture<String> future1 = CompletableFuture.supplyAsync(new Supplier<String>() {@Overridepublic String get() {try {TimeUnit.SECONDS.sleep(2);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}return "hello";}});CompletableFuture<String> future2 = CompletableFuture.supplyAsync(new Supplier<String>() {@Overridepublic String get() {return "world";}});CompletableFuture<String> future3 = CompletableFuture.supplyAsync(new Supplier<String>() {@Overridepublic String get() {try {TimeUnit.SECONDS.sleep(5);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}return "!!!";}});long currentTimeMillis = System.currentTimeMillis();System.out.println(future1.isDone()+" "+future2.isDone()+" "+future3.isDone());CompletableFuture<Void> allOf = CompletableFuture.allOf(future1,future2,future3);allOf.get();//这里会等待三个任务都执行完成System.out.println("执行完成,时间:"+(System.currentTimeMillis()-currentTimeMillis));System.out.println(future1.isDone()+" "+future2.isDone()+" "+future3.isDone());
anyOf
调用方法与allOf基本相同,区别在于anyOf是,其中一个执行完成则往下执行
anyOf get()方法返回最先执行完的Future
CompletableFuture<String> future1 = CompletableFuture.supplyAsync(new Supplier<String>() {@Overridepublic String get() {try {TimeUnit.SECONDS.sleep(2);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}return "hello";}});CompletableFuture<String> future2 = CompletableFuture.supplyAsync(new Supplier<String>() {@Overridepublic String get() {try {TimeUnit.SECONDS.sleep(2);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}return "world";}});CompletableFuture<String> future3 = CompletableFuture.supplyAsync(new Supplier<String>() {@Overridepublic String get() {try {TimeUnit.SECONDS.sleep(5);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}return "!!!";}});long currentTimeMillis = System.currentTimeMillis();System.out.println(future1.isDone()+" "+future2.isDone()+" "+future3.isDone());// CompletableFuture<Void> allOf = CompletableFuture.allOf(future1,future2,future3);// allOf.thenAccept((x)->{// System.out.println("123:"+x);// }).get();CompletableFuture<Object> anyOf = CompletableFuture.anyOf(future1,future2,future3);System.out.println("结果:"+anyOf.get());System.out.println("执行完成,时间:"+(System.currentTimeMillis()-currentTimeMillis));System.out.println(future1.isDone()+" "+future2.isDone()+" "+future3.isDone());
10.所有方法 Async 与 非Async 区别
比如:thenApply与thenApplyAsync 等等
参考:https://blog.csdn.net/leon_wzm/article/details/80560081
public static void main(String[] args) throws Exception {async1();async2();async3();}public static void async1() throws Exception {CompletableFuture<String> future1 = CompletableFuture.supplyAsync(()->{try {TimeUnit.SECONDS.sleep(2);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}System.out.println("supplyAsync:"+Thread.currentThread().getName() +" "+Thread.currentThread().getId());return "hello";});TimeUnit.SECONDS.sleep(5);CompletableFuture<Integer> thenApply = future1.thenApply((x)->{System.out.println("thenApply:"+Thread.currentThread().getName() +" "+Thread.currentThread().getId());return 1;});thenApply.get();}public static void async2() throws Exception {CompletableFuture<String> future1 = CompletableFuture.supplyAsync(()->{try {TimeUnit.SECONDS.sleep(2);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}System.out.println("supplyAsync:"+Thread.currentThread().getName() +" "+Thread.currentThread().getId());return "hello";});CompletableFuture<Integer> thenApply = future1.thenApply((x)->{System.out.println("thenApply:"+Thread.currentThread().getName() +" "+Thread.currentThread().getId());return 1;});thenApply.get();}public static void async3() throws Exception {CompletableFuture<String> future1 = CompletableFuture.supplyAsync(()->{try {TimeUnit.SECONDS.sleep(2);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}System.out.println("supplyAsync:"+Thread.currentThread().getName() +" "+Thread.currentThread().getId());return "hello";});TimeUnit.SECONDS.sleep(5);CompletableFuture<Integer> thenApply = future1.thenApplyAsync((x)->{System.out.println("thenApply:"+Thread.currentThread().getName() +" "+Thread.currentThread().getId());return 1;});thenApply.get();}