public static void main(String[] args) {
try {
CompletableFuture<Void> f1 = async("f ");
f1.get();
CompletableFuture<Void> f2 = async("s ");
f2.get();
CompletableFuture<Void> f3 = async("t ");
f3.get();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
private static CompletableFuture<Void> async(final String str) {
return CompletableFuture.runAsync(() -> {
int i = 0;
while (i < 6) {
System.out.println(str+i);
try {
Thread.sleep(1000);
i++;
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
}
CompletableFuture案例
最新推荐文章于 2024-09-05 20:09:32 发布