邵阳市住房和城乡建设局网站,全网vip视频网站怎么做,科技股,wordpress 微博同步背景 CompletableFuture是Java 8中引入的一个类#xff0c;它实现了Future和CompletionStage接口#xff0c;用于表示异步计算的结果。使用CompletableFuture可以方便地编写异步编程的代码#xff0c;并且可以链式地组合多个异步操作。 接口
CompletableFuture实现了Future…背景 CompletableFuture是Java 8中引入的一个类它实现了Future和CompletionStage接口用于表示异步计算的结果。使用CompletableFuture可以方便地编写异步编程的代码并且可以链式地组合多个异步操作。 接口
CompletableFuture实现了Future接口和CompletionStage接口
public class CompletableFutureT implements FutureT, CompletionStageT {//...
} 常用API
CompletableFuture 提供了很多实用的 API 来处理异步计算的结果。以下是 CompletableFuture 的一些常用 API
supplyAsync 含义传入一个Supplier返回一个新的 CompletableFuture public static U CompletableFutureU supplyAsync(SupplierU supplier) {return asyncSupplyStage(ASYNC_POOL, supplier);
}public static U CompletableFutureU supplyAsync(SupplierU supplier,Executor executor) {return asyncSupplyStage(screenExecutor(executor), supplier);
}
runAsync 含义传入一个Runnable返回一个新的CompletableFuture public static CompletableFutureVoid runAsync(Runnable runnable) {return asyncRunStage(ASYNC_POOL, runnable);
}public static CompletableFutureVoid runAsync(Runnable runnable,Executor executor) {return asyncRunStage(screenExecutor(executor), runnable);
}
thenApply 含义对于一个已完成的CompletableFuture的返回值进行同步操作 public U CompletableFutureU thenApply(Function? super T,? extends U fn) {return uniApplyStage(null, fn);
}
CompletableFutureString future CompletableFuture.supplyAsync(() - result_1);
CompletableFutureString future2 future.thenApply(result - result- result);
System.out.println(future2.get());
result-result_1
thenApplyAsync 含义对于一个已完成的CompletableFuture的返回值进行异步操作 public U CompletableFutureU thenApplyAsync(Function? super T,? extends U fn) {return uniApplyStage(defaultExecutor(), fn);
}public U CompletableFutureU thenApplyAsync(Function? super T,? extends U fn, Executor executor) {return uniApplyStage(screenExecutor(executor), fn);
}
thenAccept 含义 public CompletableFutureVoid thenAccept(Consumer? super T action) {return uniAcceptStage(null, action);
}
thenAcceptAsync 含义 public CompletableFutureVoid thenAcceptAsync(Consumer? super T action) {return uniAcceptStage(defaultExecutor(), action);
}public CompletableFutureVoid thenAcceptAsync(Consumer? super T action,Executor executor) {return uniAcceptStage(screenExecutor(executor), action);
}
thenRun 含义 public CompletableFutureVoid thenRun(Runnable action) {return uniRunStage(null, action);
}
thenRunAsync 含义 public CompletableFutureVoid thenRunAsync(Runnable action) {return uniRunStage(defaultExecutor(), action);
}public CompletableFutureVoid thenRunAsync(Runnable action,Executor executor) {return uniRunStage(screenExecutor(executor), action);
}
thenCombine 含义 public U,V CompletableFutureV thenCombine(CompletionStage? extends U other,BiFunction? super T,? super U,? extends V fn) {return biApplyStage(null, other, fn);
}
thenCombineAsync 含义 public U,V CompletableFutureV thenCombineAsync(CompletionStage? extends U other,BiFunction? super T,? super U,? extends V fn) {return biApplyStage(defaultExecutor(), other, fn);
}public U,V CompletableFutureV thenCombineAsync(CompletionStage? extends U other,BiFunction? super T,? super U,? extends V fn, Executor executor) {return biApplyStage(screenExecutor(executor), other, fn);
}
示例
1. 创建CompletableFuture实例可以通过无参数的构造函数来创建一个表示异步计算结果的CompletableFuture实例。
CompletableFutureString future new CompletableFuture();
2. 提交异步任务可以使用CompletableFuture的静态方法supplyAsync或runAsync来提交异步任务。supplyAsync接受一个Supplier函数式接口表示异步计算的任务runAsync接受一个Runnable接口表示异步执行的任务。这两个方法都可以指定执行异步任务的线程池。
CompletableFutureString future CompletableFuture.supplyAsync(() - { // 异步计算任务 return Hello;
}); CompletableFutureVoid voidFuture CompletableFuture.runAsync(() - { // 异步执行任务
});
完成Future当异步计算任务完成后可以使用CompletableFuture的complete方法来完成Future并设置结果值。如果异步计算任务抛出异常可以使用completeExceptionally方法来完成Future并设置异常信息。
future.complete(Hello);
future.completeExceptionally(new Exception(Error));
获取结果可以使用CompletableFuture的get方法来获取异步计算的结果。get方法会阻塞当前线程直到异步计算完成并返回结果。如果异步计算抛出异常get方法会抛出ExecutionException异常。如果需要添加超时时间可以使用get方法的重载版本。
String result future.get(); // 阻塞当前线程直到异步计算完成并返回结果
链式组合异步操作可以使用CompletableFuture的thenApply、thenAccept、thenRun等链式方法来组合多个异步操作。这些方法接受一个函数式接口作为参数用于处理前一个CompletableFuture的结果。例如可以使用thenApply方法对前一个CompletableFuture的结果进行转换并返回一个新的CompletableFuture。
CompletableFutureString newFuture future.thenApply(s - s.toUpperCase());