{"id":10006978,"date":"2022-12-15T19:48:11","date_gmt":"2022-12-15T11:48:11","guid":{"rendered":"https:\/\/ityet.com\/?p=10006978"},"modified":"2022-12-15T19:48:11","modified_gmt":"2022-12-15T11:48:11","slug":"%e4%b8%8ecompletablefuture%e6%9c%89%e5%85%b3%e7%9a%84%e4%b8%80%e4%ba%9b%e7%9f%a5%e8%af%86","status":"publish","type":"post","link":"https:\/\/itmax.cn\/?p=10006978","title":{"rendered":"\u4e0eCompletableFuture\u6709\u5173\u7684\u4e00\u4e9b\u77e5\u8bc6"},"content":{"rendered":"<blockquote><p>Java1.5\u5f15\u5165\u4e86Future\uff0c\u57281.8\u4e2d\u53c8\u5f15\u5165\u4e86CompletableFuture\u3002\u4ed6\u7684\u51fa\u73b0\u53ef\u4ee5\u4f7f\u6211\u4eec\u66f4\u597d\u7684\u53bb\u5bf9\u4efb\u52a1\u8fdb\u884c\u7f16\u6392\uff0c\u5408\u7406\u7684\u4f7f\u7528\u4f1a\u6781\u5927\u7684\u7f29\u51cf\u591a\u4efb\u52a1\u7684\u5904\u7406\u65f6\u95f4\uff0c\u8fbe\u5230\u4e8b\u534a\u529f\u500d\u7684\u76ee\u7684\u3002\u4e0b\u9762\u8ba9\u6211\u4eec\u4e00\u5757\u6765\u770b\u4e00\u4e0b\u4e0e\u5b83\u76f8\u5173\u7684\u4e00\u4e9b\u77e5\u8bc6\u3002<\/p><\/blockquote>\n<h3>1 Java\u4e2d\u7684\u51fd\u6570\u5f0f\u7f16\u7a0b<\/h3>\n<p>\u5728\u770bCompletableFuture\u4e86\u89e3\u4e00\u70b9Java\u4e2d\u51fd\u6570\u5f0f\u7f16\u7a0b\u76f8\u5173\u7684\u77e5\u8bc6\u4f1a\u66f4\u6709\u7528\u4e00\u4e9b\u3002<\/p>\n<p>\u5728Java1.8\u4e2d\u5f15\u5165\u4e86<code>@FunctionalInterface<\/code>\uff0c\u540c\u65f6\u4e5f\u5728<code>java.util.function<\/code>\u5305\u4e2d\u5f15\u5165\u4e86\u5f88\u591a\u51fd\u6570\u5f0f\u63a5\u53e3\u3002\u6211\u4eec\u6765\u770b\u51e0\u4e2a\u5e38\u89c1\u7684:<\/p>\n<h4>1.1 Consumer<\/h4>\n<p>Consumer\u6d88\u8d39\u8005\uff0c\u5173\u8054\u7684\u65b9\u6cd5\u4e3a<code>void accept(T t)<\/code>\uff0c\u6709\u53c2\u65e0\u8fd4\u56de\u503c\u3002<\/p>\n<pre><code class=\"java\">@FunctionalInterface\npublic interface Consumer&lt;T&gt; {\n\n    void accept(T t);<\/code><\/pre>\n<h4>1.2 Function<\/h4>\n<p>Function\u51fd\u6570\uff0c\u5173\u8054\u65b9\u6cd5\u4e3a<code>R apply(T t)<\/code>\uff0c\u6709\u53c2\u6709\u8fd4\u56de\u503c\u3002<\/p>\n<pre><code class=\"java\">@FunctionalInterface\npublic interface Function&lt;T, R&gt; {\n\n    R apply(T t);<\/code><\/pre>\n<h4>1.3 Predicate<\/h4>\n<p>Predicate\u8c13\u8bed(\u65ad\u8a00?)\uff0c\u5173\u8054\u65b9\u6cd5\u4e3a<code>boolean test(T t)<\/code>\uff0c\u6709\u53c2\u6709\u8fd4\u56de\u503c(bool)\u3002<\/p>\n<pre><code class=\"java\">@FunctionalInterface\npublic interface Predicate&lt;T&gt; {\n\n    boolean test(T t);<\/code><\/pre>\n<h4>1.4 Supplier<\/h4>\n<p>Supplier\u4f9b\u5e94\u8005\uff0c\u5173\u8054\u65b9\u6cd5\u4e3a<code>T get()<\/code>\uff0c\u65e0\u53c2\u6709\u8fd4\u56de\u503c\u3002<\/p>\n<pre><code class=\"java\">@FunctionalInterface\npublic interface Supplier&lt;T&gt; {\n\n    T get();\n}<\/code><\/pre>\n<h3>2 CompletableFuture<\/h3>\n<p>\u540e\u7eed\u7684\u5185\u5bb9\u4e3b\u8981\u5173\u6ce8\u70b9\u4f1a\u653e\u5728\u5176\u5185\u90e8\u5404\u79cdAPI\u4e0a\u9762\u3002\u5728\u540e\u7eed\u7684\u65b9\u6cd5\u4e2d\uff0c\u56e0\u4e3a<code>CompletableFuture<\/code>\u5b9e\u73b0\u4e86<code>CompletionStage<\/code>\u4f7f\u5176\u5177\u6709\u94fe\u5f0f\u8c03\u7528\u80fd\u529b\u3002\u5927\u591a\u6570\u65b9\u6cd5\u90fd\u4f1a\u7c7b\u4f3c\u5982\u4e0b:<\/p>\n<ul>\n<li>xxx(Function func): \u4f1a\u4e0a\u6587\u7684<code>\u540c\u4e00\u7ebf\u7a0b<\/code>\u53bb\u6267\u884c\u5f53\u524d\u4efb\u52a1<\/li>\n<li>xxxAsync(Function func): \u4f1a\u542f\u7528\u4e00\u4e2a<code>\u65b0\u7ebf\u7a0b<\/code>\u53bb\u6267\u884c\u5f53\u524d\u4efb\u52a1<\/li>\n<li>xxxAsync(Function func, Executor executor): \u4f1a\u4f7f\u7528<code>executor<\/code>\u7ebf\u7a0b\u6c60\u4e2d\u7684<code>\u65b0\u7ebf\u7a0b<\/code>\u53bb\u6267\u884c\u5f53\u524d\u4efb\u52a1<\/li>\n<\/ul>\n<h4>2.1 CompletableFuture\u7684\u521b\u5efa<\/h4>\n<ul>\n<li><code>supplyAsync<\/code>: \u4ee5<code>Supplier<\/code>\u4e3a\u53c2\uff0c\u6709\u8fd4\u56de\u503c\u3002<\/li>\n<\/ul>\n<pre><code class=\"java\">public static &lt;U&gt; CompletableFuture&lt;U&gt; supplyAsync(Supplier&lt;U&gt; supplier)\npublic static &lt;U&gt; CompletableFuture&lt;U&gt; supplyAsync(Supplier&lt;U&gt; supplier, Executor executor)<\/code><\/pre>\n<ul>\n<li><code>runAsync<\/code>: \u4ee5<code>Runnable<\/code>\u4e3a\u53c2\uff0c\u8fd4\u56de\u503c\u4e3a<code>Void<\/code>\u3002<\/li>\n<\/ul>\n<pre><code class=\"java\">public static CompletableFuture&lt;Void&gt; runAsync(Runnable runnable)\npublic static CompletableFuture&lt;Void&gt; runAsync(Runnable runnable, Executor executor)<\/code><\/pre>\n<ul>\n<li><code>completedFuture<\/code>: \u521b\u5efa\u4e00\u4e2a\u5df2\u6709\u503c\u4e14\u5b8c\u6210\u7684CompletableFuture\u3002<\/li>\n<\/ul>\n<pre><code class=\"java\">public static &lt;U&gt; CompletableFuture&lt;U&gt; completedFuture(U value)<\/code><\/pre>\n<ul>\n<li><code>failedFuture<\/code>: \u521b\u5efa\u4e00\u4e2a\u5df2\u51fa\u73b0\u5f02\u5e38\u4e14\u5b8c\u6210\u7684CompletableFuture\u3002<\/li>\n<\/ul>\n<pre><code class=\"java\">public static &lt;U&gt; CompletableFuture&lt;U&gt; failedFuture(Throwable ex)<\/code><\/pre>\n<p>\u53ef\u4ee5\u770b\u5230<code>runAsync<\/code>\u548c<code>supplyAsync<\/code>\u90fd\u63d0\u4f9b\u4e86\u989d\u5916\u53c2\u6570<code>Executor<\/code>\uff0c\u5728\u672a\u6307\u5b9a<code>executor<\/code>\u65f6\uff0c\u9ed8\u8ba4\u4f1a\u4f7f\u7528<code>ForkJoinPool.commonPool()<\/code>\u6765\u6267\u884c\u5f02\u6b65\u4efb\u52a1\uff0c\u800c<code>Parallel Stream<\/code>\u9ed8\u8ba4\u60c5\u51b5\u4e0b\u4e5f\u4f1a\u4f7f\u7528\u8be5\u7ebf\u7a0b\u6c60\uff0c\u5171\u7528\u7684\u8bdd\u53ef\u80fd\u4f1a\u5bfc\u81f4\u975e\u6838\u5fc3\u4e1a\u52a1\u62a2\u5360\u6838\u5fc3\u4e1a\u52a1\u7684\u6267\u884c\u3002\u4e00\u822c\u6765\u8bf4\u90fd\u4f1a\u4f7f\u7528\u81ea\u5b9a\u4e49\u7ebf\u7a0b\u6c60\u6765\u6267\u884c\u8fd9\u4e9b\u4efb\u52a1\u3002<\/p>\n<h4>2.2 CompletableFuture\u7684\u540e\u7eed\u64cd\u4f5c<\/h4>\n<h5>2.2.1 \u6267\u884c\u4e00\u4e2a\u4efb\u52a1(1)<\/h5>\n<ul>\n<li><code>apply<\/code>: \u6709\u53c2\u6709\u8fd4\u56de\u503c\u3002\u4f1a\u5c06\u524d\u9762\u7684\u8fd4\u56de\u503c\u4f5c\u4e3a\u5f53\u524d\u51fd\u6570\u7684\u8f93\u5165\u3002<\/li>\n<\/ul>\n<pre><code class=\"java\">public &lt;U&gt; CompletableFuture&lt;U&gt; thenApply(Function&lt;? super T,? extends U&gt; fn)\npublic &lt;U&gt; CompletableFuture&lt;U&gt; thenApplyAsync(Function&lt;? super T,? extends U&gt; fn)\npublic &lt;U&gt; CompletableFuture&lt;U&gt; thenApplyAsync(Function&lt;? super T,? extends U&gt; fn, Executor executor)<\/code><\/pre>\n<ul>\n<li><code>accept<\/code>: \u6709\u53c2\u65e0\u8fd4\u56de\u503c\u3002\u4f1a\u5c06\u524d\u9762\u7684\u8f93\u51fa\u4f5c\u4e3a\u5f53\u524d\u51fd\u6570\u7684\u8f93\u5165\u3002<\/li>\n<\/ul>\n<pre><code class=\"java\">public CompletableFuture&lt;Void&gt; thenAccept(Consumer&lt;? super T&gt; action)\npublic CompletableFuture&lt;Void&gt; thenAcceptAsync(Consumer&lt;? super T&gt; action)\npublic CompletableFuture&lt;Void&gt; thenAcceptAsync(Consumer&lt;? super T&gt; action, Executor executor)<\/code><\/pre>\n<ul>\n<li><code>run<\/code>: \u7f16\u6392\u6267\u884c\u53e6\u5916\u4e00\u4e2a\u6beb\u65e0\u76f8\u5173\u7684\u4efb\u52a1<\/li>\n<\/ul>\n<pre><code class=\"java\">public CompletableFuture&lt;Void&gt; thenRun(Runnable action)\npublic CompletableFuture&lt;Void&gt; thenRunAsync(Runnable action)\npublic CompletableFuture&lt;Void&gt; thenRunAsync(Runnable action, Executor executor)<\/code><\/pre>\n<p>\u793a\u4f8b<\/p>\n<pre><code class=\"java\">@Test\nvoid testApply() throws ExecutionException, InterruptedException {\n    final CompletableFuture&lt;Integer&gt; future = CompletableFuture\n            .supplyAsync(() -&gt; 1)\n            .thenApply((val) -&gt; val + 1);\n    assertEquals(2, future.get());\n}\n\n@Test\nvoid testAccept() {\n    final CompletableFuture&lt;Void&gt; future = CompletableFuture\n            .supplyAsync(() -&gt; 1)\n            .thenAccept((val) -&gt; {\n                assertEquals(1, val);\n            });\n    future.join();\n}\n\n@Test\nvoid testRun() {\n    final CompletableFuture&lt;Void&gt; future = CompletableFuture\n            .supplyAsync(() -&gt; 1)\n            .thenRun(() -&gt; {});\n    future.join();\n}<\/code><\/pre>\n<h5>2.2.1 \u6267\u884c\u4e00\u4e2a\u4efb\u52a1(2)<\/h5>\n<ul>\n<li><code>whenComplete<\/code>: \u5f53\u4efb\u52a1\u5b8c\u6210\u65f6\u6267\u884c\u8be5\u65b9\u6cd5\uff0c\u5c06\u8fd4\u56de\u503c\u53ca\u5f02\u5e38\u4f20\u5165<code>BiConsumer<\/code>(\u6709\u53c2\u65e0\u8fd4\u56de\u503c)<\/li>\n<\/ul>\n<pre><code class=\"java\">public CompletableFuture&lt;T&gt; whenComplete(BiConsumer&lt;? super T, ? super Throwable&gt; action)\npublic CompletableFuture&lt;T&gt; whenCompleteAsync(BiConsumer&lt;? super T, ? super Throwable&gt; action)\npublic CompletableFuture&lt;T&gt; whenCompleteAsync(BiConsumer&lt;? super T, ? super Throwable&gt; action, Executor executor)<\/code><\/pre>\n<ul>\n<li><code>exceptionally<\/code>: \u5f53\u76f8\u5e94\u7684\u4efb\u52a1\u51fa\u73b0\u5f02\u5e38\u65f6\u4f1a\u8c03\u7528\u8be5\u65b9\u6cd5\uff0c\u5c06\u5f02\u5e38\u4f5c\u4e3a\u53c2\u6570\u4f20\u8fdb\u6765<\/li>\n<\/ul>\n<pre><code class=\"java\">public CompletableFuture&lt;T&gt; exceptionally(Function&lt;Throwable, ? extends T&gt; fn)\npublic CompletableFuture&lt;T&gt; exceptionallyAsync(Function&lt;Throwable, ? extends T&gt; fn)\npublic CompletableFuture&lt;T&gt; exceptionallyAsync(Function&lt;Throwable, ? extends T&gt; fn, Executor executor)<\/code><\/pre>\n<ul>\n<li><code>handler<\/code>: \u63a5\u6536\u5904\u7406\u524d\u9762\u4efb\u52a1\u7684\u7ed3\u679c<code>BiFunction<\/code>(\u6709\u53c2\u6709\u8fd4\u56de\u503c)<\/li>\n<\/ul>\n<pre><code class=\"java\">public &lt;U&gt; CompletableFuture&lt;U&gt; handle(BiFunction&lt;? super T, Throwable, ? extends U&gt; fn)\npublic &lt;U&gt; CompletableFuture&lt;U&gt; handleAsync(BiFunction&lt;? super T, Throwable, ? extends U&gt; fn)\npublic &lt;U&gt; CompletableFuture&lt;U&gt; handleAsync(BiFunction&lt;? super T, Throwable, ? extends U&gt; fn, Executor executor)<\/code><\/pre>\n<h5>2.2.2 \u6267\u884c\u4e24\u4e2a\u4efb\u52a1\u5176\u4e2d\u4e00\u4e2a\u5373\u53ef<\/h5>\n<ul>\n<li><code>applyToEither<\/code>: \u6267\u884c\u4fe9\u4efb\u52a1\uff0c\u8fd4\u56de\u5176\u4e2d\u4e00\u4e2a\u672a\u51fa\u73b0\u5f02\u5e38\u7684\uff0c\u5e76\u5c06\u8be5\u8f93\u51fa\u4f5c\u4e3a\u53c2\u6570\u4f20\u5165\u540e\u7eed<code>Function<\/code>(\u6709\u53c2\u6709\u8fd4\u56de\u503c)<\/li>\n<\/ul>\n<pre><code class=\"java\">public &lt;U&gt; CompletableFuture&lt;U&gt; applyToEither(CompletionStage&lt;? extends T&gt; other, Function&lt;? super T, U&gt; fn)\npublic &lt;U&gt; CompletableFuture&lt;U&gt; applyToEitherAsync(CompletionStage&lt;? extends T&gt; other, Function&lt;? super T, U&gt; fn)\npublic &lt;U&gt; CompletableFuture&lt;U&gt; applyToEitherAsync(CompletionStage&lt;? extends T&gt; other, Function&lt;? super T, U&gt; fn, Executor executor)<\/code><\/pre>\n<ul>\n<li><code>acceptEither<\/code>: \u6267\u884c\u4fe9\u4efb\u52a1\uff0c\u8fd4\u56de\u5176\u4e2d\u4e00\u4e2a\u672a\u51fa\u73b0\u5f02\u5e38\u7684\uff0c\u5e76\u5c06\u8be5\u8f93\u51fa\u4f5c\u4e3a\u53c2\u6570\u4f20\u5165\u540e\u7eed<code>Consumer<\/code>(\u6709\u53c2\u65e0\u8fd4\u56de\u503c)<\/li>\n<\/ul>\n<pre><code class=\"java\">public CompletableFuture&lt;Void&gt; acceptEither(CompletionStage&lt;? extends T&gt; other, Consumer&lt;? super T&gt; action) \npublic CompletableFuture&lt;Void&gt; acceptEitherAsync(CompletionStage&lt;? extends T&gt; other, Consumer&lt;? super T&gt; action)\npublic CompletableFuture&lt;Void&gt; acceptEitherAsync(CompletionStage&lt;? extends T&gt; other, Consumer&lt;? super T&gt; action, Executor executor)<\/code><\/pre>\n<ul>\n<li><code>runAfterEither<\/code>: \u6267\u884c\u4fe9\u4efb\u52a1\uff0c\u8fd4\u56de\u5176\u4e2d\u4e00\u4e2a\u672a\u51fa\u73b0\u5f02\u5e38\u7684\uff0c\u5e76\u5c06\u8be5\u8f93\u51fa\u4f5c\u4e3a\u53c2\u6570\u4f20\u5165\u540e\u7eed<code>Runnable<\/code>(\u65e0\u53c2\u65e0\u8fd4\u56de\u503c)<\/li>\n<\/ul>\n<pre><code class=\"java\">public CompletableFuture&lt;Void&gt; runAfterEither(CompletionStage&lt;?&gt; other, Runnable action)\npublic CompletableFuture&lt;Void&gt; runAfterEitherAsync(CompletionStage&lt;?&gt; other, Runnable action)\npublic CompletableFuture&lt;Void&gt; runAfterEitherAsync(CompletionStage&lt;?&gt; other, Runnable action, Executor executor)<\/code><\/pre>\n<h5>2.2.3 \u6267\u884c\u4e24\u4e2a\u4efb\u52a1\u5168\u90e8\u5b8c\u6210<\/h5>\n<ul>\n<li><code>thenCombine<\/code>: \u6267\u884c\u4e24\u4e2a\u4efb\u52a1\uff0c\u5e76\u5c06\u8fd9\u4fe9\u4efb\u52a1\u4f5c\u4e3a\u53c2\u6570\u8f93\u5165<code>BiFunction<\/code>(\u6709\u53c2\u6709\u8fd4\u56de\u503c)<\/li>\n<\/ul>\n<pre><code class=\"java\">public &lt;U,V&gt; CompletableFuture&lt;V&gt; thenCombine(CompletionStage&lt;? extends U&gt; other, BiFunction&lt;? super T,? super U,? extends V&gt; fn)\npublic &lt;U,V&gt; CompletableFuture&lt;V&gt; thenCombineAsync(CompletionStage&lt;? extends U&gt; other, BiFunction&lt;? super T,? super U,? extends V&gt; fn)\npublic &lt;U,V&gt; CompletableFuture&lt;V&gt; thenCombineAsync(CompletionStage&lt;? extends U&gt; other, BiFunction&lt;? super T,? super U,? extends V&gt; fn, Executor executor)<\/code><\/pre>\n<ul>\n<li><code>thenAcceptBoth<\/code>: \u6267\u884c\u4e24\u4e2a\u4efb\u52a1\uff0c\u5e76\u5c06\u8fd9\u4fe9\u4efb\u52a1\u4f5c\u4e3a\u53c2\u6570\u8f93\u5165<code>BiConsumer<\/code>(\u6709\u53c2\u65e0\u8fd4\u56de\u503c)<\/li>\n<\/ul>\n<pre><code class=\"java\">public &lt;U&gt; CompletableFuture&lt;Void&gt; thenAcceptBoth(CompletionStage&lt;? extends U&gt; other, BiConsumer&lt;? super T, ? super U&gt; action)\npublic &lt;U&gt; CompletableFuture&lt;Void&gt; thenAcceptBothAsync(CompletionStage&lt;? extends U&gt; other, BiConsumer&lt;? super T, ? super U&gt; action)\npublic &lt;U&gt; CompletableFuture&lt;Void&gt; thenAcceptBothAsync(CompletionStage&lt;? extends U&gt; other, BiConsumer&lt;? super T, ? super U&gt; action, Executor executor)<\/code><\/pre>\n<ul>\n<li><code>runAfterBoth<\/code>: \u6267\u884c\u4e24\u4e2a\u4efb\u52a1\uff0c\u7136\u540e\u6267\u884c<code>Runnable<\/code>(\u65e0\u53c2\u65e0\u8fd4\u56de\u503c)<\/li>\n<\/ul>\n<pre><code class=\"java\">public CompletableFuture&lt;Void&gt; runAfterBoth(CompletionStage&lt;?&gt; other, Runnable action)\npublic CompletableFuture&lt;Void&gt; runAfterBothAsync(CompletionStage&lt;?&gt; other, Runnable action)\npublic CompletableFuture&lt;Void&gt; runAfterBothAsync(CompletionStage&lt;?&gt; other, Runnable action, Executor executor)<\/code><\/pre>\n<h5>2.2.4 \u6267\u884c\u4e24\u4e2a\u6709\u4f9d\u8d56\u7684\u4efb\u52a1<\/h5>\n<ul>\n<li><code>thenCompose<\/code>: \u524d\u4e00\u4e2a\u4efb\u52a1\u7684\u7ed3\u679c\uff0c\u8981\u7ed9\u7b2c\u4e8c\u4e2a\u4efb\u52a1\u4f5c\u4e3a\u53c2\u6570<\/li>\n<\/ul>\n<pre><code class=\"java\">public &lt;U&gt; CompletableFuture&lt;U&gt; thenCompose(Function&lt;? super T, ? extends CompletionStage&lt;U&gt;&gt; fn)\npublic &lt;U&gt; CompletableFuture&lt;U&gt; thenComposeAsync(Function&lt;? super T, ? extends CompletionStage&lt;U&gt;&gt; fn)\npublic &lt;U&gt; CompletableFuture&lt;U&gt; thenComposeAsync(Function&lt;? super T, ? extends CompletionStage&lt;U&gt;&gt; fn, Executor executor)<\/code><\/pre>\n<h5>2.2.5 \u7ec4\u5408\u66f4\u591a\u7684\u4efb\u52a1<\/h5>\n<ul>\n<li><code>allOf<\/code>: \u7b49\u5f85\u6240\u6709\u4efb\u52a1\u90fd\u5b8c\u6210<\/li>\n<\/ul>\n<pre><code class=\"java\">public static CompletableFuture&lt;Void&gt; allOf(CompletableFuture&lt;?&gt;... cfs)<\/code><\/pre>\n<ul>\n<li><code>anyOf<\/code>: \u7b49\u5f85\u5b8c\u6210\u4efb\u610f\u4e00\u4efb\u52a1\u5373\u53ef<\/li>\n<\/ul>\n<pre><code class=\"java\">public static CompletableFuture&lt;Object&gt; anyOf(CompletableFuture&lt;?&gt;... cfs)<\/code><\/pre>\n<h4>2.3 \u7ed3\u679c\u83b7\u53d6<\/h4>\n<h5>2.3.1 join<\/h5>\n<p>\u4efb\u52a1\u5b8c\u6210\u65f6\u83b7\u53d6\u6570\u636e\uff0c\u6216\u8005\u5f02\u5e38\u65f6\u629b\u51fa\u5f02\u5e38\u3002\u4e0d\u8fc7\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5b83\u629b\u51fa\u7684\u5f02\u5e38\u662f<code>(unchecked) exception<\/code>\uff0c<code>CancellationException<\/code>\u6216<code>CompletionException<\/code><\/p>\n<pre><code class=\"java\">public T join()<\/code><\/pre>\n<h5>2.3.2 get<\/h5>\n<p>\u5728\u83b7\u53d6\u6570\u636e\u65f6: <code>get()<\/code>\u7b49\u5f85\u4efb\u52a1\u5b8c\u6210\u7136\u540e\u8bfb\u53d6\u7ed3\u679c; <code>get(long timeout, TimeUnit unit)<\/code>\u6709\u9650\u65f6\u95f4\u5185\u7b49\u5f85\u5b8c\u6210\u8bfb\u53d6\u7ed3\u679c\uff0c\u6216\u629b\u51fa<code>TimeoutException<\/code>\u5f02\u5e38; <code>getNow(T valueIfAbsent)<\/code>\u7acb\u5373\u83b7\u53d6\u7ed3\u679c\uff0c\u6216\u8005\u7f3a\u7701\u503c;<\/p>\n<pre><code class=\"java\">public T get() throws InterruptedException, ExecutionException\npublic T get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException \npublic T getNow(T valueIfAbsent)<\/code><\/pre>\n<h3>3 \u540e\u8bb0<\/h3>\n<p>\u4eca\u5929\u4e3b\u8981\u719f\u6089\u4e86<code>CompletableFuture<\/code>\u7684\u7edd\u5927\u90e8\u5206API\uff0c\u53ea\u6709\u5728\u5408\u9002\u7684\u573a\u666f\u53bb\u4f7f\u7528\u624d\u80fd\u878d\u4f1a\u8d2f\u901a\u3002\u540e\u9762\u4f1a\u7ed3\u5408\u5b9e\u9645\u573a\u666f\u6765\u8bb2\u8bb2\u5982\u4f55\u4f7f\u7528\u5b83\u6765\u505a\u4efb\u52a1\u7f16\u6392\u3002<\/p>\n<hr>\n<pre><code class=\"sh\">echo '5Y6f5Yib5paH56ugOiDmjpjph5Eo5L2g5oCO5LmI5Zad5aW26Iy25ZWKWzkyMzI0NTQ5NzU1NTA4MF0pL+aAneWQpihscGUyMzQp' | base64 -d<\/code><\/pre>\n<p><a href=\"https:\/\/segmentfault.com\/a\/1190000043095903\">https:\/\/segmentfault.com\/a\/1190000043095903<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Java1.5\u5f15\u5165\u4e86Future\uff0c\u57281.8\u4e2d\u53c8\u5f15\u5165\u4e86CompletableFuture\u3002\u4ed6\u7684\u51fa\u73b0\u53ef\u4ee5\u4f7f\u6211\u4eec\u66f4\u597d [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[4],"tags":[],"class_list":["post-10006978","post","type-post","status-publish","format-standard","hentry","category-share"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10006978","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10006978"}],"version-history":[{"count":0,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10006978\/revisions"}],"wp:attachment":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10006978"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10006978"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10006978"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}