类的用途
java.util.concurrent.CompletableFuture
使用 CompletableFuture 的包
包
描述
提供对 Java 编程语言的设计至关重要的类。
HTTP 客户端和 WebSocket API
通常在并发编程中有用的实用程序类。
-
CompletableFuture 在 java.lang 中的用法
java.lang 中返回 CompletableFuture 的方法 -
CompletableFuture 在 java.net.http 中的用法
java.net.http 中返回 CompletableFuture 的方法修饰符和类型方法描述WebSocket.Builder.buildAsync(URI uri, WebSocket.Listener listener) abstract <T> CompletableFuture<HttpResponse<T>>HttpClient.sendAsync(HttpRequest request, HttpResponse.BodyHandler<T> responseBodyHandler) 使用此客户端和给定的响应主体处理程序异步发送给定的请求。abstract <T> CompletableFuture<HttpResponse<T>>HttpClient.sendAsync(HttpRequest request, HttpResponse.BodyHandler<T> responseBodyHandler, HttpResponse.PushPromiseHandler<T> pushPromiseHandler) 使用具有给定响应主体处理程序和推送承诺处理程序的此客户端异步发送给定请求。WebSocket.sendBinary(ByteBuffer data, boolean last) 从给定缓冲区发送带有字节的二进制数据。通过发送带有给定状态代码和原因的 Close 消息,启动此 WebSocket 输出的有序关闭。WebSocket.sendPing(ByteBuffer message) 从给定缓冲区发送带有字节的 Ping 消息。WebSocket.sendPong(ByteBuffer message) 从给定缓冲区发送带有字节的 Pong 消息。WebSocket.sendText(CharSequence data, boolean last) 发送包含给定字符序列中的字符的文本数据。java.net.http 中的方法参数,类型参数为 CompletableFuture修饰符和类型方法描述voidHttpResponse.PushPromiseHandler.applyPushPromise(HttpRequest initiatingRequest, HttpRequest pushPromiseRequest, Function<HttpResponse.BodyHandler<T>, CompletableFuture<HttpResponse<T>>> acceptor) 传入推送承诺的通知。static <T> HttpResponse.PushPromiseHandler<T>HttpResponse.PushPromiseHandler.of(Function<HttpRequest, HttpResponse.BodyHandler<T>> pushPromiseHandler, ConcurrentMap<HttpRequest, CompletableFuture<HttpResponse<T>>> pushPromisesMap) 返回一个推送承诺处理程序,该处理程序将推送承诺及其响应累积到给定的map中。 -
CompletableFuture 在 java.util.concurrent 中的用法
修饰符和类型方法描述CompletableFuture.acceptEither(CompletionStage<? extends T> other, Consumer<? super T> action) CompletableFuture.acceptEitherAsync(CompletionStage<? extends T> other, Consumer<? super T> action) CompletableFuture.acceptEitherAsync(CompletionStage<? extends T> other, Consumer<? super T> action, Executor executor) static CompletableFuture<Void>CompletableFuture.allOf(CompletableFuture<?>... cfs) 返回一个新的 CompletableFuture,它在所有给定的 CompletableFutures 完成时完成。static CompletableFuture<Object>CompletableFuture.anyOf(CompletableFuture<?>... cfs) 返回一个新的 CompletableFuture,它在任何给定的 CompletableFutures 完成时完成,结果相同。<U> CompletableFuture<U>CompletableFuture.applyToEither(CompletionStage<? extends T> other, Function<? super T, U> fn) <U> CompletableFuture<U>CompletableFuture.applyToEitherAsync(CompletionStage<? extends T> other, Function<? super T, U> fn) <U> CompletableFuture<U>CompletableFuture.applyToEitherAsync(CompletionStage<? extends T> other, Function<? super T, U> fn, Executor executor) CompletableFuture.completeAsync(Supplier<? extends T> supplier) 使用默认执行程序从异步任务调用的给定 Supplier 函数的结果完成此 CompletableFuture。CompletableFuture.completeAsync(Supplier<? extends T> supplier, Executor executor) 使用给定的执行程序从异步任务调用的给定供应商函数的结果完成此 CompletableFuture。static <U> CompletableFuture<U>CompletableFuture.completedFuture(U value) 返回一个已经用给定值完成的新的 CompletableFuture。CompletableFuture.completeOnTimeout(T value, long timeout, TimeUnit unit) 如果在给定超时之前未以其他方式完成,则使用给定值完成此 CompletableFuture。使用给定的 Consumer 函数处理所有已发布的项目。CompletableFuture.copy()返回一个正常完成的新 CompletableFuture,其正常完成时具有与此 CompletableFuture 相同的值。CompletableFuture.exceptionally(Function<Throwable, ? extends T> fn) static <U> CompletableFuture<U>CompletableFuture.failedFuture(Throwable ex) 返回一个新的 CompletableFuture,它已经异常完成并出现给定的异常。<U> CompletableFuture<U>CompletableFuture.handle(BiFunction<? super T, Throwable, ? extends U> fn) <U> CompletableFuture<U>CompletableFuture.handleAsync(BiFunction<? super T, Throwable, ? extends U> fn) <U> CompletableFuture<U>CompletableFuture.handleAsync(BiFunction<? super T, Throwable, ? extends U> fn, Executor executor) <U> CompletableFuture<U>CompletableFuture.newIncompleteFuture()返回一个新的不完整的 CompletableFuture,其类型由 CompletionStage 方法返回。如果在给定超时之前未以其他方式完成,则使用TimeoutException异常完成此 CompletableFuture。CompletableFuture.runAfterBoth(CompletionStage<?> other, Runnable action) CompletableFuture.runAfterBothAsync(CompletionStage<?> other, Runnable action) CompletableFuture.runAfterBothAsync(CompletionStage<?> other, Runnable action, Executor executor) CompletableFuture.runAfterEither(CompletionStage<?> other, Runnable action) CompletableFuture.runAfterEitherAsync(CompletionStage<?> other, Runnable action) CompletableFuture.runAfterEitherAsync(CompletionStage<?> other, Runnable action, Executor executor) static CompletableFuture<Void>返回一个新的 CompletableFuture,它在运行给定操作后由ForkJoinPool.commonPool()中运行的任务异步完成。static CompletableFuture<Void>返回一个新的 CompletableFuture,它在运行给定操作后由在给定执行器中运行的任务异步完成。static <U> CompletableFuture<U>CompletableFuture.supplyAsync(Supplier<U> supplier) 返回一个新的 CompletableFuture,它由ForkJoinPool.commonPool()中运行的任务异步完成,其值是通过调用给定的 Supplier 获得的。static <U> CompletableFuture<U>CompletableFuture.supplyAsync(Supplier<U> supplier, Executor executor) 返回一个新的 CompletableFuture,它由在给定执行器中运行的任务异步完成,其值是通过调用给定供应商获得的。CompletableFuture.thenAccept(Consumer<? super T> action) CompletableFuture.thenAcceptAsync(Consumer<? super T> action) CompletableFuture.thenAcceptAsync(Consumer<? super T> action, Executor executor) <U> CompletableFuture<Void>CompletableFuture.thenAcceptBoth(CompletionStage<? extends U> other, BiConsumer<? super T, ? super U> action) <U> CompletableFuture<Void>CompletableFuture.thenAcceptBothAsync(CompletionStage<? extends U> other, BiConsumer<? super T, ? super U> action) <U> CompletableFuture<Void>CompletableFuture.thenAcceptBothAsync(CompletionStage<? extends U> other, BiConsumer<? super T, ? super U> action, Executor executor) <U> CompletableFuture<U><U> CompletableFuture<U>CompletableFuture.thenApplyAsync(Function<? super T, ? extends U> fn) <U> CompletableFuture<U>CompletableFuture.thenApplyAsync(Function<? super T, ? extends U> fn, Executor executor) <U,V> CompletableFuture<V> CompletableFuture.thenCombine(CompletionStage<? extends U> other, BiFunction<? super T, ? super U, ? extends V> fn) <U,V> CompletableFuture<V> CompletableFuture.thenCombineAsync(CompletionStage<? extends U> other, BiFunction<? super T, ? super U, ? extends V> fn) <U,V> CompletableFuture<V> CompletableFuture.thenCombineAsync(CompletionStage<? extends U> other, BiFunction<? super T, ? super U, ? extends V> fn, Executor executor) <U> CompletableFuture<U>CompletableFuture.thenCompose(Function<? super T, ? extends CompletionStage<U>> fn) <U> CompletableFuture<U>CompletableFuture.thenComposeAsync(Function<? super T, ? extends CompletionStage<U>> fn) <U> CompletableFuture<U>CompletableFuture.thenComposeAsync(Function<? super T, ? extends CompletionStage<U>> fn, Executor executor) CompletableFuture.thenRunAsync(Runnable action) CompletableFuture.thenRunAsync(Runnable action, Executor executor) CompletableFuture.toCompletableFuture()返回此 CompletableFuture。CompletionStage.toCompletableFuture()返回一个CompletableFuture维护与此阶段相同的完成属性。CompletableFuture.whenComplete(BiConsumer<? super T, ? super Throwable> action) CompletableFuture.whenCompleteAsync(BiConsumer<? super T, ? super Throwable> action) CompletableFuture.whenCompleteAsync(BiConsumer<? super T, ? super Throwable> action, Executor executor) java.util.concurrent 中的方法,参数类型为 CompletableFuture修饰符和类型方法描述static CompletableFuture<Void>CompletableFuture.allOf(CompletableFuture<?>... cfs) 返回一个新的 CompletableFuture,它在所有给定的 CompletableFutures 完成时完成。static CompletableFuture<Object>CompletableFuture.anyOf(CompletableFuture<?>... cfs) 返回一个新的 CompletableFuture,它在任何给定的 CompletableFutures 完成时完成,结果相同。