{"id":1167,"date":"2022-05-03T20:36:49","date_gmt":"2022-05-03T12:36:49","guid":{"rendered":"https:\/\/fireinsect.top\/?p=1167"},"modified":"2022-05-03T20:36:49","modified_gmt":"2022-05-03T12:36:49","slug":"completablefuture-java%e5%a4%9a%e7%ba%bf%e7%a8%8b%e6%93%8d%e4%bd%9c","status":"publish","type":"post","link":"https:\/\/fireinsect.top\/index.php\/2022\/05\/03\/completablefuture-java%e5%a4%9a%e7%ba%bf%e7%a8%8b%e6%93%8d%e4%bd%9c\/","title":{"rendered":"CompletableFuture Java\u591a\u7ebf\u7a0b\u64cd\u4f5c"},"content":{"rendered":"\n<p>CompletableFuture\u662fJava8\u4e2d\u65b0\u589e\u52a0\u7684\u7c7b\uff0c\u7ed3\u5408\u4e86Future\u7684\u4f18\u70b9\uff0c\u63d0\u4f9b\u4e86\u975e\u5e38\u5f3a\u5927\u7684Future\u7684\u62d3\u5c55\u529f\u80fd\uff0c\u5e2e\u52a9\u7b80\u5316\u4e86\u5f02\u6b65\u7f16\u7a0b\u7684\u590d\u6742\u6027\u3002<\/p>\n\n\n\n<p>\u5176\u88ab\u8bbe\u8ba1\u5728Java\u4e2d\u8fdb\u884c\u5f02\u6b65\u7f16\u7a0b\u3002\u610f\u5473\u7740\u4f1a\u5728\u4e3b\u7ebf\u7a0b\u4e4b\u5916\u521b\u5efa\u4e00\u4e2a\u72ec\u7acb\u7684\u7ebf\u7a0b\uff0c\u4e0e\u4e3b\u7ebf\u7a0b\u5206\u9694\u5f00\uff0c\u5e76\u5728\u4e0a\u9762\u8fd0\u884c\u4e00\u4e2a\u975e\u963b\u585e\u7684\u4efb\u52a1\uff0c\u7136\u540e\u901a\u77e5\u4e3b\u7ebf\u7a0b\u6210\u529f\u6216\u8005\u5931\u8d25\u3002<\/p>\n\n\n\n<!--more-->\n\n\n\n<p style=\"font-size:25px\"><strong>\u5b9e\u4f8b\u5316\u65b9\u5f0f<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"java\" class=\"language-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);\n\npublic static CompletableFuture&lt;Void&gt; runAsync(Runnable runnable);\npublic static CompletableFuture&lt;Void&gt; runAsync(Runnable runnable, Executor executor);<\/code><\/pre>\n\n\n\n<p><strong>CompletableFuture<\/strong> \u7684\u5b9e\u4f8b\u5316\u6709\u4e24\u79cd\u683c\u5f0f\u3002\u4e00\u79cd\u662fsupply\u5f00\u5934\u7684\u65b9\u6cd5\uff0c\u4e00\u79cd\u662frun\u5f00\u5934\u7684\u65b9\u6cd5<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>supply\u5f00\u5934\uff1a\u8fd9\u79cd\u65b9\u6cd5\uff0c\u53ef\u4ee5\u8fd4\u56de\u5f02\u6b65\u7ebf\u7a0b\u6267\u884c\u4e4b\u540e\u7684\u7ed3\u679c<\/li><li>run\u5f00\u5934\uff1a\u8fd9\u79cd\u4e0d\u4f1a\u8fd4\u56de\u7ed3\u679c\uff0c\u5c31\u53ea\u662f\u6267\u884c\u7ebf\u7a0b\u4efb\u52a1<\/li><\/ol>\n\n\n\n<p>\u6216\u8005\u53ef\u4ee5\u901a\u8fc7\u4e00\u4e2a\u7b80\u5355\u7684\u65e0\u53c2\u6784\u9020\u5668<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"java\" class=\"language-java\">CompletableFuture&lt;String&gt; completableFuture = new CompletableFuture&lt;String&gt;();<\/code><\/pre>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p><\/p><cite>\u5728\u5b9e\u4f8b\u5316\u65b9\u6cd5\u4e2d\uff0c\u6211\u4eec\u662f\u53ef\u4ee5\u6307\u5b9aExecutor\u53c2\u6570\u7684\uff0c\u5f53\u6211\u4eec\u4e0d\u6307\u5b9a\u7684\u8bd5\u8bdd\uff0c\u6211\u4eec\u6240\u5f00\u7684\u5e76\u884c\u7ebf\u7a0b\u4f7f\u7528\u7684\u662f\u9ed8\u8ba4\u7cfb\u7edf\u53ca\u516c\u5171\u7ebf\u7a0b\u6c60ForkJoinPool\uff0c\u800c\u4e14\u8fd9\u4e9b\u7ebf\u7a0b\u90fd\u662f\u5b88\u62a4\u7ebf\u7a0b\u3002\u6211\u4eec\u5728\u7f16\u7a0b\u7684\u65f6\u5019\u9700\u8981\u8c28\u614e\u4f7f\u7528\u5b88\u62a4\u7ebf\u7a0b\uff0c\u5982\u679c\u5c06\u6211\u4eec\u666e\u901a\u7684\u7528\u6237\u7ebf\u7a0b\u8bbe\u7f6e\u6210\u5b88\u62a4\u7ebf\u7a0b\uff0c\u5f53\u6211\u4eec\u7684\u7a0b\u5e8f\u4e3b\u7ebf\u7a0b\u7ed3\u675f\uff0cJVM\u4e2d\u4e0d\u5b58\u5728\u5176\u4f59\u7528\u6237\u7ebf\u7a0b\uff0c\u90a3\u4e48CompletableFuture\u7684\u5b88\u62a4\u7ebf\u7a0b\u4f1a\u76f4\u63a5\u9000\u51fa\uff0c\u9020\u6210\u4efb\u52a1\u65e0\u6cd5\u5b8c\u6210\u7684\u95ee\u9898\u3002<\/cite><\/blockquote>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p><\/p><cite>\u5176\u4e2dsupplyAsync\u7528\u4e8e\u6709\u8fd4\u56de\u503c\u7684\u4efb\u52a1\uff0crunAsync\u5219\u7528\u4e8e\u6ca1\u6709\u8fd4\u56de\u503c\u7684\u4efb\u52a1\u3002Executor\u53c2\u6570\u53ef\u4ee5\u624b\u52a8\u6307\u5b9a\u7ebf\u7a0b\u6c60\uff0c\u5426\u5219\u9ed8\u8ba4ForkJoinPool.commonPool()\u7cfb\u7edf\u7ea7\u516c\u5171\u7ebf\u7a0b\u6c60\uff0c\u6ce8\u610f\uff1a\u8fd9\u4e9b\u7ebf\u7a0b\u90fd\u662fDaemon\u7ebf\u7a0b\uff0c\u4e3b\u7ebf\u7a0b\u7ed3\u675fDaemon\u7ebf\u7a0b\u4e0d\u7ed3\u675f\uff0c\u53ea\u6709JVM\u5173\u95ed\u65f6\uff0c\u751f\u547d\u5468\u671f\u7ec8\u6b62\u3002<\/cite><\/blockquote>\n\n\n\n<div style=\"height:22px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p style=\"font-size:25px\"><strong>\u83b7\u53d6\u7ed3\u679c<\/strong><\/p>\n\n\n\n<p>\u540c\u6b65\u83b7\u53d6\u7ed3\u679c<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"java\" class=\"language-java\">public T    get()\npublic T    get(long timeout, TimeUnit unit)\npublic T    getNow(T valueIfAbsent)\npublic T    join()<\/code><\/pre>\n\n\n\n<p>\u4f8b\u5b50\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"java\" class=\"language-java\">CompletableFuture&lt;Integer&gt; future = new CompletableFuture&lt;&gt;();\nInteger integer = future.get();<\/code><\/pre>\n\n\n\n<p><strong>get()<\/strong>\u00a0\u65b9\u6cd5\u540c\u6837\u4f1a\u963b\u585e\u76f4\u5230\u4efb\u52a1\u5b8c\u6210\uff0c\u4e0a\u9762\u7684\u4ee3\u7801\uff0c\u4e3b\u7ebf\u7a0b\u4f1a\u4e00\u76f4\u963b\u585e\uff0c\u56e0\u4e3a\u8fd9\u79cd\u65b9\u5f0f\u521b\u5efa\u7684future\u4ece\u672a\u5b8c\u6210\u3002<\/p>\n\n\n\n<p>\u524d\u4e24\u4e2a\u65b9\u6cd5\u6bd4\u8f83\u901a\u4fd7\u6613\u61c2\uff0c\u00a0<strong>getNow()<\/strong>\u00a0\u5219\u6709\u6240\u533a\u522b\uff0c\u53c2\u6570valueIfAbsent\u7684\u610f\u601d\u662f\u5f53\u8ba1\u7b97\u7ed3\u679c\u4e0d\u5b58\u5728\u6216\u8005Now\u65f6\u523b\u6ca1\u6709\u5b8c\u6210\u4efb\u52a1\uff0c\u7ed9\u5b9a\u4e00\u4e2a\u786e\u5b9a\u7684\u503c\u3002<\/p>\n\n\n\n<p><strong>join()<\/strong>\u00a0\u4e0e<strong>get()<\/strong>\u00a0\u533a\u522b\u5728\u4e8e<strong>join()<\/strong>\u00a0\u8fd4\u56de\u8ba1\u7b97\u7684\u7ed3\u679c\u6216\u8005\u629b\u51fa\u4e00\u4e2aunchecked\u5f02\u5e38(CompletionException)\uff0c\u800c<strong>get()<\/strong>\u00a0\u8fd4\u56de\u4e00\u4e2a\u5177\u4f53\u7684\u5f02\u5e38.<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p><\/p><cite>get() \u65b9\u6cd5\u4f1a\u629b\u51fa\u7ecf\u68c0\u67e5\u7684\u5f02\u5e38\uff0c\u53ef\u88ab\u6355\u83b7\uff0c\u81ea\u5b9a\u4e49\u5904\u7406\u6216\u8005\u76f4\u63a5\u629b\u51fa\u3002<br>\u800c join() \u4f1a\u629b\u51fa\u672a\u7ecf\u68c0\u67e5\u7684\u5f02\u5e38\u3002<\/cite><\/blockquote>\n\n\n\n<div style=\"height:10px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p style=\"font-size:25px\"><strong>\u8ba1\u7b97\u5b8c\u6210\u540e\u7eed\u64cd\u4f5c1\u2014\u2014complete<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"java\" class=\"language-java\">public CompletableFuture&lt;T>  whenComplete(BiConsumer&lt;? super T,? super Throwable> action)\npublic CompletableFuture&lt;T>  whenCompleteAsync(BiConsumer&lt;? super T,? super Throwable>action)\npublic CompletableFuture&lt;T>  whenCompleteAsync(BiConsumer&lt;? super T,? super Throwable>action, Executor executor)\npublic CompletableFuture&lt;T>  exceptionally(Function&lt;Throwable,? extends T> fn)<\/code><\/pre>\n\n\n\n<p>\u65b9\u6cd51\u548c2\u7684\u533a\u522b\u5728\u4e8e\u662f\u5426\u4f7f\u7528\u5f02\u6b65\u5904\u7406\uff0c2\u548c3\u7684\u533a\u522b\u5728\u4e8e\u662f\u5426\u4f7f\u7528\u81ea\u5b9a\u4e49\u7684\u7ebf\u7a0b\u6c60\uff0c\u524d\u4e09\u4e2a\u65b9\u6cd5\u90fd\u4f1a\u63d0\u4f9b<strong>\u4e00\u4e2a\u8fd4\u56de\u7ed3\u679c\u548c\u53ef\u629b\u51fa\u5f02\u5e38<\/strong>\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528lambda\u8868\u8fbe\u5f0f\u7684\u6765\u63a5\u6536\u8fd9\u4e24\u4e2a\u53c2\u6570\uff0c\u7136\u540e\u81ea\u5df1\u5904\u7406\u3002 \u65b9\u6cd54\uff0c\u63a5\u6536\u4e00\u4e2a\u53ef\u629b\u51fa\u7684\u5f02\u5e38\uff0c\u4e14\u5fc5\u987breturn\u4e00\u4e2a\u8fd4\u56de\u503c\uff0c\u7c7b\u578b\u4e0e\u94bb\u77f3\u8868\u8fbe\u5f0f\u79cd\u7684\u7c7b\u578b\u4e00\u6837\uff0c\u8be6\u89c1\u4e0b\u6587\u7684<strong>exceptionally()<\/strong>\u00a0\u90e8\u5206\uff0c\u66f4\u8be6\u7ec6<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"java\" class=\"language-java\">CompletableFuture&lt;Integer&gt; future = CompletableFuture.supplyAsync(() -&gt; {\n            return 10086;\n        });\n        future.whenComplete((result, error) -&gt; {\n            System.out.println(\"\u62e8\u6253\"+result);\n            error.printStackTrace();\n        });<\/code><\/pre>\n\n\n\n<div style=\"height:10px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p style=\"font-size:25px\"><strong>\u8ba1\u7b97\u5b8c\u6210\u540e\u7eed\u64cd\u4f5c2\u2014\u2014handle<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"java\" class=\"language-java\">public &lt;U> CompletableFuture&lt;U>   handle(BiFunction&lt;? super T,Throwable,? extends U> fn)\npublic &lt;U> CompletableFuture&lt;U>   handleAsync(BiFunction&lt;? super T,Throwable,? extends U> fn)\npublic &lt;U> CompletableFuture&lt;U>   handleAsync(BiFunction&lt;? super T,Throwable,? extends U> fn, Executor executor)<\/code><\/pre>\n\n\n\n<p>handle\u65b9\u6cd5\u96c6\u548c\u4e0a\u9762\u7684complete\u65b9\u6cd5\u96c6\u6ca1\u6709\u533a\u522b\uff0c\u540c\u6837\u6709\u4e24\u4e2a\u53c2\u6570<strong>\u4e00\u4e2a\u8fd4\u56de\u7ed3\u679c\u548c\u53ef\u629b\u51fa\u5f02\u5e38<\/strong>\uff0c\u533a\u522b\u5c31\u5728\u4e8e\u8fd4\u56de\u503c\uff0c\u6ca1\u9519\uff0c\u867d\u7136\u540c\u6837\u8fd4\u56deCompletableFuture\u7c7b\u578b\uff0c\u4f46\u662f\u91cc\u9762\u7684\u53c2\u6570\u7c7b\u578b\uff0chandle\u65b9\u6cd5\u662f\u53ef\u4ee5\u81ea\u5b9a\u4e49\u7684\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"java\" class=\"language-java\"><em>\/\/ \u5f00\u542f\u4e00\u4e2a\u5f02\u6b65\u65b9\u6cd5\n<\/em><em><\/em>        CompletableFuture&lt;List&gt; future = CompletableFuture.supplyAsync(() -&gt; {\n            List&lt;String&gt; list = new ArrayList&lt;&gt;();\n            list.add(\"\u8bed\u6587\");\n            list.add(\"\u6570\u5b66\");\n            <em>\/\/ \u83b7\u53d6\u5f97\u5230\u4eca\u5929\u7684\u6240\u6709\u8bfe\u7a0b\n<\/em><em><\/em>            return list;\n        });\n        <em>\/\/ \u4f7f\u7528handle()\u65b9\u6cd5\u63a5\u6536list\u6570\u636e\u548cerror\u5f02\u5e38\n<\/em><em><\/em>        CompletableFuture&lt;Integer&gt; future2 = future.handle((list,error)-&gt; {\n            <em>\/\/ \u5982\u679c\u62a5\u9519\uff0c\u5c31\u6253\u5370\u51fa\u5f02\u5e38\n<\/em><em><\/em>            error.printStackTrace();\n            <em>\/\/ \u5982\u679c\u4e0d\u62a5\u9519\uff0c\u8fd4\u56de\u4e00\u4e2a\u5305\u542bInteger\u7684\u5168\u65b0\u7684CompletableFuture\n<\/em><em><\/em>            return list.size();\n             <em>\/\/ \u6ce8\u610f\u8fd9\u91cc\u7684\u4e24\u4e2aCompletableFuture\u5305\u542b\u7684\u8fd4\u56de\u7c7b\u578b\u4e0d\u540c\n<\/em><em><\/em>        });<\/code><\/pre>\n\n\n\n<div style=\"height:10px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p style=\"font-size:25px\"><strong>\u8ba1\u7b97\u5b8c\u6210\u7684\u540e\u7eed\u64cd\u4f5c3\u2014\u2014apply<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"java\" class=\"language-java\">public &lt;U> CompletableFuture&lt;U>   thenApply(Function&lt;? super T,? extends U> fn)\npublic &lt;U> CompletableFuture&lt;U>   thenApplyAsync(Function&lt;? super T,? extends U> fn)\npublic &lt;U> CompletableFuture&lt;U>   thenApplyAsync(Function&lt;? super T,? extends U> fn, Executor executor)<\/code><\/pre>\n\n\n\n<p>apply\u65b9\u6cd5\u548chandle\u65b9\u6cd5\u4e00\u6837\uff0c\u90fd\u662f\u7ed3\u675f\u8ba1\u7b97\u4e4b\u540e\u7684\u540e\u7eed\u64cd\u4f5c\uff0c\u552f\u4e00\u7684\u4e0d\u540c\u662f\uff0chandle\u65b9\u6cd5\u4f1a\u7ed9\u51fa\u5f02\u5e38\uff0c\u53ef\u4ee5\u8ba9\u7528\u6237\u81ea\u5df1\u5728\u5185\u90e8\u5904\u7406\uff0c\u800capply\u65b9\u6cd5<strong>\u53ea\u6709\u4e00\u4e2a\u8fd4\u56de\u7ed3\u679c<\/strong>\uff0c\u5982\u679c\u5f02\u5e38\u4e86\uff0c\u4f1a\u88ab\u76f4\u63a5\u629b\u51fa\uff0c\u4ea4\u7ed9\u4e0a\u4e00\u5c42\u5904\u7406\u3002 \u5982\u679c\u4e0d\u60f3\u6bcf\u4e2a\u94fe\u5f0f\u8c03\u7528\u90fd\u5904\u7406\u5f02\u5e38\uff0c\u90a3\u4e48\u5c31\u4f7f\u7528apply\u5427\u3002<\/p>\n\n\n\n<p><strong>\u4f8b\u5b50<\/strong>\uff1a\u8bf7\u770b\u4e0b\u9762\u7684<strong>exceptionally\uff08\uff09<\/strong>\u00a0\u793a\u4f8b<\/p>\n\n\n\n<div style=\"height:15px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p style=\"font-size:25px\"><strong>\u8ba1\u7b97\u5b8c\u6210\u7684\u540e\u7eed\u64cd\u4f5c4\u2014\u2014accept<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"java\" class=\"language-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\n\n\n<p>accept() \u4e09\u4e2a\u65b9\u6cd5\u53ea\u505a\u6700\u7ec8\u7ed3\u679c\u7684\u6d88\u8d39\uff0c\u6ce8\u610f\u6b64\u65f6\u8fd4\u56de\u7684CompletableFuture\u662f\u7a7a\u8fd4\u56de\u3002\u53ea\u6d88\u8d39\uff0c\u65e0\u8fd4\u56de\uff0c\u6709\u70b9\u50cf\u6d41\u5f0f\u7f16\u7a0b\u7684<strong>\u7ec8\u7aef\u64cd\u4f5c<\/strong>\u3002<br><strong>\u4f8b\u5b50<\/strong>\uff1a\u8bf7\u770b\u4e0b\u9762\u7684<strong>exceptionally\uff08\uff09<\/strong>\u00a0\u793a\u4f8b<\/p>\n\n\n\n<div style=\"height:11px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p style=\"font-size:25px\"><strong>\u6355\u83b7\u4e2d\u95f4\u4ea7\u751f\u7684\u5f02\u5e38\u2014\u2014exceptionally<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"java\" class=\"language-java\">public CompletableFuture&lt;T&gt; exceptionally(Function&lt;Throwable, ? extends T&gt; fn)<\/code><\/pre>\n\n\n\n<p><strong>exceptionally()<\/strong>\u00a0\u53ef\u4ee5\u5e2e\u6211\u4eec\u6355\u6349\u5230\u6240\u6709\u4e2d\u95f4\u8fc7\u7a0b\u7684\u5f02\u5e38\uff0c\u65b9\u6cd5\u4f1a\u7ed9\u6211\u4eec\u4e00\u4e2a\u5f02\u5e38\u4f5c\u4e3a\u53c2\u6570\uff0c\u6211\u4eec\u53ef\u4ee5\u5904\u7406\u8fd9\u4e2a\u5f02\u5e38\uff0c\u540c\u65f6\u8fd4\u56de\u4e00\u4e2a\u9ed8\u8ba4\u503c\uff0c\u8ddf<strong>\u670d\u52a1\u964d\u7ea7<\/strong>\u00a0\u6709\u70b9\u50cf\uff0c\u9ed8\u8ba4\u503c\u7684\u7c7b\u578b\u548c\u4e0a\u4e00\u4e2a\u64cd\u4f5c\u7684\u8fd4\u56de\u503c\u76f8\u540c\u3002\u00a0<strong>\u5c0f\u8d34\u58eb<\/strong>\u00a0\uff1a\u5411\u7ebf\u7a0b\u6c60\u63d0\u4ea4\u4efb\u52a1\u7684\u65f6\u5019\u53d1\u751f\u7684\u5f02\u5e38\u5c5e\u4e8e\u5916\u90e8\u5f02\u5e38\uff0c\u662f\u65e0\u6cd5\u6355\u6349\u5230\u7684\uff0c\u6bd5\u7adf\u8fd8\u6ca1\u6709\u5f00\u59cb\u6267\u884c\u4efb\u52a1\u3002<strong>exceptionally\uff08\uff09<\/strong>\u00a0\u65e0\u6cd5\u6355\u6349<strong>RejectedExecutionException\uff08\uff09<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"java\" class=\"language-java\"><em>\/\/ \u5b9e\u4f8b\u5316\u4e00\u4e2aCompletableFuture,\u8fd4\u56de\u503c\u662fInteger\n<\/em><em><\/em>        CompletableFuture&lt;Integer&gt; future = CompletableFuture.supplyAsync(() -&gt; {\n            <em>\/\/ \u8fd4\u56denull\n<\/em><em><\/em>            return null;\n        });\n\n        CompletableFuture&lt;String&gt; exceptionally = future.thenApply(result -&gt; {\n            <em>\/\/ \u5236\u9020\u4e00\u4e2a\u7a7a\u6307\u9488\u5f02\u5e38NPE\n<\/em><em><\/em>            int i = result;\n            return i;\n        }).thenApply(result -&gt; {\n            <em>\/\/ \u8fd9\u91cc\u4e0d\u4f1a\u6267\u884c\uff0c\u56e0\u4e3a\u4e0a\u9762\u51fa\u73b0\u4e86\u5f02\u5e38\n<\/em><em><\/em>            String words = \"\u73b0\u5728\u662f\" + result + \"\u70b9\u949f\";\n            return words;\n        }).exceptionally(error -&gt; {\n            <em>\/\/ \u6211\u4eec\u9009\u62e9\u5728\u8fd9\u91cc\u6253\u5370\u51fa\u5f02\u5e38\n<\/em><em><\/em>            error.printStackTrace();\n            <em>\/\/ \u5e76\u4e14\u5f53\u5f02\u5e38\u53d1\u751f\u7684\u65f6\u5019\uff0c\u6211\u4eec\u8fd4\u56de\u4e00\u4e2a\u9ed8\u8ba4\u7684\u6587\u5b57\n<\/em><em><\/em>            return \"\u51fa\u9519\u554a~\";\n        });\n\n        exceptionally.thenAccept(System.out::println);\n\n    }<\/code><\/pre>\n\n\n\n<div style=\"height:21px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">\u7ec4\u5408\u5f0f\u5f02\u6b65\u7f16\u7a0b<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\u7ec4\u5408\u4e24\u4e2acompletableFuture<\/h3>\n\n\n\n<p>Future\u505a\u4e0d\u5230\u7684\u4e8b:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>\u5c06\u4e24\u4e2a\u5f02\u6b65\u8ba1\u7b97\u5408\u5e76\u4e3a\u4e00\u4e2a\uff0c\u8fd9\u4e24\u4e2a\u5f02\u6b65\u8ba1\u7b97\u4e4b\u95f4\u76f8\u4e92\u72ec\u7acb\uff0c\u540c\u65f6\u7b2c\u4e8c\u4e2a\u53c8\u4f9d\u8d56\u4e8e\u7b2c\u4e00\u4e2a\u7684\u7ed3\u679c\u3002<\/li><\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">thenApply()<\/h3>\n\n\n\n<p><strong>thenApply()<\/strong>\u53ef\u4ee5\u5c06\u4e0a\u4e00\u6b65\u7684\u7ed3\u679c\u8fdb\u884c\u4e0b\u4e00\u6b65\u64cd\u4f5c<\/p>\n\n\n\n<p>\u5047\u8bbe\u4e00\u4e2a\u573a\u666f\uff0c\u6211\u662f\u4e00\u4e2a\u5c0f\u5b66\u751f\uff0c\u6211\u60f3\u77e5\u9053\u4eca\u5929\u6211\u9700\u8981\u4e0a\u51e0\u95e8\u8bfe\u7a0b \u6b64\u65f6\u6211\u9700\u8981\u4e24\u4e2a\u6b65\u9aa4\uff0c1.\u6839\u636e\u6211\u7684\u540d\u5b57\u83b7\u53d6\u6211\u7684\u5b66\u751f\u4fe1\u606f 2.\u6839\u636e\u6211\u7684\u5b66\u751f\u4fe1\u606f\u67e5\u8be2\u8bfe\u7a0b \u6211\u4eec\u53ef\u4ee5\u7528\u4e0b\u9762\u8fd9\u79cd\u65b9\u5f0f\u6765\u94fe\u5f0f\u8c03\u7528api\uff0c\u4f7f\u7528\u4e0a\u4e00\u6b65\u7684\u7ed3\u679c\u8fdb\u884c\u4e0b\u4e00\u6b65\u64cd\u4f5c<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"java\" class=\"language-java\">CompletableFuture&lt;List&lt;Lesson&gt;&gt; future = CompletableFuture.supplyAsync(() -&gt; {\n            <em>\/\/ \u6839\u636e\u5b66\u751f\u59d3\u540d\u83b7\u53d6\u5b66\u751f\u4fe1\u606f\n<\/em><em><\/em>            return StudentService.getStudent(name);\n        }).thenApply(student -&gt; {\n            <em>\/\/ \u518d\u6839\u636e\u5b66\u751f\u4fe1\u606f\u83b7\u53d6\u4eca\u5929\u7684\u8bfe\u7a0b\n<\/em><em><\/em>            return LessonsService.getLessons(student);\n        });<\/code><\/pre>\n\n\n\n<p>\u6211\u4eec\u6839\u636e\u5b66\u751f\u59d3\u540d\u83b7\u53d6\u5b66\u751f\u4fe1\u606f\uff0c\u7136\u540e\u4f7f\u7528\u628a\u5f97\u5230\u7684\u5b66\u751f\u4fe1\u606fstudent\u4f20\u9012\u5230<strong>apply\uff08\uff09<\/strong>&nbsp;\u65b9\u6cd5\u518d\u83b7\u53d6\u5f97\u5230\u5b66\u751f\u4eca\u5929\u7684\u8bfe\u7a0b\u5217\u8868\u3002<\/p>\n\n\n\n<div style=\"height:19px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<ul class=\"wp-block-list\"><li>\u5c06\u4e24\u4e2a\u5f02\u6b65\u8ba1\u7b97\u5408\u5e76\u4e3a\u4e00\u4e2a\uff0c\u8fd9\u4e24\u4e2a\u5f02\u6b65\u8ba1\u7b97\u4e4b\u95f4\u76f8\u4e92\u72ec\u7acb\uff0c\u4e92\u4e0d\u4f9d\u8d56<\/li><\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">thenCompose()<\/h3>\n\n\n\n<p><strong>thenCompose()<\/strong> \u53ef\u4ee5\u8fdb\u884c\u4e24\u4e2a\u5f02\u6b65\u64cd\u4f5c\u7684\u503c\u4f20\u9012\uff1a<\/p>\n\n\n\n<p>\u5047\u8bbe\u4e00\u4e2a\u573a\u666f\uff0c\u6211\u662f\u4e00\u4e2a\u5c0f\u5b66\u751f\uff0c\u4eca\u5929\u6709\u52b3\u6280\u8bfe\u548c\u7f8e\u672f\u8bfe\uff0c\u6211\u9700\u8981\u67e5\u8be2\u5230\u4eca\u5929\u9700\u8981\u5e26\u4ec0\u4e48\u4e1c\u897f\u5230\u5b66\u6821<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">CompletableFuture&lt;List&lt;String>> total = CompletableFuture.supplyAsync(() -> {\n            <em>\/\/ \u7b2c\u4e00\u4e2a\u4efb\u52a1\u83b7\u53d6\u7f8e\u672f\u8bfe\u9700\u8981\u5e26\u7684\u4e1c\u897f\uff0c\u8fd4\u56de\u4e00\u4e2alist\n<\/em>            List&lt;String> stuff = new ArrayList&lt;>();\n            stuff.add(\"\u753b\u7b14\");\n            stuff.add(\"\u989c\u6599\");\n            return stuff;\n        }).thenCompose(list -> {\n            <em>\/\/ \u5411\u7b2c\u4e8c\u4e2a\u4efb\u52a1\u4f20\u9012\u53c2\u6570list(\u4e0a\u4e00\u4e2a\u4efb\u52a1\u7f8e\u672f\u8bfe\u6240\u9700\u7684\u4e1c\u897flist)\n<\/em>            CompletableFuture&lt;List&lt;String>> insideFuture = CompletableFuture.supplyAsync(() -> {\n                List&lt;String> stuff = new ArrayList&lt;>();\n                <em>\/\/ \u7b2c\u4e8c\u4e2a\u4efb\u52a1\u83b7\u53d6\u52b3\u6280\u8bfe\u6240\u9700\u7684\u5de5\u5177\n<\/em>                stuff.add(\"\u526a\u5200\");\n                stuff.add(\"\u6298\u7eb8\");\n                <em>\/\/ \u5408\u5e76\u4e24\u4e2alist\uff0c\u83b7\u53d6\u8bfe\u7a0b\u6240\u9700\u6240\u6709\u5de5\u5177\n<\/em>                List&lt;String> allStuff = Stream.of(list, stuff)\n                                .flatMap(Collection::stream).collect(Collectors.toList());\n                return allStuff;\n            });\n            return insideFuture;\n        });\n        System.out.println(total.join().size());<\/code><\/pre>\n\n\n\n<p>\u6211\u4eec\u901a\u8fc7<strong>CompletableFuture.supplyAsync(\uff09<\/strong>\u00a0\u65b9\u6cd5\u521b\u5efa\u7b2c\u4e00\u4e2a\u4efb\u52a1\uff0c\u83b7\u5f97\u7f8e\u672f\u8bfe\u6240\u9700\u7684\u7269\u54c1list\uff0c\u7136\u540e\u4f7f\u7528<strong>thenCompose\uff08\uff09<\/strong>\u00a0\u63a5\u53e3\u4f20\u9012list\u5230\u7b2c\u4e8c\u4e2a\u4efb\u52a1\uff0c\u7136\u540e\u7b2c\u4e8c\u4e2a\u4efb\u52a1\u83b7\u53d6\u52b3\u6280\u8bfe\u6240\u9700\u7684\u7269\u54c1\uff0c\u6574\u5408\u4e4b\u540e\u518d\u8fd4\u56de\u3002\u81f3\u6b64\u6211\u4eec\u5b8c\u6210\u4e24\u4e2a\u4efb\u52a1\u7684\u5408\u5e76\u3002 \uff08\u8bf4\u5b9e\u8bdd\uff0c\u7528compose\u53bb\u5b9e\u73b0\u8fd9\u4e2a\u4e1a\u52a1\u573a\u666f\u770b\u8d77\u6765\u6709\u70b9\u522b\u626d\uff0c\u6211\u4eec\u770b\u4e0b\u4e00\u4e2a\u4f8b\u5b50\uff09<\/p>\n\n\n\n<div style=\"height:10px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<ul class=\"wp-block-list\"><li>\u5c06\u4e24\u4e2a\u5f02\u6b65\u8ba1\u7b97\u5408\u5e76\u4e3a\u4e00\u4e2a\uff0c\u8fd9\u4e24\u4e2a\u5f02\u6b65\u8ba1\u7b97\u4e4b\u95f4\u76f8\u4e92\u72ec\u7acb\uff0c\u4e92\u4e0d\u4f9d\u8d56<\/li><\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">thenCombine(\uff09<\/h3>\n\n\n\n<p><strong>thenCombine(\uff09<\/strong>\u53ef\u4ee5\u5c06\u4e24\u4e2aCompletableFuture\u5f02\u6b65\u8ba1\u7b97\u8fdb\u884c\u7ec4\u5408<\/p>\n\n\n\n<p>\u8fd8\u662f\u4e0a\u9762\u90a3\u4e2a\u573a\u666f\uff0c\u6211\u662f\u4e00\u4e2a\u5c0f\u5b66\u751f\uff0c\u4eca\u5929\u6709\u52b3\u6280\u8bfe\u548c\u7f8e\u672f\u8bfe\uff0c\u6211\u9700\u8981\u67e5\u8be2\u5230\u4eca\u5929\u9700\u8981\u5e26\u4ec0\u4e48\u4e1c\u897f\u5230\u5b66\u6821<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">CompletableFuture&lt;List&lt;String&gt;&gt; painting = CompletableFuture.supplyAsync(() -&gt; {\n            <em>\/\/ \u7b2c\u4e00\u4e2a\u4efb\u52a1\u83b7\u53d6\u7f8e\u672f\u8bfe\u9700\u8981\u5e26\u7684\u4e1c\u897f\uff0c\u8fd4\u56de\u4e00\u4e2alist\n<\/em><em><\/em>            List&lt;String&gt; stuff = new ArrayList&lt;&gt;();\n            stuff.add(\"\u753b\u7b14\");\n            stuff.add(\"\u989c\u6599\");\n            return stuff;\n        });\n        CompletableFuture&lt;List&lt;String&gt;&gt; handWork = CompletableFuture.supplyAsync(() -&gt; {\n            <em>\/\/ \u7b2c\u4e8c\u4e2a\u4efb\u52a1\u83b7\u53d6\u52b3\u6280\u8bfe\u9700\u8981\u5e26\u7684\u4e1c\u897f\uff0c\u8fd4\u56de\u4e00\u4e2alist\n<\/em><em><\/em>            List&lt;String&gt; stuff = new ArrayList&lt;&gt;();\n            stuff.add(\"\u526a\u5200\");\n            stuff.add(\"\u6298\u7eb8\");\n            return stuff;\n        });\n        CompletableFuture&lt;List&lt;String&gt;&gt; total = painting\n                <em>\/\/ \u4f20\u5165handWork\u5217\u8868\uff0c\u7136\u540e\u5f97\u5230\u4e24\u4e2aCompletableFuture\u7684\u53c2\u6570Stuff1\u548c2\n<\/em><em><\/em>                .thenCombine(handWork, (stuff1, stuff2) -&gt; {\n                    <em>\/\/ \u5408\u5e76\u6210\u65b0\u7684list\n<\/em><em><\/em>                    List&lt;String&gt; totalStuff = Stream.of(stuff1, stuff1)\n                            .flatMap(Collection::stream)\n                            .collect(Collectors.toList());\n                    return totalStuff;\n                });\n        System.out.println(JSONObject.toJSONString(total.join()));<\/code><\/pre>\n\n\n\n<div style=\"height:29px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<ul class=\"wp-block-list\"><li>\u7b49\u5f85Future\u96c6\u5408\u4e2d\u7684\u6240\u6709\u4efb\u52a1\u90fd\u5b8c\u6210\u3002<\/li><\/ul>\n\n\n\n<p style=\"font-size:25px\"><strong>\u83b7\u53d6\u6240\u6709\u5b8c\u6210\u7ed3\u679c\u2014\u2014allOf<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"java\" class=\"language-java\">public static CompletableFuture&lt;Void&gt; allOf(CompletableFuture&lt;?&gt;... cfs)<\/code><\/pre>\n\n\n\n<p>allOf\u65b9\u6cd5\uff0c\u5f53\u6240\u6709\u7ed9\u5b9a\u7684\u4efb\u52a1\u5b8c\u6210\u540e\uff0c\u8fd4\u56de\u4e00\u4e2a\u5168\u65b0\u7684\u5df2\u5b8c\u6210CompletableFuture<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"java\" class=\"language-java\">CompletableFuture&lt;Integer&gt; future1 = CompletableFuture.supplyAsync(() -&gt; {\n            try {\n                <em>\/\/\u4f7f\u7528sleep()\u6a21\u62df\u8017\u65f6\u64cd\u4f5c\n<\/em><em><\/em>                TimeUnit.SECONDS.sleep(2);\n            } catch (InterruptedException e) {\n                e.printStackTrace();\n            }\n            return 1;\n        });\n\n        CompletableFuture&lt;Integer&gt; future2 = CompletableFuture.supplyAsync(() -&gt; {\n            return 2;\n        });\n        CompletableFuture.allOf(future1, future1);\n        <em>\/\/ \u8f93\u51fa3\n<\/em><em><\/em>        System.out.println(future1.join()+future2.join());<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" style=\"font-size:25px\">\u83b7\u53d6\u7387\u5148\u5b8c\u6210\u7684\u4efb\u52a1\u7ed3\u679c\u2014\u2014anyOf<\/h3>\n\n\n\n<ul class=\"wp-block-list\"><li>\u4ec5\u7b49\u5f85Future\u96c6\u5408\u79cd\u6700\u5feb\u7ed3\u675f\u7684\u4efb\u52a1\u5b8c\u6210\uff08\u6709\u53ef\u80fd\u56e0\u4e3a\u4ed6\u4eec\u8bd5\u56fe\u901a\u8fc7\u4e0d\u540c\u7684\u65b9\u5f0f\u8ba1\u7b97\u540c\u4e00\u4e2a\u503c\uff09\uff0c\u5e76\u8fd4\u56de\u5b83\u7684\u7ed3\u679c\u3002&nbsp;<strong>\u5c0f\u8d34\u58eb<\/strong>&nbsp;\uff1a\u5982\u679c\u6700\u5feb\u5b8c\u6210\u7684\u4efb\u52a1\u51fa\u73b0\u4e86\u5f02\u5e38\uff0c\u4e5f\u4f1a\u5148\u8fd4\u56de\u5f02\u5e38\uff0c\u5982\u679c\u5bb3\u6015\u51fa\u9519\u53ef\u4ee5\u52a0\u4e2a<strong>exceptionally()<\/strong>&nbsp;\u53bb\u5904\u7406\u4e00\u4e0b\u53ef\u80fd\u53d1\u751f\u7684\u5f02\u5e38\u5e76\u8bbe\u5b9a\u9ed8\u8ba4\u8fd4\u56de\u503c<\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"java\" class=\"language-java\">public static CompletableFuture&lt;Object&gt; anyOf(CompletableFuture&lt;?&gt;... cfs)\nCompletableFuture&lt;Integer&gt; future = CompletableFuture.supplyAsync(() -&gt; {\n            throw new NullPointerException();\n        });\n\n        CompletableFuture&lt;Integer&gt; future2 = CompletableFuture.supplyAsync(() -&gt; {\n            try {\n                <em>\/\/ \u7761\u77203s\u6a21\u62df\u5ef6\u65f6\n<\/em><em><\/em>                TimeUnit.SECONDS.sleep(3);\n            } catch (InterruptedException e) {\n                e.printStackTrace();\n            }\n            return 1;\n        });\n        CompletableFuture&lt;Object&gt; anyOf = CompletableFuture\n                .anyOf(future, future2)\n                .exceptionally(error -&gt; {\n                    error.printStackTrace();\n                    return 2;\n                });\n        System.out.println(anyOf.join());<\/code><\/pre>\n\n\n\n<p class=\"has-large-font-size\">\u4f8b\u5b50<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u591a\u4e2a\u65b9\u6cd5\u7ec4\u5408\u4f7f\u7528<\/h3>\n\n\n\n<ul class=\"wp-block-list\"><li>\u901a\u8fc7\u7f16\u7a0b\u65b9\u5f0f\u5b8c\u6210\u4e00\u4e2aFuture\u4efb\u52a1\u7684\u6267\u884c\uff08\u5373\u4ee5\u624b\u5de5\u8bbe\u5b9a\u5f02\u6b65\u64cd\u4f5c\u7ed3\u679c\u7684\u65b9\u5f0f\uff09\u3002<\/li><li>\u5e94\u5bf9Future\u7684\u5b8c\u6210\u65f6\u95f4\uff08\u5373\u5f53Future\u7684\u5b8c\u6210\u65f6\u95f4\u5b8c\u6210\u65f6\u4f1a\u6536\u5230\u901a\u77e5\uff0c\u5e76\u80fd\u4f7f\u7528Future\u7684\u8ba1\u7b97\u7ed3\u679c\u8fdb\u884c\u4e0b\u4e00\u6b65\u7684\u7684\u64cd\u4f5c\uff0c\u4e0d\u53ea\u662f\u7b80\u5355\u5730\u963b\u585e\u7b49\u5f85\u64cd\u4f5c\u7684\u7ed3\u679c\uff09<\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"java\" class=\"language-java\">public static void main(String[] args) {\n        CompletableFuture.supplyAsync(() -&gt; 1)\n                .whenComplete((result, error) -&gt; {\n                    System.out.println(result);\n                    error.printStackTrace();\n                })\n                .handle((result, error) -&gt; {\n                    error.printStackTrace();\n                    return error;\n                })\n                .thenApply(Object::toString)\n                .thenApply(Integer::valueOf)\n                .thenAccept((param) -&gt; System.out.println(\"done\"));\n    }<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u5faa\u73af\u521b\u5efa\u5e76\u53d1\u4efb\u52a1<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"java\" class=\"language-java\">public static void main(String[] args) {\n        long begin = System.currentTimeMillis();\n        <em>\/\/ \u81ea\u5b9a\u4e49\u4e00\u4e2a\u7ebf\u7a0b\u6c60\n<\/em><em><\/em>        ExecutorService executorService = Executors.newFixedThreadPool(10);\n        <em>\/\/ \u5faa\u73af\u521b\u5efa10\u4e2aCompletableFuture\n<\/em><em><\/em>        List&lt;CompletableFuture&lt;Integer&gt;&gt; collect = IntStream.range(1, 10).mapToObj(i -&gt; {\n            CompletableFuture&lt;Integer&gt; future = CompletableFuture.supplyAsync(() -&gt; {\n                <em>\/\/ \u5728i=5\u7684\u65f6\u5019\u629b\u51fa\u4e00\u4e2aNPE\n<\/em><em><\/em>                if (i == 5) {\n                    throw new NullPointerException();\n                }\n                try {\n                    <em>\/\/ \u6bcf\u4e2a\u4f9d\u6b21\u7761\u77201-9s\uff0c\u6a21\u62df\u7ebf\u7a0b\u8017\u65f6\n<\/em><em><\/em>                    TimeUnit.SECONDS.sleep(i);\n                } catch (InterruptedException e) {\n                    e.printStackTrace();\n                }\n                System.out.println(i);\n                return i;\n            }, executorService)\n                    <em>\/\/ \u8fd9\u91cc\u5904\u7406\u4e00\u4e0bi=5\u65f6\u51fa\u73b0\u7684NPE\n<\/em><em><\/em>                    <em>\/\/ \u5982\u679c\u8fd9\u91cc\u4e0d\u5904\u7406\u5f02\u5e38\uff0c\u90a3\u4e48\u5f02\u5e38\u4f1a\u5728\u6240\u6709\u4efb\u52a1\u5b8c\u6210\u540e\u629b\u51fa,\u5c0f\u4f19\u4f34\u53ef\u81ea\u884c\u6d4b\u8bd5\n<\/em><em><\/em>                    .exceptionally(Error -&gt; {\n                        try {\n                            TimeUnit.SECONDS.sleep(5);\n                            System.out.println(100);\n                        } catch (InterruptedException e) {\n                            e.printStackTrace();\n                        }\n                        return 100;\n                    });\n            return future;\n        }).collect(Collectors.toList());\n        <em>\/\/ List\u5217\u8868\u8f6c\u6210CompletableFuture\u7684Array\u6570\u7ec4,\u4f7f\u5176\u53ef\u4ee5\u4f5c\u4e3aallOf()\u7684\u53c2\u6570\n<\/em><em><\/em>        <em>\/\/ \u4f7f\u7528join()\u65b9\u6cd5\u4f7f\u5f97\u4e3b\u7ebf\u7a0b\u963b\u585e\uff0c\u5e76\u7b49\u5f85\u6240\u6709\u5e76\u884c\u7ebf\u7a0b\u5b8c\u6210\n<\/em><em><\/em>        CompletableFuture.allOf(collect.toArray(new CompletableFuture[]{})).join();\n        System.out.println(\"\u6700\u7ec8\u8017\u65f6\" + (System.currentTimeMillis() - begin) + \"\u6beb\u79d2\");\n        executorService.shutdown();\n    }<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u4f7f\u7528CompletableFuture\u573a\u666f<\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li>\u6267\u884c\u6bd4\u8f83\u8017\u65f6\u7684\u64cd\u4f5c\u65f6\uff0c\u5c24\u5176\u662f\u90a3\u4e9b\u4f9d\u8d56\u4e00\u4e2a\u6216\u591a\u4e2a\u8fdc\u7a0b\u670d\u52a1\u7684\u64cd\u4f5c\uff0c\u4f7f\u7528\u5f02\u6b65\u4efb\u52a1\u53ef\u4ee5\u6539\u5584\u7a0b\u5e8f\u7684\u6027\u80fd\uff0c\u52a0\u5feb\u7a0b\u5e8f\u7684\u54cd\u5e94\u901f\u5ea6<\/li><li>\u4f7f\u7528CompletableFuture\u7c7b\uff0c\u5b83\u63d0\u4f9b\u4e86\u5f02\u5e38\u7ba1\u7406\u7684\u673a\u5236\uff0c\u8ba9\u4f60\u6709\u673a\u4f1a\u629b\u51fa\u3001\u7ba1\u7406\u5f02\u6b65\u4efb\u52a1\u6267\u884c\u79cd\u53d1\u751f\u7684\u5f02\u5e38<\/li><li>\u5982\u679c\u8fd9\u4e9b\u5f02\u6b65\u4efb\u52a1\u4e4b\u95f4\u76f8\u4e92\u72ec\u7acb\uff0c\u6216\u8005\u4ed6\u4eec\u4e4b\u95f4\u7684\u7684\u67d0\u4e00\u4e9b\u7684\u7ed3\u679c\u662f\u53e6\u4e00\u4e9b\u7684\u8f93\u5165\uff0c\u4f60\u53ef\u4ee5\u8bb2\u8fd9\u4e9b\u5f02\u6b65\u4efb\u52a1\u6784\u9020\u6216\u5408\u5e76\u6210\u4e00\u4e2a<\/li><\/ul>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>CompletableFuture\u662fJava8\u4e2d\u65b0\u589e\u52a0\u7684\u7c7b\uff0c\u7ed3\u5408\u4e86Future\u7684\u4f18\u70b9\uff0c\u63d0\u4f9b\u4e86\u975e\u5e38\u5f3a\u5927\u7684Futu [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","footnotes":""},"categories":[6,1],"tags":[],"class_list":["post-1167","post","type-post","status-publish","format-standard","hentry","category-6","category-wcd"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/fireinsect.top\/index.php\/wp-json\/wp\/v2\/posts\/1167"}],"collection":[{"href":"https:\/\/fireinsect.top\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fireinsect.top\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fireinsect.top\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fireinsect.top\/index.php\/wp-json\/wp\/v2\/comments?post=1167"}],"version-history":[{"count":2,"href":"https:\/\/fireinsect.top\/index.php\/wp-json\/wp\/v2\/posts\/1167\/revisions"}],"predecessor-version":[{"id":1171,"href":"https:\/\/fireinsect.top\/index.php\/wp-json\/wp\/v2\/posts\/1167\/revisions\/1171"}],"wp:attachment":[{"href":"https:\/\/fireinsect.top\/index.php\/wp-json\/wp\/v2\/media?parent=1167"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fireinsect.top\/index.php\/wp-json\/wp\/v2\/categories?post=1167"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fireinsect.top\/index.php\/wp-json\/wp\/v2\/tags?post=1167"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}