thenAcceptBothAsync
thenAcceptBothAsync(CompletionStage<? extends U> other,
BiConsumer<? super T, ? super U> action)
同时执行两个线程,获取两个返回结果T,U作为参数,进入BiConsumer<T, U>,retrun Void
,返回 CompletableFuture<Void>
public static void main(String[] args) {
CompletableFuture<Void> voidCompletableFuture = CompletableFuture.supplyAsync(() -> {
System.out.println(Thread.currentThread().getName() + "---------supplyAsync1---------开始---");
try {
TimeUnit.SECONDS.sleep(1);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + "---------supplyAsync1---------结束---");
return "hello";
}).thenAcceptBothAsync(CompletableFuture.supplyAsync(() -> {
System.out.println(Thread.currentThread().getName() + "---------supplyAsync2---------开始---");
System.out.println(Thread.currentThread().getName() + "---------supplyAsync2---------结束---");
return "hello".length();
}), (t, v) -> {
try {
TimeUnit.SECONDS.sleep(5);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + "........." + t + "---" + v);
});
System.out.println("---------------------------");
voidCompletableFuture.join();
}
acceptEitherAsync
acceptEitherAsync(
CompletionStage<? extends T> other, Consumer<? super T> action)
获取快的线程的结果(两个线程返回类型需一致)T,T作为参数,进入Consumer,return void,返回CompletableFuture<Void>
public static void main(String[] args) {
CompletableFuture<Void> voidCompletableFuture = CompletableFuture.supplyAsync(() -> {
System.out.println(Thread.currentThread().getName() + "---------supplyAsync1---------开始---");
System.out.println(Thread.currentThread().getName() + "---------supplyAsync1---------结束---");
return "hello1";
}).acceptEitherAsync(CompletableFuture.supplyAsync(() -> {
System.out.println(Thread.currentThread().getName() + "---------supplyAsync2---------开始---");
System.out.println(Thread.currentThread().getName() + "---------supplyAsync2---------结束---");
return "hello2";
}), t -> {
try {
TimeUnit.SECONDS.sleep(5);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + "........." + t);
});
System.out.println("---------------------------");
voidCompletableFuture.join();
}
runAfterBothAsync
runAfterBothAsync(CompletionStage<?> other, Runnable action)
两个线程执行完毕后,不关心结果,进入Runnable ,return void,返回CompletableFuture<Void>
public static void main(String[] args) {
CompletableFuture<Void> voidCompletableFuture = CompletableFuture.supplyAsync(() -> {
System.out.println(Thread.currentThread().getName() + "---------supplyAsync1---------开始---");
System.out.println(Thread.currentThread().getName() + "---------supplyAsync1---------结束---");
return "hello1";
}).runAfterBothAsync(CompletableFuture.supplyAsync(() -> {
System.out.println(Thread.currentThread().getName() + "---------supplyAsync2---------开始---");
try {
TimeUnit.SECONDS.sleep(5);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + "---------supplyAsync2---------结束---");
return "hello2".length();
}), () -> {
System.out.println(Thread.currentThread().getName() + ".........");
try {
TimeUnit.SECONDS.sleep(5);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + ".........");
});
System.out.println("---------------------------");
voidCompletableFuture.join();
}
runAfterEitherAsync
CompletableFuture<Void> runAfterEitherAsync(CompletionStage<?> other,
Runnable action)
有线程结束后,不关心结果,进入Runnable ,return void,返回CompletableFuture<Void>
public static void main(String[] args) {
CompletableFuture<Void> voidCompletableFuture = CompletableFuture.supplyAsync(() -> {
System.out.println(Thread.currentThread().getName() + "---------supplyAsync1---------开始---");
try {
TimeUnit.SECONDS.sleep(5);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + "---------supplyAsync1---------结束---");
return "hello1";
}).runAfterEitherAsync(CompletableFuture.supplyAsync(() -> {
System.out.println(Thread.currentThread().getName() + "---------supplyAsync2---------开始---");
System.out.println(Thread.currentThread().getName() + "---------supplyAsync2---------结束---");
return "hello2".length();
}), () -> {
System.out.println(Thread.currentThread().getName() + ".........");
try {
TimeUnit.SECONDS.sleep(3);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + ".........");
});
System.out.println("---------------------------");
voidCompletableFuture.join();
}
thenCombineAsync
CompletableFuture<V> thenCombineAsync(
CompletionStage<? extends U> other,
BiFunction<? super T,? super U,? extends V>
同时执行两个线程,获取两个返回结果T,U作为参数,进入BiFunction,return v , CompletableFuture<V>
public static void main(String[] args) {
CompletableFuture<String> completableFuture = CompletableFuture.supplyAsync(() -> {
System.out.println(Thread.currentThread().getName() + "---------supplyAsync1---------开始---");
System.out.println(Thread.currentThread().getName() + "---------supplyAsync1---------结束---");
return "hello1";
}).thenCombineAsync(CompletableFuture.supplyAsync(() -> {
System.out.println(Thread.currentThread().getName() + "---------supplyAsync2---------开始---");
System.out.println(Thread.currentThread().getName() + "---------supplyAsync2---------结束---");
return 100;
}), (t, u) -> {
System.out.println(Thread.currentThread().getName() + "........." + t);
try {
TimeUnit.SECONDS.sleep(3);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + "........." + u);
return "=thenCombine=";
});
try {
System.out.println("---------------------------" + completableFuture.get());
} catch (Exception e) {
e.printStackTrace();
}
}
thenComposeAsync
CompletableFuture<U> thenComposeAsync(
Function<? super T, ? extends CompletionStage<U>> fn)
public static void main(String[] args) {
CompletableFuture<Integer> completableFuture = CompletableFuture.supplyAsync(() -> {
System.out.println(Thread.currentThread().getName() + "---------supplyAsync1---------开始---");
try {
TimeUnit.SECONDS.sleep(3);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + "---------supplyAsync1---------结束---");
return "hello1";
}).thenComposeAsync(t -> {
CompletableFuture<Integer> integerCompletableFuture = CompletableFuture.supplyAsync(() -> {
System.out.println(Thread.currentThread().getName() + "---------supplyAsync2---------开始---" + t);
try {
TimeUnit.SECONDS.sleep(3);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + "---------supplyAsync2---------结束---" + t);
return 100;
});
return integerCompletableFuture;
});
try {
System.out.println("=----------------------------=");
System.out.println("---------------------------" + completableFuture.get());
} catch (Exception e) {
e.printStackTrace();
}
}
其它方法
- getNow(T valueIfAbsent):线程执行完了,获取返回的值,如果没有执行完毕,返回指定值。不阻塞。
public static void main(String[] args) {
CompletableFuture<String> stringCompletableFuture = CompletableFuture.supplyAsync(() -> {
System.out.println(Thread.currentThread().getName() + "---------supplyAsync----begin-------"+ System.currentTimeMillis());
try {
TimeUnit.SECONDS.sleep(3);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + "---------supplyAsync-----end------"+ System.currentTimeMillis());
return "hello";
});
//执行完了 获取返回的值,如果没有执行完毕,返回指定值
String now = stringCompletableFuture.getNow("i am getNow value");
System.err.println(Thread.currentThread().getName() + "=====getNow值:" + now + " ====时间: " + System.currentTimeMillis());
try {
System.err.println(Thread.currentThread().getName() + "====get值:" + stringCompletableFuture.get() + "===时间: " + System.currentTimeMillis());
} catch (Exception e) {
e.printStackTrace();
}
}
- complete(T value):若完成,返回false,get()时获取返回值; 若未完成,返回true,get()时获取指定值。
public static void main(String[] args) {
CompletableFuture<String> stringCompletableFuture = CompletableFuture.supplyAsync(() -> {
System.out.println(Thread.currentThread().getName() + "---------supplyAsync----begin-------"+ System.currentTimeMillis());
try {
TimeUnit.SECONDS.sleep(4);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + "---------supplyAsync-----end------"+ System.currentTimeMillis());
return "hello";
});
try {
TimeUnit.SECONDS.sleep(5);
} catch (Exception e) {
e.printStackTrace();
}
//若完成,返回false,get()时获取返回值; 若未完成,返回true,get()时获取指定值
boolean complete = stringCompletableFuture.complete("WWWWW");
System.out.println(complete);
try {
System.out.println(Thread.currentThread().getName() + " get值:" + stringCompletableFuture.get() + " 时间: " + System.currentTimeMillis());
} catch (Exception e) {
e.printStackTrace();
}
}
- completeExceptionally(Throwable ex)://若完成,返回false,get()时获取返回值; 若未完成,返回true,get()时抛出指定异常