Multithreading 主线程没有等待工作线程在可完成的将来完成
//主线程没有等待工作线程在可完成的将来完成。当我在Completable上运行System.out.println时,它们没有按顺序运行Multithreading 主线程没有等待工作线程在可完成的将来完成,multithreading,java-8,parallel-processing,Multithreading,Java 8,Parallel Processing,//主线程没有等待工作线程在可完成的将来完成。当我在Completable上运行System.out.println时,它们没有按顺序运行 public class CompletableFutureMain { public static void main(String[] args) { System.out.println("Main Task : Thread Name=" + Thread.currentThread().getName());
public class CompletableFutureMain {
public static void main(String[] args) {
System.out.println("Main Task : Thread Name=" + Thread.currentThread().getName());
CompletableFuture<String> stringCompletableFuture = null;
CompletableFuture<Integer> intCompletableFuture = null;
stringCompletableFuture = CompletableFuture.supplyAsync(() -> {
System.out.println("Task 1 : Thread Name=" + Thread.currentThread().getName());
return getMeWelcomeMessage();
});
intCompletableFuture = CompletableFuture.supplyAsync(() -> {
System.out.println("Task 2 : Thread Name=" + Thread.currentThread().getName());
return getMeARandomInteger();
});
System.out.println(stringCompletableFuture.get());
System.out.println(intCompletableFuture.get().toString());
}
private static String getMeWelcomeMessage() {
return "Trying Completable Future";
}
private static Integer getMeARandomInteger() {
return new Random().nextInt(100);
}
private static Double getMeARandomDouble() {
return new Random().nextDouble();
}
}
公共类CompletableFutureMain{
公共静态void main(字符串[]args){
System.out.println(“主任务:线程名称=“+Thread.currentThread().getName()”);
CompletableFuture stringCompletableFuture=null;
CompletableFuture intCompletableFuture=null;
stringCompletableFuture=CompletableFuture.SupplySync(()->{
System.out.println(“任务1:Thread Name=“+Thread.currentThread().getName());
返回getMeWelcomeMessage();
});
intCompletableFuture=CompletableFuture.SupplySync(()->{
System.out.println(“任务2:Thread Name=“+Thread.currentThread().getName());
返回getMeARandomInteger();
});
System.out.println(stringCompletableFuture.get());
System.out.println(intCompletableFuture.get().toString());
}
私有静态字符串getMeWelcomeMessage(){
返回“尝试可完成的未来”;
}
私有静态整数getMeARandomInteger(){
返回新的Random().nextInt(100);
}
私有静态双getMeARandomDouble(){
返回新的Random().nextDouble();
}
}
看起来您并不是在等待所有Completable加入。
加入completableFutures后运行System.out.println()
CompletableFuture.allOf(stringCompletableFuture, intCompletableFuture).join();
此外,completable.get()还可能导致java.lang.InteruptedException和java.util.concurrent.ExecutionException。您需要处理这些已检查的异常。
示例代码段
public class CompletableFutureMain {
public static void main(String[] args) {
System.out.println("Main Task : Thread Name=" + Thread.currentThread().getName());
CompletableFuture<String> stringCompletableFuture = null;
CompletableFuture<Integer> intCompletableFuture = null;
stringCompletableFuture = CompletableFuture.supplyAsync(() -> {
System.out.println("Task 1 : Thread Name=" + Thread.currentThread().getName());
return getMeWelcomeMessage();
});
intCompletableFuture = CompletableFuture.supplyAsync(() -> {
System.out.println("Task 2 : Thread Name=" + Thread.currentThread().getName());
return getMeARandomInteger();
});
CompletableFuture.allOf(stringCompletableFuture, intCompletableFuture).join();
try {
System.out.println(stringCompletableFuture.get());
System.out.println(intCompletableFuture.get().toString());
} catch (Exception exp) {
System.out.println(exp.getMessage());
}
}
private static String getMeWelcomeMessage() {
return "Trying Completable Future";
}
private static Integer getMeARandomInteger() {
return new Random().nextInt(100);
}
private static Double getMeARandomDouble() {
return new Random().nextDouble();
}
}
公共类CompletableFutureMain{
公共静态void main(字符串[]args){
System.out.println(“主任务:线程名称=“+Thread.currentThread().getName()”);
CompletableFuture stringCompletableFuture=null;
CompletableFuture intCompletableFuture=null;
stringCompletableFuture=CompletableFuture.SupplySync(()->{
System.out.println(“任务1:Thread Name=“+Thread.currentThread().getName());
返回getMeWelcomeMessage();
});
intCompletableFuture=CompletableFuture.SupplySync(()->{
System.out.println(“任务2:Thread Name=“+Thread.currentThread().getName());
返回getMeARandomInteger();
});
allOf(stringCompletableFuture,intCompletableFuture).join();
试一试{
System.out.println(stringCompletableFuture.get());
System.out.println(intCompletableFuture.get().toString());
}捕获(异常扩展){
System.out.println(exp.getMessage());
}
}
私有静态字符串getMeWelcomeMessage(){
返回“尝试可完成的未来”;
}
私有静态整数getMeARandomInteger(){
返回新的Random().nextInt(100);
}
私有静态双getMeARandomDouble(){
返回新的Random().nextDouble();
}
}
由于get()
确实在等待结果,因此无需执行allOf(…).join()
。这种冗余等待对结果没有影响。为什么要按顺序运行?您已经启动了两个异步操作。这意味着不存在排序关系。等待和它有什么关系?当你在等火车时,火车时刻表会改变吗?