public static void main(String[] args) throws ExecutionException, InterruptedException {
final int num = 100;
CompletableFuture cf1 = CompletableFuture.completedFuture(num)
.thenApply(sNum -> {
System.out.println("first start");
return sNum + 100;
}).thenAcceptAsync(aNum ->{
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("first end");
});
CompletableFuture cf2 = CompletableFuture.completedFuture(num)
.thenApply(sNum -> {
System.out.println("second start");
return sNum;
}).thenAcceptAsync(aNum ->{
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("second end");
});
CompletableFuture.allOf(cf1, cf2).join();
System.out.println("all end");
}