串行任务方法
thenRun & thenRunAsync (无返回值)
CompletableFuture<Void> future = CompletableFuture.runAsync(() -> {
System.out.println("执行异步任务");
}).thenRun(() -> {
System.out.println("执行异步任务完成后的回调");
});CompletableFuture<Void> future = CompletableFuture.runAsync(() -> {
System.out.println("执行异步任务");
}).thenRunAsync(() -> {
System.out.println("执行异步任务完成后的回调");
});thenAccept & thenAcceptAsync (无返回值)
CompletableFuture<Void> future = CompletableFuture.supplyAsync(() -> {
System.out.println("执行异步任务");
return "异步任务返回值";
}).thenAccept((String s) -> {
System.out.println("执行异步任务完成后的回调");
});CompletableFuture<Void> future = CompletableFuture.supplyAsync(() -> {
System.out.println("执行异步任务");
return "异步任务返回值";
}).thenAcceptAsync((String s) -> {
System.out.println("执行异步任务完成后的回调");
});whenComplete & whenCompleteAsync (无返回值)
thenApply & thenApplyAsync
handle & handleAsync
thenCompose & thenComposeAsync (返回新任务)
最后更新于