{"id":1791,"date":"2023-03-27T22:32:34","date_gmt":"2023-03-27T14:32:34","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=1791"},"modified":"2023-04-23T21:14:10","modified_gmt":"2023-04-23T13:14:10","slug":"completefuture-usage-details","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/03\/27\/completefuture-usage-details\/","title":{"rendered":"CompletableFuture \u4f7f\u7528\u8be6\u89e3"},"content":{"rendered":"<h3>runAsync \u548c supplyAsync\u65b9\u6cd5<\/h3>\n<p>CompletableFuture \u63d0\u4f9b\u4e86\u56db\u4e2a\u9759\u6001\u65b9\u6cd5\u6765\u521b\u5efa\u4e00\u4e2a\u5f02\u6b65\u64cd\u4f5c\u3002<\/p>\n<pre><code class=\"language-java\">public static CompletableFuture&lt;Void&gt; runAsync(Runnable runnable)\npublic static CompletableFuture&lt;Void&gt; runAsync(Runnable runnable, Executor executor)\npublic 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<p><!-- more --><\/p>\n<p>\u6ca1\u6709\u6307\u5b9aExecutor\u7684\u65b9\u6cd5\u4f1a\u4f7f\u7528<code>ForkJoinPool.commonPool()<\/code>\u4f5c\u4e3a\u5b83\u7684\u7ebf\u7a0b\u6c60\u6267\u884c\u5f02\u6b65\u4ee3\u7801\u3002\u5982\u679c\u6307\u5b9a\u7ebf\u7a0b\u6c60\uff0c\u5219\u4f7f\u7528\u6307\u5b9a\u7684\u7ebf\u7a0b\u6c60\u8fd0\u884c\u3002\u4ee5\u4e0b\u6240\u6709\u7684\u65b9\u6cd5\u90fd\u7c7b\u540c\u3002<\/p>\n<ul>\n<li><code>runAsync<\/code>\u65b9\u6cd5\u4e0d\u652f\u6301\u8fd4\u56de\u503c<\/li>\n<li><code>supplyAsync<\/code>\u53ef\u4ee5\u652f\u6301\u8fd4\u56de\u503c<\/li>\n<\/ul>\n<pre><code class=\"language-java\">\/\/\u65e0\u8fd4\u56de\u503c\npublic static void runAsync() throws Exception {\n    CompletableFuture&lt;Void&gt; future = CompletableFuture.runAsync(() -&gt; {\n        try {\n            TimeUnit.SECONDS.sleep(1);\n        } catch (InterruptedException e) {\n        }\n        System.out.println(&quot;run end ...&quot;);\n    });\n\n    future.get();\n}\n\n\/\/\u6709\u8fd4\u56de\u503c\npublic static void supplyAsync() throws Exception {         \n    CompletableFuture&lt;Long&gt; future = CompletableFuture.supplyAsync(() -&gt; {\n        try {\n            TimeUnit.SECONDS.sleep(1);\n        } catch (InterruptedException e) {\n        }\n        System.out.println(&quot;run end ...&quot;);\n        return System.currentTimeMillis();\n    });\n\n    long time = future.get();\n    System.out.println(&quot;time = &quot;+time);\n}<\/code><\/pre>\n<h3>\u8ba1\u7b97\u7ed3\u679c\u5b8c\u6210\u65f6\u7684\u56de\u8c03\u65b9\u6cd5<\/h3>\n<p>\u5f53CompletableFuture\u7684\u8ba1\u7b97\u7ed3\u679c\u5b8c\u6210\uff0c\u6216\u8005\u629b\u51fa\u5f02\u5e38\u7684\u65f6\u5019\uff0c\u53ef\u4ee5\u6267\u884c\u7279\u5b9a\u7684Action\u3002\u4e3b\u8981\u662f\u4e0b\u9762\u7684\u65b9\u6cd5\uff1a<\/p>\n<pre><code class=\"language-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)\npublic CompletableFuture&lt;T&gt; exceptionally(Function&lt;Throwable, ? extends T&gt; fn)<\/code><\/pre>\n<p>\u53ef\u4ee5\u770b\u5230Action\u7684\u7c7b\u578b\u662f<code>BiConsumer&lt;? super T, ? super Throwable&gt;<\/code>\u5b83\u53ef\u4ee5\u5904\u7406\u6b63\u5e38\u7684\u8ba1\u7b97\u7ed3\u679c\uff0c\u6216\u8005\u5f02\u5e38\u60c5\u51b5\u3002<\/p>\n<p><code>whenComplete<\/code>\u548c<code>whenCompleteAsync<\/code>\u7684\u533a\u522b\uff1a<\/p>\n<ul>\n<li><code>whenComplete<\/code>\uff1a\u662f\u6267\u884c\u5f53\u524d\u4efb\u52a1\u7684\u7ebf\u7a0b\u6267\u884c\u7ee7\u7eed\u6267\u884c<code>whenComplete<\/code>\u7684\u4efb\u52a1<\/li>\n<li><code>whenCompleteAsync<\/code>\uff1a\u662f\u6267\u884c\u628a<code>whenCompleteAsync<\/code>\u8fd9\u4e2a\u4efb\u52a1\u7ee7\u7eed\u63d0\u4ea4\u7ed9\u7ebf\u7a0b\u6c60\u6765\u8fdb\u884c\u6267\u884c<\/li>\n<\/ul>\n<pre><code class=\"language-java\">public static void whenComplete() throws Exception {\n    CompletableFuture&lt;Void&gt; future = CompletableFuture.runAsync(() -&gt; {\n        try {\n            TimeUnit.SECONDS.sleep(1);\n        } catch (InterruptedException e) {\n        }\n        if(new Random().nextInt()%2 &gt;= 0) {\n            int i = 12\/0;\n        }\n        System.out.println(&quot;run end ...&quot;);\n    });\n\n    future.whenComplete(new BiConsumer&lt;Void, Throwable&gt;() {\n        @Override\n        public void accept(Void t, Throwable action) {\n            System.out.println(&quot;\u6267\u884c\u5b8c\u6210\uff01&quot;);\n        }\n\n    });\n    future.exceptionally(new Function&lt;Throwable, Void&gt;() {\n        @Override\n        public Void apply(Throwable t) {\n            System.out.println(&quot;\u6267\u884c\u5931\u8d25\uff01&quot; + t.getMessage());\n            return null;\n        }\n    });\n\n    TimeUnit.SECONDS.sleep(2);\n}<\/code><\/pre>\n<h3>thenApply \u65b9\u6cd5<\/h3>\n<p>\u5f53\u4e00\u4e2a\u7ebf\u7a0b\u4f9d\u8d56\u53e6\u4e00\u4e2a\u7ebf\u7a0b\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528<code>thenApply<\/code>\u65b9\u6cd5\u6765\u628a\u8fd9\u4e24\u4e2a\u7ebf\u7a0b\u4e32\u884c\u5316\u3002<\/p>\n<pre><code class=\"language-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<p><code>Function&lt;? super T,? extends U&gt;<\/code><\/p>\n<ul>\n<li>T\uff1a\u4e0a\u4e00\u4e2a\u4efb\u52a1\u8fd4\u56de\u7ed3\u679c\u7684\u7c7b\u578b<\/li>\n<li>U\uff1a\u5f53\u524d\u4efb\u52a1\u7684\u8fd4\u56de\u503c\u7c7b\u578b<\/li>\n<\/ul>\n<pre><code class=\"language-java\">private static void thenApply() throws Exception {\n    CompletableFuture&lt;Long&gt; future = CompletableFuture.supplyAsync(new Supplier&lt;Long&gt;() {\n        @Override\n        public Long get() {\n            long result = new Random().nextInt(100);\n            System.out.println(&quot;result1=&quot; + result);\n            return result;\n        }\n    }).thenApply(new Function&lt;Long, Long&gt;() {\n        @Override\n        public Long apply(Long t) {\n            long result = t*5;\n            System.out.println(&quot;result2=&quot; + result);\n            return result;\n        }\n    });\n\n    long result = future.get();\n    System.out.println(result);\n}<\/code><\/pre>\n<p>\u7b2c\u4e8c\u4e2a\u4efb\u52a1\u4f9d\u8d56\u7b2c\u4e00\u4e2a\u4efb\u52a1\u7684\u7ed3\u679c\u3002<\/p>\n<h3>handle \u65b9\u6cd5<\/h3>\n<ul>\n<li><code>handle<\/code>\u662f\u6267\u884c\u4efb\u52a1\u5b8c\u6210\u65f6\u5bf9\u7ed3\u679c\u7684\u5904\u7406<\/li>\n<li><code>handle<\/code>\u65b9\u6cd5\u548c<code>thenApply<\/code>\u65b9\u6cd5\u5904\u7406\u65b9\u5f0f\u57fa\u672c\u4e00\u6837\u3002\u4e0d\u540c\u7684\u662f<code>handle<\/code>\u662f\u5728\u4efb\u52a1\u5b8c\u6210\u540e\u518d\u6267\u884c\uff0c\u8fd8\u53ef\u4ee5\u5904\u7406\u5f02\u5e38\u7684\u4efb\u52a1\u3002<code>thenApply<\/code>\u53ea\u53ef\u4ee5\u6267\u884c\u6b63\u5e38\u7684\u4efb\u52a1\uff0c\u4efb\u52a1\u51fa\u73b0\u5f02\u5e38\u5219\u4e0d\u6267\u884c<code>thenApply<\/code>\u65b9\u6cd5<\/li>\n<\/ul>\n<pre><code class=\"language-java\">public &lt;U&gt; CompletionStage&lt;U&gt; handle(BiFunction&lt;? super T, Throwable, ? extends U&gt; fn);\npublic &lt;U&gt; CompletionStage&lt;U&gt; handleAsync(BiFunction&lt;? super T, Throwable, ? extends U&gt; fn);\npublic &lt;U&gt; CompletionStage&lt;U&gt; handleAsync(BiFunction&lt;? super T, Throwable, ? extends U&gt; fn,Executor executor);<\/code><\/pre>\n<pre><code class=\"language-java\">public static void handle() throws Exception{\n    CompletableFuture&lt;Integer&gt; future = CompletableFuture.supplyAsync(new Supplier&lt;Integer&gt;() {\n\n        @Override\n        public Integer get() {\n            int i= 10\/0;\n            return new Random().nextInt(10);\n        }\n    }).handle(new BiFunction&lt;Integer, Throwable, Integer&gt;() {\n        @Override\n        public Integer apply(Integer param, Throwable throwable) {\n            int result = -1;\n            if (throwable == null) {\n                result = param * 2;\n            } else {\n                System.out.println(throwable.getMessage());\n            }\n            return result;\n        }\n    });\n    System.out.println(future.get());\n}<\/code><\/pre>\n<p>\u4ece\u793a\u4f8b\u4e2d\u53ef\u4ee5\u770b\u51fa\uff0c\u5728<code>handle<\/code>\u4e2d\u53ef\u4ee5\u6839\u636e\u4efb\u52a1\u662f\u5426\u6709\u5f02\u5e38\u6765\u8fdb\u884c\u505a\u76f8\u5e94\u7684\u540e\u7eed\u5904\u7406\u64cd\u4f5c\u3002\u800c<code>thenApply<\/code>\u65b9\u6cd5\uff0c\u5982\u679c\u4e0a\u4e2a\u4efb\u52a1\u51fa\u73b0\u9519\u8bef\uff0c\u5219\u4e0d\u4f1a\u6267\u884c<code>thenApply<\/code>\u65b9\u6cd5\u3002<\/p>\n<h3>thenAccept \u6d88\u8d39\u5904\u7406\u7ed3\u679c<\/h3>\n<p>\u63a5\u6536\u4efb\u52a1\u7684\u5904\u7406\u7ed3\u679c\uff0c\u5e76\u6d88\u8d39\u5904\u7406\uff0c\u65e0\u8fd4\u56de\u7ed3\u679c\u3002<\/p>\n<pre><code class=\"language-java\">public CompletionStage&lt;Void&gt; thenAccept(Consumer&lt;? super T&gt; action);\npublic CompletionStage&lt;Void&gt; thenAcceptAsync(Consumer&lt;? super T&gt; action);\npublic CompletionStage&lt;Void&gt; thenAcceptAsync(Consumer&lt;? super T&gt; action,Executor executor);<\/code><\/pre>\n<pre><code class=\"language-java\">public static void thenAccept() throws Exception{\n    CompletableFuture&lt;Void&gt; future = CompletableFuture.supplyAsync(new Supplier&lt;Integer&gt;() {\n        @Override\n        public Integer get() {\n            return new Random().nextInt(10);\n        }\n    }).thenAccept(integer -&gt; {\n        System.out.println(integer);\n    });\n    future.get();\n}<\/code><\/pre>\n<p>\u4ece\u793a\u4f8b\u4ee3\u7801\u4e2d\u53ef\u4ee5\u770b\u51fa\uff0c\u8be5\u65b9\u6cd5\u53ea\u662f\u6d88\u8d39\u6267\u884c\u5b8c\u6210\u7684\u4efb\u52a1\uff0c\u5e76\u53ef\u4ee5\u6839\u636e\u4e0a\u9762\u7684\u4efb\u52a1\u8fd4\u56de\u7684\u7ed3\u679c\u8fdb\u884c\u5904\u7406\u3002\u5e76\u6ca1\u6709\u540e\u7eed\u7684\u8f93\u9519\u64cd\u4f5c\u3002<\/p>\n<h3>thenRun \u65b9\u6cd5<\/h3>\n<p>\u8ddf<code>thenAccept<\/code>\u65b9\u6cd5\u4e0d\u4e00\u6837\u7684\u662f\uff0c\u4e0d\u5173\u5fc3\u4efb\u52a1\u7684\u5904\u7406\u7ed3\u679c\u3002\u53ea\u8981\u4e0a\u9762\u7684\u4efb\u52a1\u6267\u884c\u5b8c\u6210\uff0c\u5c31\u5f00\u59cb\u6267\u884c<code>thenRun<\/code>\u3002<\/p>\n<pre><code class=\"language-java\">public CompletionStage&lt;Void&gt; thenRun(Runnable action);\npublic CompletionStage&lt;Void&gt; thenRunAsync(Runnable action);\npublic CompletionStage&lt;Void&gt; thenRunAsync(Runnable action,Executor executor);<\/code><\/pre>\n<pre><code class=\"language-java\">public static void thenRun() throws Exception{\n    CompletableFuture&lt;Void&gt; future = CompletableFuture.supplyAsync(new Supplier&lt;Integer&gt;() {\n        @Override\n        public Integer get() {\n            return new Random().nextInt(10);\n        }\n    }).thenRun(() -&gt; {\n        System.out.println(&quot;thenRun ...&quot;);\n    });\n    future.get();\n}<\/code><\/pre>\n<p>\u8be5\u65b9\u6cd5\u540c<code>thenAccept<\/code>\u65b9\u6cd5\u7c7b\u4f3c\u3002\u4e0d\u540c\u7684\u662f\u4e0a\u4e2a\u4efb\u52a1\u5904\u7406\u5b8c\u6210\u540e\uff0c\u5e76\u4e0d\u4f1a\u628a\u8ba1\u7b97\u7684\u7ed3\u679c\u4f20\u7ed9<code>thenRun<\/code>\u65b9\u6cd5\u3002\u53ea\u662f\u5904\u7406\u73a9\u4efb\u52a1\u540e\uff0c\u6267\u884c<code>thenAccept<\/code>\u7684\u540e\u7eed\u64cd\u4f5c\u3002<\/p>\n<h3>thenCombine \u5408\u5e76\u4efb\u52a1<\/h3>\n<p><code>thenCombine<\/code>\u4f1a\u628a\u4e24\u4e2a<code>CompletionStage<\/code>\u7684\u4efb\u52a1\u90fd\u6267\u884c\u5b8c\u6210\u540e\uff0c\u628a\u4e24\u4e2a\u4efb\u52a1\u7684\u7ed3\u679c\u4e00\u5757\u4ea4\u7ed9<code>thenCombine<\/code>\u6765\u5904\u7406\u3002<\/p>\n<pre><code class=\"language-java\">public &lt;U,V&gt; CompletionStage&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; CompletionStage&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; CompletionStage&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<pre><code class=\"language-java\">private static void thenCombine() throws Exception {\n    CompletableFuture&lt;String&gt; future1 = CompletableFuture.supplyAsync(new Supplier&lt;String&gt;() {\n        @Override\n        public String get() {\n            return &quot;hello&quot;;\n        }\n    });\n    CompletableFuture&lt;String&gt; future2 = CompletableFuture.supplyAsync(new Supplier&lt;String&gt;() {\n        @Override\n        public String get() {\n            return &quot;hello&quot;;\n        }\n    });\n    CompletableFuture&lt;String&gt; result = future1.thenCombine(future2, new BiFunction&lt;String, String, String&gt;() {\n        @Override\n        public String apply(String t, String u) {\n            return t + &quot; &quot; + u;\n        }\n    });\n    System.out.println(result.get());\n}<\/code><\/pre>\n<h3>thenAcceptBoth<\/h3>\n<p>\u5f53\u4e24\u4e2aCompletionStage\u90fd\u6267\u884c\u5b8c\u6210\u540e\uff0c\u628a\u7ed3\u679c\u4e00\u5757\u4ea4\u7ed9<code>thenAcceptBoth<\/code>\u6765\u8fdb\u884c\u6d88\u8017<\/p>\n<pre><code class=\"language-java\">public &lt;U&gt; CompletionStage&lt;Void&gt; thenAcceptBoth(CompletionStage&lt;? extends U&gt; other, BiConsumer&lt;? super T, ? super U&gt; action);\npublic &lt;U&gt; CompletionStage&lt;Void&gt; thenAcceptBothAsync(CompletionStage&lt;? extends U&gt; other, BiConsumer&lt;? super T, ? super U&gt; action);\npublic &lt;U&gt; CompletionStage&lt;Void&gt; thenAcceptBothAsync(CompletionStage&lt;? extends U&gt; other, BiConsumer&lt;? super T, ? super U&gt; action, Executor executor);<\/code><\/pre>\n<pre><code class=\"language-java\">private static void thenAcceptBoth() throws Exception {\n    CompletableFuture&lt;Integer&gt; f1 = CompletableFuture.supplyAsync(new Supplier&lt;Integer&gt;() {\n        @Override\n        public Integer get() {\n            int t = new Random().nextInt(3);\n            try {\n                TimeUnit.SECONDS.sleep(t);\n            } catch (InterruptedException e) {\n                e.printStackTrace();\n            }\n            System.out.println(&quot;f1=&quot; + t);\n            return t;\n        }\n    });\n\n    CompletableFuture&lt;Integer&gt; f2 = CompletableFuture.supplyAsync(new Supplier&lt;Integer&gt;() {\n        @Override\n        public Integer get() {\n            int t = new Random().nextInt(3);\n            try {\n                TimeUnit.SECONDS.sleep(t);\n            } catch (InterruptedException e) {\n                e.printStackTrace();\n            }\n            System.out.println(&quot;f2=&quot; + t);\n            return t;\n        }\n    });\n    f1.thenAcceptBoth(f2, new BiConsumer&lt;Integer, Integer&gt;() {\n        @Override\n        public void accept(Integer t, Integer u) {\n            System.out.println(&quot;f1=&quot; + t + &quot;; f2=&quot; + u + &quot;;&quot;);\n        }\n    });\n}<\/code><\/pre>\n<h3>applyToEither \u65b9\u6cd5<\/h3>\n<p>\u4e24\u4e2aCompletionStage\uff0c\u8c01\u6267\u884c\u8fd4\u56de\u7684\u7ed3\u679c\u5feb\uff0c\u5c31\u7528\u90a3\u4e2aCompletionStage\u7684\u7ed3\u679c\u8fdb\u884c\u4e0b\u4e00\u6b65\u7684\u8f6c\u5316\u64cd\u4f5c\u3002<\/p>\n<pre><code class=\"language-java\">public &lt;U&gt; CompletionStage&lt;U&gt; applyToEither(CompletionStage&lt;? extends T&gt; other, Function&lt;? super T, U&gt; fn);\npublic &lt;U&gt; CompletionStage&lt;U&gt; applyToEitherAsync(CompletionStage&lt;? extends T&gt; other, Function&lt;? super T, U&gt; fn);\npublic &lt;U&gt; CompletionStage&lt;U&gt; applyToEitherAsync(CompletionStage&lt;? extends T&gt; other, Function&lt;? super T, U&gt; fn, Executor executor);<\/code><\/pre>\n<pre><code class=\"language-java\">private static void applyToEither() throws Exception {\n    CompletableFuture&lt;Integer&gt; f1 = CompletableFuture.supplyAsync(new Supplier&lt;Integer&gt;() {\n        @Override\n        public Integer get() {\n            int t = new Random().nextInt(3);\n            try {\n                TimeUnit.SECONDS.sleep(t);\n            } catch (InterruptedException e) {\n                e.printStackTrace();\n            }\n            System.out.println(&quot;f1=&quot;+t);\n            return t;\n        }\n    });\n    CompletableFuture&lt;Integer&gt; f2 = CompletableFuture.supplyAsync(new Supplier&lt;Integer&gt;() {\n        @Override\n        public Integer get() {\n            int t = new Random().nextInt(3);\n            try {\n                TimeUnit.SECONDS.sleep(t);\n            } catch (InterruptedException e) {\n                e.printStackTrace();\n            }\n            System.out.println(&quot;f2=&quot;+t);\n            return t;\n        }\n    });\n\n    CompletableFuture&lt;Integer&gt; result = f1.applyToEither(f2, new Function&lt;Integer, Integer&gt;() {\n        @Override\n        public Integer apply(Integer t) {\n            System.out.println(t);\n            return t * 2;\n        }\n    });\n\n    System.out.println(result.get());\n}<\/code><\/pre>\n<h3>acceptEither \u65b9\u6cd5<\/h3>\n<p>\u4e24\u4e2aCompletionStage\uff0c\u8c01\u6267\u884c\u8fd4\u56de\u7684\u7ed3\u679c\u5feb\uff0c\u5c31\u7528\u90a3\u4e2aCompletionStage\u7684\u7ed3\u679c\u8fdb\u884c\u4e0b\u4e00\u6b65\u7684\u6d88\u8017\u64cd\u4f5c\u3002<\/p>\n<pre><code class=\"language-java\">public CompletionStage&lt;Void&gt; acceptEither(CompletionStage&lt;? extends T&gt; other, Consumer&lt;? super T&gt; action);\npublic CompletionStage&lt;Void&gt; acceptEitherAsync(CompletionStage&lt;? extends T&gt; other, Consumer&lt;? super T&gt; action);\npublic CompletionStage&lt;Void&gt; acceptEitherAsync(CompletionStage&lt;? extends T&gt; other, Consumer&lt;? super T&gt; action, Executor executor);<\/code><\/pre>\n<pre><code class=\"language-java\">private static void acceptEither() throws Exception {\n    CompletableFuture&lt;Integer&gt; f1 = CompletableFuture.supplyAsync(new Supplier&lt;Integer&gt;() {\n        @Override\n        public Integer get() {\n            int t = new Random().nextInt(3);\n            try {\n                TimeUnit.SECONDS.sleep(t);\n            } catch (InterruptedException e) {\n                e.printStackTrace();\n            }\n            System.out.println(&quot;f1=&quot;+t);\n            return t;\n        }\n    });\n\n    CompletableFuture&lt;Integer&gt; f2 = CompletableFuture.supplyAsync(new Supplier&lt;Integer&gt;() {\n        @Override\n        public Integer get() {\n            int t = new Random().nextInt(3);\n            try {\n                TimeUnit.SECONDS.sleep(t);\n            } catch (InterruptedException e) {\n                e.printStackTrace();\n            }\n            System.out.println(&quot;f2=&quot;+t);\n            return t;\n        }\n    });\n    f1.acceptEither(f2, new Consumer&lt;Integer&gt;() {\n        @Override\n        public void accept(Integer t) {\n            System.out.println(t);\n        }\n    });\n}<\/code><\/pre>\n<h3>runAfterEither \u65b9\u6cd5<\/h3>\n<p>\u4e24\u4e2aCompletionStage\uff0c\u4efb\u4f55\u4e00\u4e2a\u5b8c\u6210\u4e86\u90fd\u4f1a\u6267\u884c\u4e0b\u4e00\u6b65\u7684\u64cd\u4f5c\uff08Runnable\uff09<\/p>\n<pre><code class=\"language-java\">public CompletionStage&lt;Void&gt; runAfterEither(CompletionStage&lt;?&gt; other, Runnable action);\npublic CompletionStage&lt;Void&gt; runAfterEitherAsync(CompletionStage&lt;?&gt; other, Runnable action);\npublic CompletionStage&lt;Void&gt; runAfterEitherAsync(CompletionStage&lt;?&gt; other, Runnable action, Executor executor);<\/code><\/pre>\n<pre><code class=\"language-java\">private static void runAfterEither() throws Exception {\n    CompletableFuture&lt;Integer&gt; f1 = CompletableFuture.supplyAsync(new Supplier&lt;Integer&gt;() {\n        @Override\n        public Integer get() {\n            int t = new Random().nextInt(3);\n            try {\n                TimeUnit.SECONDS.sleep(t);\n            } catch (InterruptedException e) {\n                e.printStackTrace();\n            }\n            System.out.println(&quot;f1=&quot; + t);\n            return t;\n        }\n    });\n\n    CompletableFuture&lt;Integer&gt; f2 = CompletableFuture.supplyAsync(new Supplier&lt;Integer&gt;() {\n        @Override\n        public Integer get() {\n            int t = new Random().nextInt(3);\n            try {\n                TimeUnit.SECONDS.sleep(t);\n            } catch (InterruptedException e) {\n                e.printStackTrace();\n            }\n            System.out.println(&quot;f2=&quot; + t);\n            return t;\n        }\n    });\n    f1.runAfterEither(f2, new Runnable() {\n        @Override\n        public void run() {\n            System.out.println(&quot;\u4e0a\u9762\u6709\u4e00\u4e2a\u5df2\u7ecf\u5b8c\u6210\u4e86\u3002&quot;);\n        }\n    });\n}<\/code><\/pre>\n<h3>runAfterBoth<\/h3>\n<p>\u4e24\u4e2aCompletionStage\uff0c\u90fd\u5b8c\u6210\u4e86\u8ba1\u7b97\u624d\u4f1a\u6267\u884c\u4e0b\u4e00\u6b65\u7684\u64cd\u4f5c\uff08Runnable\uff09<\/p>\n<pre><code class=\"language-java\">public CompletionStage&lt;Void&gt; runAfterBoth(CompletionStage&lt;?&gt; other, Runnable action);\npublic CompletionStage&lt;Void&gt; runAfterBothAsync(CompletionStage&lt;?&gt; other, Runnable action);\npublic CompletionStage&lt;Void&gt; runAfterBothAsync(CompletionStage&lt;?&gt; other, Runnable action, Executor executor);<\/code><\/pre>\n<pre><code class=\"language-java\">private static void runAfterBoth() throws Exception {\n    CompletableFuture&lt;Integer&gt; f1 = CompletableFuture.supplyAsync(new Supplier&lt;Integer&gt;() {\n        @Override\n        public Integer get() {\n            int t = new Random().nextInt(3);\n            try {\n                TimeUnit.SECONDS.sleep(t);\n            } catch (InterruptedException e) {\n                e.printStackTrace();\n            }\n            System.out.println(&quot;f1=&quot;+t);\n            return t;\n        }\n    });\n\n    CompletableFuture&lt;Integer&gt; f2 = CompletableFuture.supplyAsync(new Supplier&lt;Integer&gt;() {\n        @Override\n        public Integer get() {\n            int t = new Random().nextInt(3);\n            try {\n                TimeUnit.SECONDS.sleep(t);\n            } catch (InterruptedException e) {\n                e.printStackTrace();\n            }\n            System.out.println(&quot;f2=&quot;+t);\n            return t;\n        }\n    });\n    f1.runAfterBoth(f2, new Runnable() {\n        @Override\n        public void run() {\n            System.out.println(&quot;\u4e0a\u9762\u4e24\u4e2a\u4efb\u52a1\u90fd\u6267\u884c\u5b8c\u6210\u4e86\u3002&quot;);\n        }\n    });\n}<\/code><\/pre>\n<h3>thenCompose \u65b9\u6cd5<\/h3>\n<p><code>thenCompose<\/code>\u65b9\u6cd5\u5141\u8bb8\u5bf9\u4e24\u4e2a CompletionStage \u8fdb\u884c\u6d41\u6c34\u7ebf\u64cd\u4f5c\uff0c\u7b2c\u4e00\u4e2a\u64cd\u4f5c\u5b8c\u6210\u65f6\uff0c\u5c06\u5176\u7ed3\u679c\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9\u7b2c\u4e8c\u4e2a\u64cd\u4f5c\u3002<\/p>\n<pre><code class=\"language-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<pre><code class=\"language-java\">private static void thenCompose() throws Exception {\n    CompletableFuture&lt;Integer&gt; f = CompletableFuture.supplyAsync(new Supplier&lt;Integer&gt;() {\n        @Override\n        public Integer get() {\n            int t = new Random().nextInt(3);\n            System.out.println(&quot;t1=&quot; + t);\n            return t;\n        }\n    }).thenCompose(new Function&lt;Integer, CompletionStage&lt;Integer&gt;&gt;() {\n        @Override\n        public CompletionStage&lt;Integer&gt; apply(Integer param) {\n            return CompletableFuture.supplyAsync(new Supplier&lt;Integer&gt;() {\n                @Override\n                public Integer get() {\n                    int t = param *2;\n                    System.out.println(&quot;t2=&quot; + t);\n                    return t;\n                }\n            });\n        }\n    });\n    System.out.println(&quot;thenCompose result : &quot; + f.get());\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>runAsync \u548c supplyAsync\u65b9\u6cd5 CompletableFuture \u63d0\u4f9b\u4e86\u56db\u4e2a\u9759\u6001\u65b9\u6cd5\u6765\u521b\u5efa [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[43],"tags":[455],"class_list":["post-1791","post","type-post","status-publish","format-standard","hentry","category-java-basic","tag-future"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1791","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=1791"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1791\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=1791"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=1791"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=1791"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}