目錄
2. whenComplete、whenCompleteAsync、exceptionally
4.thenAccept 、thenRun 方法 消費處理結果
5. thenCombine 、 thenAcceptBoth
6.applyToEither 、 acceptEither
7. runAfterEither 、 runAfterBoth
9.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() {
@Override
public 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>() {
@Override
public 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>() {
@Override
public void accept(Void t, Throwable action) {
System.out.println("執行完成!");
}
});
future.exceptionally(new Function<Throwable, Void>() {
@Override
public 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>() {
@Override
public 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>() {
@Override
public Integer apply(Integer value) {
System.out.println("上一步結果:"+value);
System.out.println("返回結果:"+20);
return 20;
}
}).exceptionally(new Function<Throwable, Integer>() {
@Override
public 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>() {
@Override
public Integer get() {
int i= 10/0;
return new Random().nextInt(10);
}
}).handle(new BiFunction<Integer, Throwable, Integer>() {
@Override
public 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>() {
@Override
public Integer get() {
return new Random().nextInt(10);
}
}).thenAccept(new Consumer<Integer>() {
@Override
public 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>() {
@Override
public String get() {
return "第一步完成";
}
}).thenRun(new Runnable() {
@Override
public 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>() {
@Override
public String get() {
try {
TimeUnit.SECONDS.sleep(2);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "hello";
}
});
CompletableFuture<String> future2 = CompletableFuture.supplyAsync(new Supplier<String>() {
@Override
public String get() {
return "world";
}
});
CompletableFuture<String> result = future1.thenCombine(future2, new BiFunction<String, String, String>() {
@Override
public 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>() {
@Override
public String get() {
try {
TimeUnit.SECONDS.sleep(2);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "hello";
}
});
CompletableFuture<String> future2 = CompletableFuture.supplyAsync(new Supplier<String>() {
@Override
public String get() {
return "world";
}
});
future1.thenAcceptBoth(future2, new BiConsumer<String, String>() {
@Override
public 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 block
e.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>() {
@Override
public 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>() {
@Override
public 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>() {
@Override
public 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>() {
@Override
public 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>() {
@Override
public 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>() {
@Override
public 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>() {
@Override
public 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>() {
@Override
public 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() {
@Override
public 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>() {
@Override
public 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>() {
@Override
public 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() {
@Override
public 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>() {
@Override
public String get() {
try {
TimeUnit.SECONDS.sleep(2);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "hello";
}
});
CompletableFuture<String> future2 = CompletableFuture.supplyAsync(new Supplier<String>() {
@Override
public String get() {
return "world";
}
});
CompletableFuture<String> future3 = CompletableFuture.supplyAsync(new Supplier<String>() {
@Override
public String get() {
try {
TimeUnit.SECONDS.sleep(5);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.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>() {
@Override
public String get() {
try {
TimeUnit.SECONDS.sleep(2);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "hello";
}
});
CompletableFuture<String> future2 = CompletableFuture.supplyAsync(new Supplier<String>() {
@Override
public String get() {
try {
TimeUnit.SECONDS.sleep(2);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "world";
}
});
CompletableFuture<String> future3 = CompletableFuture.supplyAsync(new Supplier<String>() {
@Override
public String get() {
try {
TimeUnit.SECONDS.sleep(5);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.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 block
e.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 block
e.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 block
e.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();
}