{"id":79197,"date":"2026-03-01T11:25:23","date_gmt":"2026-03-01T03:25:23","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/79197.html"},"modified":"2026-03-01T11:25:23","modified_gmt":"2026-03-01T03:25:23","slug":"completablefuture%e4%b8%ba%e4%bd%95%e8%83%bd%e6%9b%bf%e4%bb%a3future","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/79197.html","title":{"rendered":"CompletableFuture\u4e3a\u4f55\u80fd\u66ff\u4ee3Future"},"content":{"rendered":"<h2>\u4e00\u3001\u524d\u8a00<\/h2>\n<p>Java\u5f02\u6b65\u7f16\u7a0b\u4eceThread\u3001Runnable\u4e00\u8def\u6f14\u8fdb\u5230Future&#xff0c;\u4f46\u4f20\u7edf\u65b9\u6848\u59cb\u7ec8\u5b58\u5728\u96be\u4ee5\u89e3\u51b3\u7684\u75db\u70b9\u3002\u800cCompletableFuture\u7684\u51fa\u73b0&#xff0c;\u8ba9Java\u5f02\u6b65\u7f16\u7a0b\u8fdb\u5165\u4e86\u66f4\u7075\u6d3b\u3001\u66f4\u9ad8\u6548\u7684\u9636\u6bb5\u3002\u672c\u7bc7\u4f5c\u4e3a\u7cfb\u5217\u7684\u5165\u95e8\u7bc7&#xff0c;\u5c06\u5148\u5e26\u5927\u5bb6\u7406\u6e05\u4f20\u7edf\u5f02\u6b65\u7f16\u7a0b\u7684\u5c40\u9650&#xff0c;\u518d\u6df1\u5165\u8bb2\u89e3CompletableFuture\u7684\u6838\u5fc3\u4f18\u52bf\u4e0e\u57fa\u7840\u4f7f\u7528\u65b9\u6cd5&#xff0c;\u5e2e\u4f60\u5feb\u901f\u5efa\u7acb\u5bf9CompletableFuture\u7684\u8ba4\u77e5\u3002<\/p>\n<\/p>\n<h2>\u4e8c\u3001\u4f20\u7edf\u5f02\u6b65\u7f16\u7a0b\u7684\u75db\u70b9<\/h2>\n<p>\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d&#xff0c;\u6211\u4eec\u7ecf\u5e38\u4f1a\u9047\u5230\u9700\u8981\u201c\u5e76\u884c\u5904\u7406\u4efb\u52a1\u201d\u7684\u573a\u666f&#xff0c;\u6bd4\u5982\u67e5\u8be2\u7528\u6237\u4fe1\u606f\u7684\u540c\u65f6\u52a0\u8f7d\u7528\u6237\u8ba2\u5355\u6570\u636e&#xff0c;\u6216\u8005\u6279\u91cf\u8c03\u7528\u591a\u4e2a\u7b2c\u4e09\u65b9\u63a5\u53e3\u83b7\u53d6\u6570\u636e\u3002\u4e3a\u4e86\u907f\u514d\u540c\u6b65\u6267\u884c\u5bfc\u81f4\u7684\u6027\u80fd\u4f4e\u4e0b&#xff0c;\u5f02\u6b65\u7f16\u7a0b\u6210\u4e3a\u4e86\u5fc5\u7136\u9009\u62e9\u3002\u4f46\u5728CompletableFuture\u51fa\u73b0\u4e4b\u524d&#xff0c;Java\u7684\u5f02\u6b65\u65b9\u6848\u90fd\u5b58\u5728\u660e\u663e\u7684\u4e0d\u8db3\u3002<\/p>\n<h3>1\u3001Thread&#043;Runnable\u7684\u5c40\u9650\u6027<\/h3>\n<p>\u6700\u65e9\u671f\u7684\u5f02\u6b65\u5b9e\u73b0\u662f\u901a\u8fc7Thread\u914d\u5408Runnable\u63a5\u53e3\u5b8c\u6210\u7684\u3002Runnable\u63a5\u53e3\u5b9a\u4e49\u4e86\u4e00\u4e2a\u65e0\u8fd4\u56de\u503c\u3001\u65e0\u5f02\u5e38\u629b\u51fa\u7684run()\u65b9\u6cd5&#xff0c;\u6211\u4eec\u53ef\u4ee5\u5728\u5176\u4e2d\u5c01\u88c5\u5f02\u6b65\u4efb\u52a1\u903b\u8f91\u3002<\/p>\n<p>\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b&#xff1a;<\/p>\n<p>public\u00a0class\u00a0ThreadRunnableDemo\u00a0{<br \/>\n\u00a0 \u00a0\u00a0public\u00a0static\u00a0void\u00a0main(String[]\u00a0args)\u00a0{<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0\u00a0\/\/ \u5f02\u6b65\u6267\u884c\u4efb\u52a1<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0\u00a0new\u00a0Thread(new\u00a0Runnable() {<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 &#064;Override<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0public\u00a0void\u00a0run()\u00a0{<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0\/\/ \u6a21\u62df\u8017\u65f6\u64cd\u4f5c&#xff1a;\u67e5\u8be2\u7528\u6237\u8ba2\u5355<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0try\u00a0{<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 Thread.sleep(1000);<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 }\u00a0catch\u00a0(InterruptedException e) {<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 e.printStackTrace();<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 }<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 System.out.println(&#034;\u5f02\u6b65\u4efb\u52a1\u6267\u884c\u5b8c\u6210&#xff1a;\u67e5\u8be2\u5230\u7528\u6237\u8ba2\u5355\u6570\u636e&#034;);<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 }<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 }).start();<\/p>\n<p>\u00a0 \u00a0 \u00a0 \u00a0\u00a0\/\/ \u4e3b\u7ebf\u7a0b\u7ee7\u7eed\u6267\u884c\u5176\u4ed6\u64cd\u4f5c<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 System.out.println(&#034;\u4e3b\u7ebf\u7a0b\u6267\u884c\u5b8c\u6bd5&#034;);<br \/>\n\u00a0 \u00a0 }<br \/>\n} <\/p>\n<p>\u8fd0\u884c\u7ed3\u679c&#xff1a;<\/p>\n<p>\u4e3b\u7ebf\u7a0b\u6267\u884c\u5b8c\u6bd5<br \/>\n\u5f02\u6b65\u4efb\u52a1\u6267\u884c\u5b8c\u6210&#xff1a;\u67e5\u8be2\u5230\u7528\u6237\u8ba2\u5355\u6570\u636e <\/p>\n<p>\u4ece\u793a\u4f8b\u4e2d\u53ef\u4ee5\u770b\u51fa&#xff0c;Thread&#043;Runnable\u786e\u5b9e\u5b9e\u73b0\u4e86\u5f02\u6b65\u6267\u884c&#xff0c;\u4f46\u5b83\u7684\u5c40\u9650\u6027\u975e\u5e38\u660e\u663e&#xff1a;<\/p>\n<ul>\n<li>\n<p>\u65e0\u6cd5\u83b7\u53d6\u4efb\u52a1\u8fd4\u56de\u503c&#xff1a;Runnable\u7684run()\u65b9\u6cd5\u65e0\u8fd4\u56de\u503c&#xff0c;\u82e5\u5f02\u6b65\u4efb\u52a1\u9700\u8981\u4ea7\u751f\u7ed3\u679c&#xff08;\u6bd4\u5982\u67e5\u8be2\u5230\u7684\u8ba2\u5355\u6570\u636e&#xff09;&#xff0c;\u65e0\u6cd5\u76f4\u63a5\u4f20\u9012\u7ed9\u4e3b\u7ebf\u7a0b&#xff0c;\u53ea\u80fd\u901a\u8fc7\u5171\u4eab\u53d8\u91cf\u7b49\u95f4\u63a5\u65b9\u5f0f&#xff0c;\u64cd\u4f5c\u7e41\u7410\u4e14\u5bb9\u6613\u5f15\u53d1\u7ebf\u7a0b\u5b89\u5168\u95ee\u9898\u3002<\/p>\n<\/li>\n<\/ul>\n<ul>\n<li>\n<p>\u96be\u4ee5\u7ba1\u7406\u7ebf\u7a0b\u8d44\u6e90&#xff1a;\u6bcf\u6b21\u521b\u5efa\u65b0Thread\u4f1a\u6d88\u8017\u7cfb\u7edf\u8d44\u6e90&#xff0c;\u9891\u7e41\u521b\u5efa\u7ebf\u7a0b\u4f1a\u5bfc\u81f4\u7cfb\u7edf\u8d1f\u8f7d\u8fc7\u9ad8&#xff1b;\u4e14\u7ebf\u7a0b\u7f3a\u4e4f\u7edf\u4e00\u7ba1\u7406&#xff0c;\u65e0\u6cd5\u63a7\u5236\u5e76\u53d1\u6570&#xff0c;\u53ef\u80fd\u5f15\u53d1\u8d44\u6e90\u8017\u5c3d\u95ee\u9898\u3002<\/p>\n<\/li>\n<\/ul>\n<ul>\n<li>\n<p>\u65e0\u5f02\u5e38\u5904\u7406\u673a\u5236&#xff1a;run()\u65b9\u6cd5\u4e0d\u5141\u8bb8\u629b\u51fachecked\u5f02\u5e38&#xff0c;\u53ea\u80fd\u5728\u65b9\u6cd5\u5185\u90e8\u6355\u83b7\u5904\u7406&#xff1b;\u82e5\u53d1\u751f\u672a\u6355\u83b7\u5f02\u5e38&#xff0c;\u7ebf\u7a0b\u4f1a\u76f4\u63a5\u7ec8\u6b62&#xff0c;\u4e3b\u7ebf\u7a0b\u65e0\u6cd5\u611f\u77e5\u3002<\/p>\n<\/li>\n<\/ul>\n<h3>2\u3001Future\u7684\u6838\u5fc3\u80fd\u529b\u4e0e\u4e0d\u8db3<\/h3>\n<p>\u4e3a\u4e86\u89e3\u51b3Runnable\u65e0\u6cd5\u83b7\u53d6\u8fd4\u56de\u503c\u7684\u95ee\u9898&#xff0c;Java 5\u5f15\u5165\u4e86Callable\u63a5\u53e3\u548cFuture\u63a5\u53e3\u3002Callable\u63a5\u53e3\u7684call()\u65b9\u6cd5\u652f\u6301\u8fd4\u56de\u503c&#xff0c;\u4e14\u53ef\u4ee5\u629b\u51fa\u5f02\u5e38&#xff1b;Future\u63a5\u53e3\u5219\u7528\u4e8e\u63a5\u6536Callable\u4efb\u52a1\u7684\u6267\u884c\u7ed3\u679c&#xff0c;\u76f8\u5f53\u4e8e\u4e00\u4e2a\u201c\u5f02\u6b65\u4efb\u52a1\u7684\u7ed3\u679c\u5360\u4f4d\u7b26\u201d\u3002<\/p>\n<p>\u540c\u65f6&#xff0c;Java\u8fd8\u63d0\u4f9b\u4e86ExecutorService\u7ebf\u7a0b\u6c60\u6846\u67b6&#xff0c;\u7528\u4e8e\u7edf\u4e00\u7ba1\u7406\u7ebf\u7a0b\u8d44\u6e90&#xff0c;\u907f\u514d\u9891\u7e41\u521b\u5efa\u7ebf\u7a0b\u7684\u95ee\u9898\u3002\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u7ebf\u7a0b\u6c60\u63d0\u4ea4Callable\u4efb\u52a1&#xff0c;\u83b7\u53d6Future\u5bf9\u8c61&#xff0c;\u518d\u901a\u8fc7Future\u83b7\u53d6\u4efb\u52a1\u7ed3\u679c\u3002<\/p>\n<p>\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b&#xff1a;<\/p>\n<p>import\u00a0java.util.concurrent.*;<br \/>\npublic\u00a0class\u00a0FutureDemo\u00a0{<br \/>\n\u00a0 \u00a0\u00a0\/\/ \u521b\u5efa\u56fa\u5b9a\u5927\u5c0f\u7684\u7ebf\u7a0b\u6c60<br \/>\n\u00a0 \u00a0\u00a0private\u00a0static\u00a0final\u00a0ExecutorService\u00a0executor &#061;\u00a0Executors.newFixedThreadPool(2);<br \/>\n\u00a0 \u00a0\u00a0public\u00a0static\u00a0void\u00a0main(String[] args) {<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0\u00a0\/\/ 1. \u63d0\u4ea4Callable\u4efb\u52a1&#xff0c;\u83b7\u53d6Future\u5bf9\u8c61<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0\u00a0Future&lt;String&gt; future &#061; executor.submit(new\u00a0Callable&lt;String&gt;() {<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0&#064;Override<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0public\u00a0String\u00a0call() throws\u00a0Exception\u00a0{<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0\/\/ \u6a21\u62df\u8017\u65f6\u64cd\u4f5c&#xff1a;\u67e5\u8be2\u7528\u6237\u8ba2\u5355<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0Thread.sleep(1000);<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0return\u00a0&#034;\u7528\u6237\u8ba2\u5355\u6570\u636e&#xff1a;\u8ba2\u5355\u53f7123&#xff0c;\u91d1\u989d100\u5143&#034;;<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 }<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 });<\/p>\n<p>\u00a0 \u00a0 \u00a0 \u00a0\u00a0\/\/ 2. \u4e3b\u7ebf\u7a0b\u6267\u884c\u5176\u4ed6\u64cd\u4f5c<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0\u00a0System.out.println(&#034;\u4e3b\u7ebf\u7a0b\u6267\u884c\u5176\u4ed6\u4efb\u52a1&#8230;&#034;);<\/p>\n<p>\u00a0 \u00a0 \u00a0 \u00a0\u00a0\/\/ 3. \u83b7\u53d6\u5f02\u6b65\u4efb\u52a1\u7ed3\u679c<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0\u00a0try\u00a0{<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0\/\/ get()\u65b9\u6cd5\u4f1a\u963b\u585e\u4e3b\u7ebf\u7a0b&#xff0c;\u76f4\u5230\u4efb\u52a1\u5b8c\u6210<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0String\u00a0result &#061; future.get();<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0System.out.println(&#034;\u83b7\u53d6\u5230\u5f02\u6b65\u4efb\u52a1\u7ed3\u679c&#xff1a;&#034;\u00a0&#043; result);<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 }\u00a0catch\u00a0(InterruptedException\u00a0e) {<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0\/\/ \u7ebf\u7a0b\u88ab\u4e2d\u65ad\u7684\u5f02\u5e38\u5904\u7406<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 e.printStackTrace();<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 }\u00a0catch\u00a0(ExecutionException\u00a0e) {<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0\/\/ \u5f02\u6b65\u4efb\u52a1\u6267\u884c\u8fc7\u7a0b\u4e2d\u629b\u51fa\u7684\u5f02\u5e38<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 e.printStackTrace();<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 }\u00a0finally\u00a0{<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0\/\/ \u5173\u95ed\u7ebf\u7a0b\u6c60<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 executor.shutdown();<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 }<br \/>\n\u00a0 \u00a0 } <\/p>\n<p>\u8fd0\u884c\u7ed3\u679c&#xff1a;<\/p>\n<p>\u4e3b\u7ebf\u7a0b\u6267\u884c\u5176\u4ed6\u4efb\u52a1&#8230;<br \/>\n\u83b7\u53d6\u5230\u5f02\u6b65\u4efb\u52a1\u7ed3\u679c&#xff1a;\u7528\u6237\u8ba2\u5355\u6570\u636e&#xff1a;\u8ba2\u5355\u53f7123&#xff0c;\u91d1\u989d100\u5143 <\/p>\n<p>\u76f8\u6bd4Thread&#043;Runnable&#xff0c;Future\u7684\u8fdb\u6b65\u662f\u663e\u8457\u7684&#xff1a;\u00a0\u652f\u6301\u83b7\u53d6\u4efb\u52a1\u8fd4\u56de\u503c \u3001 \u53ef\u901a\u8fc7\u7ebf\u7a0b\u6c60\u7ba1\u7406\u7ebf\u7a0b\u8d44\u6e90 \u3001 \u80fd\u6355\u83b7\u5f02\u6b65\u4efb\u52a1\u629b\u51fa\u7684\u5f02\u5e38\u3002\u4f46\u5728\u5b9e\u9645\u590d\u6742\u573a\u666f\u4e2d&#xff0c;Future\u7684\u4e0d\u8db3\u4f9d\u7136\u7a81\u51fa&#xff1a;<\/p>\n<ul>\n<li>\n<p>get()\u65b9\u6cd5\u963b\u585e\u95ee\u9898&#xff1a;\u5982\u679c\u5f02\u6b65\u4efb\u52a1\u8fd8\u672a\u5b8c\u6210&#xff0c;\u8c03\u7528get()\u65b9\u6cd5\u4f1a\u76f4\u63a5\u963b\u585e\u4e3b\u7ebf\u7a0b&#xff0c;\u76f4\u5230\u4efb\u52a1\u5b8c\u6210\u3002\u8fd9\u4f1a\u5bfc\u81f4\u5f02\u6b65\u7f16\u7a0b\u7684\u4f18\u52bf\u5927\u6253\u6298\u6263&#xff0c;\u751a\u81f3\u9000\u5316\u4e3a\u540c\u6b65\u6267\u884c\u3002\u867d\u7136Future\u63d0\u4f9b\u4e86\u5e26\u8d85\u65f6\u53c2\u6570\u7684get(long timeout, TimeUnit unit)\u65b9\u6cd5&#xff0c;\u4f46\u8d85\u65f6\u540e\u4f1a\u629b\u51faTimeoutException&#xff0c;\u9700\u8981\u624b\u52a8\u5904\u7406\u3002<\/p>\n<\/li>\n<\/ul>\n<ul>\n<li>\n<p>\u7f3a\u4e4f\u56de\u8c03\u673a\u5236&#xff1a;Future\u65e0\u6cd5\u6ce8\u518c\u56de\u8c03\u51fd\u6570&#xff0c;\u65e0\u6cd5\u5b9e\u73b0\u201c\u4efb\u52a1\u5b8c\u6210\u540e\u81ea\u52a8\u6267\u884c\u540e\u7eed\u64cd\u4f5c\u201d\u3002\u6bd4\u5982&#xff0c;\u6211\u4eec\u5e0c\u671b\u5728\u8ba2\u5355\u67e5\u8be2\u5b8c\u6210\u540e&#xff0c;\u81ea\u52a8\u6267\u884c\u8ba2\u5355\u91d1\u989d\u7edf\u8ba1&#xff0c;\u53ea\u80fd\u901a\u8fc7\u4e3b\u7ebf\u7a0b\u5faa\u73af\u5224\u65adFuture\u662f\u5426\u5b8c\u6210&#xff08;isDone()\u65b9\u6cd5&#xff09;&#xff0c;\u518d\u624b\u52a8\u89e6\u53d1\u540e\u7eed\u64cd\u4f5c&#xff0c;\u4ee3\u7801\u7e41\u7410\u4e14\u6548\u7387\u4f4e\u3002<\/p>\n<\/li>\n<\/ul>\n<ul>\n<li>\n<p>\u65e0\u6cd5\u7ec4\u5408\u591a\u4e2a\u4efb\u52a1&#xff1a;Future\u4e0d\u652f\u6301\u591a\u4e2a\u5f02\u6b65\u4efb\u52a1\u7684\u4e32\u884c\u3001\u5e76\u884c\u7ec4\u5408\u3002\u6bd4\u5982&#xff0c;\u9700\u8981\u5148\u67e5\u8be2\u7528\u6237\u4fe1\u606f&#xff0c;\u518d\u6839\u636e\u7528\u6237ID\u67e5\u8be2\u8ba2\u5355&#xff0c;\u6700\u540e\u6839\u636e\u8ba2\u5355ID\u67e5\u8be2\u7269\u6d41\u4fe1\u606f&#xff1b;\u6216\u8005\u9700\u8981\u5e76\u884c\u67e5\u8be2\u7528\u6237\u7684\u8ba2\u5355\u548c\u79ef\u5206&#xff0c;\u518d\u805a\u5408\u7ed3\u679c\u3002\u8fd9\u4e9b\u573a\u666f\u7528Future\u5b9e\u73b0\u4f1a\u975e\u5e38\u590d\u6742&#xff0c;\u9700\u8981\u624b\u52a8\u7ba1\u7406\u591a\u4e2aFuture\u7684\u6267\u884c\u987a\u5e8f\u548c\u7ed3\u679c\u3002<\/p>\n<\/li>\n<\/ul>\n<ul>\n<li>\n<p>\u5f02\u5e38\u5904\u7406\u4e0d\u76f4\u89c2&#xff1a;\u5f02\u6b65\u4efb\u52a1\u629b\u51fa\u7684\u5f02\u5e38\u4f1a\u88ab\u5c01\u88c5\u5728ExecutionException\u4e2d&#xff0c;\u9700\u8981\u901a\u8fc7e.getCause()\u624d\u80fd\u83b7\u53d6\u539f\u59cb\u5f02\u5e38&#xff0c;\u6392\u67e5\u95ee\u9898\u6210\u672c\u9ad8\u3002<\/p>\n<\/li>\n<\/ul>\n<h3>3\u3001\u5b9e\u9645\u5f00\u53d1\u573a\u666f\u7684\u75db\u70b9\u793a\u4f8b<\/h3>\n<p>\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u7535\u5546\u9996\u9875\u7684\u9700\u6c42&#xff1a;\u9700\u8981\u540c\u65f6\u67e5\u8be2\u7528\u6237\u7684\u57fa\u7840\u4fe1\u606f\u3001\u5f85\u652f\u4ed8\u8ba2\u5355\u3001\u4f1a\u5458\u79ef\u5206&#xff0c;\u7136\u540e\u5c06\u8fd9\u4e09\u90e8\u5206\u6570\u636e\u805a\u5408\u540e\u8fd4\u56de\u7ed9\u524d\u7aef\u3002\u5982\u679c\u7528Future\u5b9e\u73b0&#xff0c;\u4ee3\u7801\u4f1a\u662f\u8fd9\u6837\u7684&#xff1a;<\/p>\n<p>import\u00a0java.util.concurrent.*;<br \/>\npublic\u00a0class\u00a0FutureMultiTaskDemo\u00a0{<br \/>\n\u00a0 \u00a0\u00a0private\u00a0static\u00a0final\u00a0ExecutorService\u00a0executor &#061;\u00a0Executors.newFixedThreadPool(3);<br \/>\n\u00a0 \u00a0\u00a0\/\/ \u6a21\u62df\u67e5\u8be2\u7528\u6237\u57fa\u7840\u4fe1\u606f<br \/>\n\u00a0 \u00a0\u00a0public\u00a0static\u00a0Future&lt;String&gt;\u00a0queryUserInfo(String\u00a0userId) {<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0\u00a0return\u00a0executor.submit(() -&gt; {<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0Thread.sleep(800);<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0return\u00a0&#034;\u7528\u6237\u4fe1\u606f&#xff1a;userId&#061;&#034;\u00a0&#043; userId &#043;\u00a0&#034;, \u59d3\u540d&#061;\u5f20\u4e09&#034;;<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 });<br \/>\n\u00a0 \u00a0 }<br \/>\n\u00a0 \u00a0\u00a0\/\/ \u6a21\u62df\u67e5\u8be2\u5f85\u652f\u4ed8\u8ba2\u5355<br \/>\n\u00a0 \u00a0\u00a0public\u00a0static\u00a0Future&lt;String&gt;\u00a0queryPendingOrders(String\u00a0userId) {<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0\u00a0return\u00a0executor.submit(() -&gt; {<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0Thread.sleep(1000);<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0return\u00a0&#034;\u5f85\u652f\u4ed8\u8ba2\u5355&#xff1a;userId&#061;&#034;\u00a0&#043; userId &#043;\u00a0&#034;, \u8ba2\u5355\u6570&#061;2&#034;;<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 });<br \/>\n\u00a0 \u00a0 }<br \/>\n\u00a0 \u00a0\u00a0\/\/ \u6a21\u62df\u67e5\u8be2\u4f1a\u5458\u79ef\u5206<br \/>\n\u00a0 \u00a0\u00a0public\u00a0static\u00a0Future&lt;String&gt;\u00a0queryUserPoints(String\u00a0userId) {<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0\u00a0return\u00a0executor.submit(() -&gt; {<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0Thread.sleep(1200);<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0return\u00a0&#034;\u4f1a\u5458\u79ef\u5206&#xff1a;userId&#061;&#034;\u00a0&#043; userId &#043;\u00a0&#034;, \u79ef\u5206&#061;1000&#034;;<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 });<br \/>\n\u00a0 \u00a0 }<br \/>\n\u00a0 \u00a0\u00a0public\u00a0static\u00a0void\u00a0main(String[] args) {<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0\u00a0String\u00a0userId &#061;\u00a0&#034;1001&#034;;<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 long start &#061;\u00a0System.currentTimeMillis();<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0\u00a0\/\/ 1. \u5e76\u884c\u63d0\u4ea4\u4e09\u4e2a\u5f02\u6b65\u4efb\u52a1<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0\u00a0Future&lt;String&gt; userInfoFuture &#061;\u00a0queryUserInfo(userId);<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0\u00a0Future&lt;String&gt; ordersFuture &#061;\u00a0queryPendingOrders(userId);<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0\u00a0Future&lt;String&gt; pointsFuture &#061;\u00a0queryUserPoints(userId);<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0\u00a0\/\/ 2. \u9010\u4e2a\u83b7\u53d6\u7ed3\u679c&#xff08;\u6b64\u5904\u4f1a\u963b\u585e&#xff0c;\u76f4\u5230\u5bf9\u5e94\u4efb\u52a1\u5b8c\u6210&#xff09;<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0\u00a0try\u00a0{<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0String\u00a0userInfo &#061; userInfoFuture.get();<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0String\u00a0orders &#061; ordersFuture.get();<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0String\u00a0points &#061; pointsFuture.get();<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0\/\/ 3. \u805a\u5408\u7ed3\u679c<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0String\u00a0result &#061;\u00a0&#034;\u9996\u9875\u6570\u636e\u805a\u5408&#xff1a;\\\\n&#034;\u00a0&#043; userInfo &#043;\u00a0&#034;\\\\n&#034;\u00a0&#043; orders &#043;\u00a0&#034;\\\\n&#034;\u00a0&#043; points;<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0System.out.println(result);<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0System.out.println(&#034;\u603b\u8017\u65f6&#xff1a;&#034;\u00a0&#043; (System.currentTimeMillis() &#8211; start) &#043;\u00a0&#034;ms&#034;);<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 }\u00a0catch\u00a0(InterruptedException\u00a0|\u00a0ExecutionException\u00a0e) {<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 e.printStackTrace();<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 }\u00a0finally\u00a0{<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 executor.shutdown();<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 }<br \/>\n\u00a0 \u00a0 } <\/p>\n<p>\u8fd0\u884c\u7ed3\u679c&#xff1a;<\/p>\n<p>\u9996\u9875\u6570\u636e\u805a\u5408&#xff1a;<br \/>\n\u7528\u6237\u4fe1\u606f&#xff1a;userId&#061;1001, \u59d3\u540d&#061;\u5f20\u4e09<br \/>\n\u5f85\u652f\u4ed8\u8ba2\u5355&#xff1a;userId&#061;1001, \u8ba2\u5355\u6570&#061;2<br \/>\n\u4f1a\u5458\u79ef\u5206&#xff1a;userId&#061;1001, \u79ef\u5206&#061;1000<br \/>\n\u603b\u8017\u65f6&#xff1a;1205ms <\/p>\n<p>\u4ece\u7ed3\u679c\u6765\u770b&#xff0c;\u4e09\u4e2a\u4efb\u52a1\u5e76\u884c\u6267\u884c&#xff0c;\u603b\u8017\u65f6\u63a5\u8fd1\u6700\u957f\u4efb\u52a1\u7684\u8017\u65f6&#xff08;1200ms&#xff09;&#xff0c;\u6548\u7387\u5c1a\u53ef\u3002\u4f46\u4ee3\u7801\u5b58\u5728\u4e24\u4e2a\u6838\u5fc3\u95ee\u9898&#xff1a;<\/p>\n<ul>\n<li>\n<p>\u83b7\u53d6\u7ed3\u679c\u65f6\u7684\u963b\u585e&#xff1a;\u867d\u7136\u4e09\u4e2a\u4efb\u52a1\u662f\u5e76\u884c\u7684&#xff0c;\u4f46\u8c03\u7528get()\u65b9\u6cd5\u65f6&#xff0c;\u82e5\u5bf9\u5e94\u4efb\u52a1\u672a\u5b8c\u6210&#xff0c;\u4e3b\u7ebf\u7a0b\u4f9d\u7136\u4f1a\u963b\u585e\u3002\u6bd4\u5982&#xff0c;\u82e5userInfoFuture\u5148\u5b8c\u6210&#xff0c;\u8c03\u7528ordersFuture.get()\u65f6&#xff0c;\u4e3b\u7ebf\u7a0b\u4f1a\u963b\u585e\u5230\u8ba2\u5355\u67e5\u8be2\u4efb\u52a1\u5b8c\u6210&#xff0c;\u518d\u8c03\u7528pointsFuture.get()\u65f6&#xff0c;\u53ef\u80fd\u8fd8\u9700\u8981\u7ee7\u7eed\u963b\u585e\u3002<\/p>\n<\/li>\n<\/ul>\n<ul>\n<li>\n<p>\u7f3a\u4e4f\u5f02\u5e38\u964d\u7ea7\u673a\u5236&#xff1a;\u5982\u679c\u5176\u4e2d\u4e00\u4e2a\u4efb\u52a1\u6267\u884c\u5931\u8d25&#xff08;\u6bd4\u5982\u67e5\u8be2\u79ef\u5206\u7684\u63a5\u53e3\u62a5\u9519&#xff09;&#xff0c;\u4f1a\u76f4\u63a5\u629b\u51faExecutionException&#xff0c;\u5bfc\u81f4\u6574\u4e2a\u805a\u5408\u4efb\u52a1\u5931\u8d25\u3002\u4f46\u5b9e\u9645\u4e1a\u52a1\u4e2d&#xff0c;\u6211\u4eec\u53ef\u80fd\u5e0c\u671b\u201c\u79ef\u5206\u67e5\u8be2\u5931\u8d25\u65f6&#xff0c;\u7528\u9ed8\u8ba4\u503c0\u586b\u5145&#xff0c;\u4e0d\u5f71\u54cd\u5176\u4ed6\u6570\u636e\u7684\u5c55\u793a\u201d&#xff0c;\u8fd9\u79cd\u9700\u6c42\u7528Future\u5b9e\u73b0\u4f1a\u975e\u5e38\u7e41\u7410\u3002<\/p>\n<\/li>\n<\/ul>\n<p>\u800c\u8fd9\u4e9b\u95ee\u9898&#xff0c;CompletableFuture\u90fd\u80fd\u7ed9\u51fa\u4f18\u96c5\u7684\u89e3\u51b3\u65b9\u6848\u3002<\/p>\n<\/p>\n<h2>\u4e09\u3001\u6838\u5fc3\u4f18\u52bf<\/h2>\n<p>CompletableFuture\u662fJava 8\u65b0\u589e\u7684\u7c7b&#xff0c;\u5b83\u5b9e\u73b0\u4e86Future\u63a5\u53e3\u548cCompletionStage\u63a5\u53e3\u3002\u5176\u4e2d&#xff0c;Future\u63a5\u53e3\u4fdd\u8bc1\u4e86\u5b83\u5177\u5907\u83b7\u53d6\u5f02\u6b65\u7ed3\u679c\u7684\u57fa\u7840\u80fd\u529b&#xff1b;\u800cCompletionStage\u63a5\u53e3\u5219\u5b9a\u4e49\u4e86\u4efb\u52a1\u5b8c\u6210\u540e\u7684\u540e\u7eed\u5904\u7406\u903b\u8f91&#xff08;\u56de\u8c03\u3001\u7ec4\u5408\u7b49&#xff09;&#xff0c;\u8fd9\u4e5f\u662fCompletableFuture\u7684\u6838\u5fc3\u4ef7\u503c\u6240\u5728\u3002<\/p>\n<p>\u76f8\u6bd4\u4f20\u7edf\u7684Future&#xff0c;CompletableFuture\u7684\u6838\u5fc3\u4f18\u52bf\u4e3b\u8981\u4f53\u73b0\u5728\u4ee5\u4e0b4\u4e2a\u65b9\u9762&#xff1a;<\/p>\n<h3>1\u3001\u975e\u963b\u585e\u83b7\u53d6\u7ed3\u679c&#xff0c;\u652f\u6301\u56de\u8c03\u673a\u5236<\/h3>\n<p>CompletableFuture\u65e0\u9700\u901a\u8fc7\u963b\u585e\u7684get()\u65b9\u6cd5\u83b7\u53d6\u7ed3\u679c&#xff0c;\u800c\u662f\u53ef\u4ee5\u901a\u8fc7\u6ce8\u518c\u56de\u8c03\u51fd\u6570&#xff08;\u5982thenAccept()\u3001thenApply()\u7b49&#xff09;&#xff0c;\u5b9e\u73b0\u201c\u4efb\u52a1\u5b8c\u6210\u540e\u81ea\u52a8\u6267\u884c\u540e\u7eed\u64cd\u4f5c\u201d\u3002\u56de\u8c03\u51fd\u6570\u7684\u6267\u884c\u662f\u5f02\u6b65\u7684&#xff0c;\u4e0d\u4f1a\u963b\u585e\u4e3b\u7ebf\u7a0b&#xff0c;\u771f\u6b63\u53d1\u6325\u4e86\u5f02\u6b65\u7f16\u7a0b\u7684\u4f18\u52bf\u3002<\/p>\n<p>\u6bd4\u5982&#xff0c;\u6211\u4eec\u53ef\u4ee5\u7ed9\u201c\u67e5\u8be2\u8ba2\u5355\u201d\u7684\u4efb\u52a1\u6ce8\u518c\u4e00\u4e2a\u56de\u8c03\u51fd\u6570&#xff0c;\u8ba2\u5355\u67e5\u8be2\u5b8c\u6210\u540e&#xff0c;\u81ea\u52a8\u6267\u884c\u201c\u7edf\u8ba1\u8ba2\u5355\u91d1\u989d\u201d\u7684\u64cd\u4f5c&#xff0c;\u4ee3\u7801\u7b80\u6d01\u4e14\u975e\u963b\u585e\u3002<\/p>\n<\/p>\n<h3>2\u3001\u5f3a\u5927\u7684\u4efb\u52a1\u7ec4\u5408\u80fd\u529b<\/h3>\n<p>CompletableFuture\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684API&#xff0c;\u652f\u6301\u591a\u4e2a\u5f02\u6b65\u4efb\u52a1\u7684\u4e32\u884c\u3001\u5e76\u884c\u3001\u4f9d\u8d56\u7ec4\u5408\u3002\u6bd4\u5982&#xff1a;<\/p>\n<ul>\n<li>\n<p>\u4e32\u884c\u7ec4\u5408&#xff1a;\u4efb\u52a1A\u5b8c\u6210\u540e&#xff0c;\u81ea\u52a8\u6267\u884c\u4efb\u52a1B&#xff08;\u4efb\u52a1B\u4f9d\u8d56\u4efb\u52a1A\u7684\u7ed3\u679c&#xff09;\u3002<\/p>\n<\/li>\n<\/ul>\n<ul>\n<li>\n<p>\u5e76\u884c\u7ec4\u5408&#xff1a;\u7b49\u5f85\u591a\u4e2a\u4efb\u52a1\u5168\u90e8\u5b8c\u6210\u540e&#xff0c;\u805a\u5408\u7ed3\u679c&#xff08;\u5982\u524d\u9762\u7684\u9996\u9875\u6570\u636e\u805a\u5408\u573a\u666f&#xff09;&#xff1b;\u6216\u4efb\u610f\u4e00\u4e2a\u4efb\u52a1\u5b8c\u6210\u540e&#xff0c;\u7acb\u5373\u8fd4\u56de\u7ed3\u679c&#xff08;\u5982\u8c03\u7528\u591a\u4e2a\u652f\u4ed8\u6e20\u9053&#xff0c;\u53d6\u6700\u5feb\u54cd\u5e94\u7684\u4e00\u4e2a&#xff09;\u3002<\/p>\n<\/li>\n<\/ul>\n<ul>\n<li>\n<p>\u4f9d\u8d56\u7ec4\u5408&#xff1a;\u4e24\u4e2a\u4efb\u52a1\u90fd\u5b8c\u6210\u540e&#xff0c;\u6267\u884c\u540e\u7eed\u64cd\u4f5c&#xff08;\u5982\u67e5\u8be2\u7528\u6237\u4fe1\u606f\u548c\u8ba2\u5355\u4fe1\u606f\u90fd\u5b8c\u6210\u540e&#xff0c;\u805a\u5408\u4e3a\u5b8c\u6574\u7528\u6237\u6570\u636e&#xff09;\u3002<\/p>\n<\/li>\n<\/ul>\n<p>\u8fd9\u4e9b\u7ec4\u5408\u80fd\u529b\u65e0\u9700\u624b\u52a8\u7ba1\u7406\u4efb\u52a1\u7684\u6267\u884c\u987a\u5e8f\u548c\u7ed3\u679c&#xff0c;\u6781\u5927\u7b80\u5316\u4e86\u590d\u6742\u5f02\u6b65\u573a\u666f\u7684\u5f00\u53d1\u3002<\/p>\n<\/p>\n<h3>3\u3001\u4e30\u5bcc\u7684\u5f02\u5e38\u5904\u7406\u673a\u5236<\/h3>\n<p>CompletableFuture\u63d0\u4f9b\u4e86\u4e13\u95e8\u7684\u5f02\u5e38\u5904\u7406API&#xff08;\u5982exceptionally()\u3001handle()\u7b49&#xff09;&#xff0c;\u53ef\u4ee5\u7cbe\u51c6\u6355\u83b7\u5f02\u6b65\u4efb\u52a1\u6267\u884c\u8fc7\u7a0b\u4e2d\u7684\u5f02\u5e38&#xff0c;\u5e76\u8fdb\u884c\u964d\u7ea7\u5904\u7406&#xff08;\u5982\u8fd4\u56de\u9ed8\u8ba4\u503c\u3001\u91cd\u8bd5\u4efb\u52a1\u7b49&#xff09;\u3002\u76f8\u6bd4Future\u9700\u8981\u901a\u8fc7get()\u65b9\u6cd5\u6355\u83b7ExecutionException\u7684\u65b9\u5f0f&#xff0c;CompletableFuture\u7684\u5f02\u5e38\u5904\u7406\u66f4\u76f4\u89c2\u3001\u66f4\u7075\u6d3b\u3002<\/p>\n<p>\u6bd4\u5982&#xff0c;\u524d\u9762\u7684\u79ef\u5206\u67e5\u8be2\u4efb\u52a1\u5931\u8d25\u65f6&#xff0c;\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7exceptionally()\u65b9\u6cd5\u76f4\u63a5\u8fd4\u56de\u9ed8\u8ba4\u79ef\u52060&#xff0c;\u4e0d\u5f71\u54cd\u5176\u4ed6\u4efb\u52a1\u7684\u7ed3\u679c\u805a\u5408\u3002<\/p>\n<\/p>\n<h3>4\u3001\u652f\u6301\u81ea\u5b9a\u4e49\u7ebf\u7a0b\u6c60<\/h3>\n<p>CompletableFuture\u7684\u6240\u6709\u5f02\u6b65\u65b9\u6cd5&#xff08;\u5982supplyAsync()\u3001runAsync()&#xff09;\u90fd\u652f\u6301\u4f20\u5165\u81ea\u5b9a\u4e49\u7684Executor&#xff08;\u7ebf\u7a0b\u6c60&#xff09;&#xff0c;\u53ef\u4ee5\u6839\u636e\u4e1a\u52a1\u9700\u6c42\u7075\u6d3b\u63a7\u5236\u7ebf\u7a0b\u8d44\u6e90\u3002\u800c\u4f20\u7edf\u7684Future\u867d\u7136\u4e5f\u80fd\u901a\u8fc7\u7ebf\u7a0b\u6c60\u63d0\u4ea4\u4efb\u52a1&#xff0c;\u4f46CompletableFuture\u5c06\u7ebf\u7a0b\u6c60\u7684\u914d\u7f6e\u4e0e\u4efb\u52a1\u7684\u5b9a\u4e49\u66f4\u7d27\u5bc6\u5730\u7ed3\u5408\u8d77\u6765&#xff0c;\u4f7f\u7528\u66f4\u4fbf\u6377\u3002<\/p>\n<p>\u540c\u65f6&#xff0c;\u8fd9\u4e5f\u907f\u514d\u4e86\u4f7f\u7528\u9ed8\u8ba4\u7ebf\u7a0b\u6c60&#xff08;ForkJoinPool.commonPool()&#xff09;\u53ef\u80fd\u5e26\u6765\u7684\u8d44\u6e90\u7ade\u4e89\u95ee\u9898&#xff08;\u540e\u7eed\u7bc7\u7ae0\u4f1a\u8be6\u7ec6\u8bb2\u89e3&#xff09;\u3002<\/p>\n<\/p>\n<h2>\u56db\u3001\u57fa\u7840\u4f7f\u7528\u5165\u95e8<\/h2>\n<p>\u4e86\u89e3\u4e86CompletableFuture\u7684\u6838\u5fc3\u4f18\u52bf\u540e&#xff0c;\u6211\u4eec\u4ece\u6700\u57fa\u7840\u7684\u4f7f\u7528\u65b9\u6cd5\u5f00\u59cb\u5b66\u4e60\u3002CompletableFuture\u7684\u6838\u5fc3\u6784\u9020\u65b9\u6cd5\u548c\u57fa\u7840API\u5e76\u4e0d\u591a&#xff0c;\u638c\u63e1\u8fd9\u90e8\u5206\u5185\u5bb9&#xff0c;\u5c31\u80fd\u5e94\u5bf9\u7b80\u5355\u7684\u5f02\u6b65\u573a\u666f\u3002<\/p>\n<h3>1\u3001\u6838\u5fc3\u6784\u9020\u65b9\u6cd5<\/h3>\n<p>CompletableFuture\u63d0\u4f9b\u4e86\u4e24\u4e2a\u6700\u5e38\u7528\u7684\u9759\u6001\u65b9\u6cd5\u6765\u521b\u5efa\u5f02\u6b65\u4efb\u52a1&#xff1a;supplyAsync()\u548crunAsync()&#xff0c;\u5b83\u4eec\u7684\u6838\u5fc3\u533a\u522b\u5728\u4e8e\u662f\u5426\u6709\u8fd4\u56de\u503c\u3002<\/p>\n<p>1.supplyAsync()&#xff1a;\u521b\u5efa\u6709\u8fd4\u56de\u503c\u7684\u5f02\u6b65\u4efb\u52a1<\/p>\n<p>supplyAsync()\u63a5\u6536\u4e00\u4e2aSupplier\u51fd\u6570\u5f0f\u63a5\u53e3\u53c2\u6570&#xff08;\u65e0\u53c2\u6570&#xff0c;\u8fd4\u56deT\u7c7b\u578b\u7ed3\u679c&#xff09;&#xff0c;\u7528\u4e8e\u5b9a\u4e49\u6709\u8fd4\u56de\u503c\u7684\u5f02\u6b65\u4efb\u52a1\u3002\u5b83\u6709\u4e24\u4e2a\u91cd\u8f7d\u65b9\u6cd5&#xff1a;<\/p>\n<p>\/\/ 1. \u4e0d\u6307\u5b9a\u7ebf\u7a0b\u6c60&#xff0c;\u4f7f\u7528\u9ed8\u8ba4\u7ebf\u7a0b\u6c60&#xff08;ForkJoinPool.commonPool()&#xff09;<br \/>\npublic\u00a0static\u00a0&lt;U&gt;\u00a0CompletableFuture&lt;U&gt; supplyAsync(Supplier&lt;U&gt; supplier)<br \/>\n\/\/ 2. \u6307\u5b9a\u81ea\u5b9a\u4e49\u7ebf\u7a0b\u6c60<br \/>\npublic\u00a0static\u00a0&lt;U&gt;\u00a0CompletableFuture&lt;U&gt; supplyAsync(Supplier&lt;U&gt; supplier,\u00a0Executor\u00a0executor) <\/p>\n<p>\u793a\u4f8b\u4ee3\u7801&#xff1a;\u7528supplyAsync()\u5f02\u6b65\u67e5\u8be2\u7528\u6237\u8ba2\u5355&#xff0c;\u5e76\u83b7\u53d6\u8fd4\u56de\u503c<\/p>\n<p>import\u00a0java.util.concurrent.CompletableFuture;<br \/>\nimport\u00a0java.util.concurrent.ExecutorService;<br \/>\nimport\u00a0java.util.concurrent.Executors;<br \/>\npublic\u00a0class\u00a0CompletableFutureSupplyAsyncDemo\u00a0{<br \/>\n\u00a0 \u00a0\u00a0\/\/ \u81ea\u5b9a\u4e49\u7ebf\u7a0b\u6c60<br \/>\n\u00a0 \u00a0\u00a0private\u00a0static\u00a0final\u00a0ExecutorService\u00a0executor &#061;\u00a0Executors.newFixedThreadPool(2);<br \/>\n\u00a0 \u00a0\u00a0public\u00a0static\u00a0void\u00a0main(String[] args) {<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0\u00a0\/\/ 1. \u521b\u5efa\u6709\u8fd4\u56de\u503c\u7684\u5f02\u6b65\u4efb\u52a1&#xff08;\u6307\u5b9a\u81ea\u5b9a\u4e49\u7ebf\u7a0b\u6c60&#xff09;<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0\u00a0CompletableFuture&lt;String&gt; future &#061;\u00a0CompletableFuture.supplyAsync(() -&gt; {<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0\/\/ \u6a21\u62df\u8017\u65f6\u64cd\u4f5c&#xff1a;\u67e5\u8be2\u7528\u6237\u8ba2\u5355<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0try\u00a0{<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0Thread.sleep(1000);<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 }\u00a0catch\u00a0(InterruptedException\u00a0e) {<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0throw\u00a0new\u00a0RuntimeException(&#034;\u4efb\u52a1\u6267\u884c\u88ab\u4e2d\u65ad&#034;, e);<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 }<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0return\u00a0&#034;\u7528\u6237\u8ba2\u5355\u6570\u636e&#xff1a;\u8ba2\u5355\u53f7123&#xff0c;\u91d1\u989d100\u5143&#034;;<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 }, executor);<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0\u00a0\/\/ 2. \u6ce8\u518c\u56de\u8c03\u51fd\u6570&#xff1a;\u4efb\u52a1\u5b8c\u6210\u540e\u81ea\u52a8\u6267\u884c&#xff08;\u975e\u963b\u585e&#xff09;<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 future.thenAccept(result -&gt; {<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0System.out.println(&#034;\u5f02\u6b65\u4efb\u52a1\u6267\u884c\u5b8c\u6210&#xff0c;\u7ed3\u679c&#xff1a;&#034;\u00a0&#043; result);<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 });<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0\u00a0\/\/ 3. \u4e3b\u7ebf\u7a0b\u7ee7\u7eed\u6267\u884c\u5176\u4ed6\u64cd\u4f5c<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0\u00a0System.out.println(&#034;\u4e3b\u7ebf\u7a0b\u6267\u884c\u5b8c\u6bd5&#xff0c;\u65e0\u9700\u7b49\u5f85\u5f02\u6b65\u4efb\u52a1&#034;);<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0\u00a0\/\/ \u6ce8\u610f&#xff1a;\u4e3b\u7ebf\u7a0b\u4e0d\u80fd\u7acb\u5373\u7ed3\u675f&#xff0c;\u5426\u5219\u81ea\u5b9a\u4e49\u7ebf\u7a0b\u6c60\u4e2d\u7684\u4efb\u52a1\u4f1a\u88ab\u4e2d\u65ad<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0\u00a0try\u00a0{<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0Thread.sleep(2000);<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 }\u00a0catch\u00a0(InterruptedException\u00a0e) {<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 e.printStackTrace();<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 }\u00a0finally\u00a0{<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 executor.shutdown();<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 }<br \/>\n\u00a0 \u00a0 } <\/p>\n<p>\u8fd0\u884c\u7ed3\u679c&#xff1a;<\/p>\n<p>\u4e3b\u7ebf\u7a0b\u6267\u884c\u5b8c\u6bd5&#xff0c;\u65e0\u9700\u7b49\u5f85\u5f02\u6b65\u4efb\u52a1<br \/>\n\u5f02\u6b65\u4efb\u52a1\u6267\u884c\u5b8c\u6210&#xff0c;\u7ed3\u679c&#xff1a;\u7528\u6237\u8ba2\u5355\u6570\u636e&#xff1a;\u8ba2\u5355\u53f7123&#xff0c;\u91d1\u989d100\u5143 <\/p>\n<p>\u4ece\u7ed3\u679c\u53ef\u4ee5\u770b\u51fa&#xff0c;\u4e3b\u7ebf\u7a0b\u65e0\u9700\u7b49\u5f85\u5f02\u6b65\u4efb\u52a1\u5b8c\u6210&#xff0c;\u800c\u662f\u76f4\u63a5\u6267\u884c\u5b8c\u6bd5&#xff1b;\u5f53\u5f02\u6b65\u4efb\u52a1\u5b8c\u6210\u540e&#xff0c;\u56de\u8c03\u51fd\u6570thenAccept()\u4f1a\u81ea\u52a8\u6267\u884c&#xff0c;\u6253\u5370\u4efb\u52a1\u7ed3\u679c\u3002\u8fd9\u5c31\u662f\u975e\u963b\u585e\u56de\u8c03\u7684\u6838\u5fc3\u4f18\u52bf\u3002<\/p>\n<p>2.runAsync()&#xff1a;\u521b\u5efa\u65e0\u8fd4\u56de\u503c\u7684\u5f02\u6b65\u4efb\u52a1<\/p>\n<p>runAsync()\u63a5\u6536\u4e00\u4e2aRunnable\u51fd\u6570\u5f0f\u63a5\u53e3\u53c2\u6570&#xff08;\u65e0\u53c2\u6570&#xff0c;\u65e0\u8fd4\u56de\u503c&#xff09;&#xff0c;\u7528\u4e8e\u5b9a\u4e49\u65e0\u8fd4\u56de\u503c\u7684\u5f02\u6b65\u4efb\u52a1\u3002\u5b83\u4e5f\u6709\u4e24\u4e2a\u91cd\u8f7d\u65b9\u6cd5&#xff1a;<\/p>\n<p>\/\/ 1. \u4f7f\u7528\u9ed8\u8ba4\u7ebf\u7a0b\u6c60<br \/>\npublic\u00a0static\u00a0CompletableFuture&lt;Void&gt;\u00a0runAsync(Runnable runnable)<br \/>\n\/\/ 2. \u6307\u5b9a\u81ea\u5b9a\u4e49\u7ebf\u7a0b\u6c60<br \/>\npublic\u00a0static\u00a0CompletableFuture&lt;Void&gt;\u00a0runAsync(Runnable runnable, Executor executor) <\/p>\n<p>\u793a\u4f8b\u4ee3\u7801&#xff1a;\u7528runAsync()\u5f02\u6b65\u8bb0\u5f55\u7cfb\u7edf\u65e5\u5fd7&#xff08;\u65e0\u8fd4\u56de\u503c&#xff09;<\/p>\n<p>import\u00a0java.util.concurrent.CompletableFuture;<br \/>\nimport\u00a0java.util.concurrent.ExecutorService;<br \/>\nimport\u00a0java.util.concurrent.Executors;<br \/>\npublic\u00a0class\u00a0CompletableFutureRunAsyncDemo\u00a0{<br \/>\n\u00a0 \u00a0\u00a0private\u00a0static\u00a0final\u00a0ExecutorService\u00a0executor &#061;\u00a0Executors.newFixedThreadPool(2);<br \/>\n\u00a0 \u00a0\u00a0public\u00a0static\u00a0void\u00a0main(String[] args) {<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0\u00a0\/\/ 1. \u521b\u5efa\u65e0\u8fd4\u56de\u503c\u7684\u5f02\u6b65\u4efb\u52a1<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0\u00a0CompletableFuture&lt;Void&gt; future &#061;\u00a0CompletableFuture.runAsync(() -&gt; {<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0\/\/ \u6a21\u62df\u8017\u65f6\u64cd\u4f5c&#xff1a;\u8bb0\u5f55\u7cfb\u7edf\u65e5\u5fd7<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0try\u00a0{<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0Thread.sleep(1000);<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 }\u00a0catch\u00a0(InterruptedException\u00a0e) {<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 e.printStackTrace();<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 }<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0System.out.println(&#034;\u5f02\u6b65\u4efb\u52a1\u6267\u884c\u5b8c\u6210&#xff1a;\u7cfb\u7edf\u65e5\u5fd7\u8bb0\u5f55\u6210\u529f&#034;);<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 }, executor);<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0\u00a0\/\/ 2. \u6ce8\u518c\u56de\u8c03\u51fd\u6570&#xff1a;\u4efb\u52a1\u5b8c\u6210\u540e\u6253\u5370\u63d0\u793a<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 future.thenRun(() -&gt; {<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0System.out.println(&#034;\u56de\u8c03\u51fd\u6570\u6267\u884c&#xff1a;\u65e5\u5fd7\u8bb0\u5f55\u4efb\u52a1\u5df2\u7ed3\u675f&#034;);<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 });<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0\u00a0System.out.println(&#034;\u4e3b\u7ebf\u7a0b\u6267\u884c\u5b8c\u6bd5&#034;);<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0\u00a0try\u00a0{<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0Thread.sleep(2000);<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 }\u00a0catch\u00a0(InterruptedException\u00a0e) {<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 e.printStackTrace();<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 }\u00a0finally\u00a0{<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 executor.shutdown();<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 }<br \/>\n\u00a0 \u00a0 } <\/p>\n<p>\u8fd0\u884c\u7ed3\u679c&#xff1a;<\/p>\n<p>\u4e3b\u7ebf\u7a0b\u6267\u884c\u5b8c\u6bd5<br \/>\n\u5f02\u6b65\u4efb\u52a1\u6267\u884c\u5b8c\u6210&#xff1a;\u7cfb\u7edf\u65e5\u5fd7\u8bb0\u5f55\u6210\u529f<br \/>\n\u56de\u8c03\u51fd\u6570\u6267\u884c&#xff1a;\u65e5\u5fd7\u8bb0\u5f55\u4efb\u52a1\u5df2\u7ed3\u675f <\/p>\n<p>runAsync()\u9002\u7528\u4e8e\u4e0d\u9700\u8981\u8fd4\u56de\u7ed3\u679c\u7684\u5f02\u6b65\u573a\u666f&#xff0c;\u5982\u65e5\u5fd7\u8bb0\u5f55\u3001\u6d88\u606f\u63a8\u9001\u7b49\u3002<\/p>\n<\/p>\n<h3>2\u3001\u57fa\u7840\u83b7\u53d6\u7ed3\u679c\u65b9\u6cd5<\/h3>\n<p>\u867d\u7136CompletableFuture\u63a8\u8350\u4f7f\u7528\u56de\u8c03\u51fd\u6570\u83b7\u53d6\u7ed3\u679c&#xff0c;\u4f46\u5728\u67d0\u4e9b\u7279\u6b8a\u573a\u666f\u4e0b&#xff08;\u5982\u9700\u8981\u540c\u6b65\u7b49\u5f85\u7ed3\u679c\u805a\u5408&#xff09;&#xff0c;\u4f9d\u7136\u53ef\u4ee5\u901a\u8fc7join()\u6216get()\u65b9\u6cd5\u4e3b\u52a8\u83b7\u53d6\u4efb\u52a1\u7ed3\u679c\u3002\u8fd9\u4e24\u4e2a\u65b9\u6cd5\u7684\u6838\u5fc3\u533a\u522b\u5728\u4e8e\u5f02\u5e38\u5904\u7406\u3002<\/p>\n<p>1.join()\u65b9\u6cd5<\/p>\n<p>join()\u65b9\u6cd5\u65e0\u8fd4\u56de\u503c&#xff08;\u82e5\u4efb\u52a1\u6709\u8fd4\u56de\u503c&#xff0c;\u8fd4\u56de\u4efb\u52a1\u7ed3\u679c&#xff1b;\u65e0\u8fd4\u56de\u503c\u5219\u8fd4\u56deVoid&#xff09;&#xff0c;\u4e14\u4e0d\u4f1a\u629b\u51fachecked\u5f02\u5e38&#xff08;\u5373\u4e0d\u9700\u8981try-catch\u6355\u83b7&#xff09;\u3002\u82e5\u5f02\u6b65\u4efb\u52a1\u6267\u884c\u5931\u8d25&#xff0c;\u4f1a\u76f4\u63a5\u629b\u51faUncheckedException&#xff08;\u8fd0\u884c\u65f6\u5f02\u5e38&#xff09;\u3002<\/p>\n<p>\u793a\u4f8b\u4ee3\u7801&#xff1a;<\/p>\n<p>import\u00a0java.util.concurrent.CompletableFuture;<br \/>\nimport\u00a0java.util.concurrent.ExecutorService;<br \/>\nimport\u00a0java.util.concurrent.Executors;<br \/>\npublic\u00a0class\u00a0CompletableFutureJoinDemo\u00a0{<br \/>\n\u00a0 \u00a0\u00a0private\u00a0static\u00a0final\u00a0ExecutorService\u00a0executor &#061;\u00a0Executors.newFixedThreadPool(2);<br \/>\n\u00a0 \u00a0\u00a0public\u00a0static\u00a0void\u00a0main(String[] args) {<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0\u00a0CompletableFuture&lt;String&gt; future &#061;\u00a0CompletableFuture.supplyAsync(() -&gt; {<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0if\u00a0(true) {<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0throw\u00a0new\u00a0RuntimeException(&#034;\u4efb\u52a1\u6267\u884c\u5931\u8d25&#034;);<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 }<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0return\u00a0&#034;\u4efb\u52a1\u6267\u884c\u6210\u529f&#034;;<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 }, executor);<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0\u00a0\/\/ \u8c03\u7528join()\u65b9\u6cd5\u83b7\u53d6\u7ed3\u679c&#xff0c;\u65e0\u9700try-catch<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0\u00a0String\u00a0result &#061; future.join();<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0\u00a0System.out.println(result);<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 executor.shutdown();<br \/>\n\u00a0 \u00a0 } <\/p>\n<p>\u8fd0\u884c\u7ed3\u679c&#xff08;\u629b\u51fa\u8fd0\u884c\u65f6\u5f02\u5e38&#xff09;&#xff1a;<\/p>\n<p>import\u00a0java.util.concurrent.CompletableFuture;<br \/>\nimport\u00a0java.util.concurrent.ExecutorService;<br \/>\nimport\u00a0java.util.concurrent.Executors;<br \/>\npublic\u00a0class\u00a0CompletableFutureJoinDemo\u00a0{<br \/>\n\u00a0 \u00a0\u00a0private\u00a0static\u00a0final\u00a0ExecutorService\u00a0executor &#061;\u00a0Executors.newFixedThreadPool(2);<br \/>\n\u00a0 \u00a0\u00a0public\u00a0static\u00a0void\u00a0main(String[] args) {<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0\u00a0CompletableFuture&lt;String&gt; future &#061;\u00a0CompletableFuture.supplyAsync(() -&gt; {<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0if\u00a0(true) {<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0throw\u00a0new\u00a0RuntimeException(&#034;\u4efb\u52a1\u6267\u884c\u5931\u8d25&#034;);<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 }<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0return\u00a0&#034;\u4efb\u52a1\u6267\u884c\u6210\u529f&#034;;<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 }, executor);<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0\u00a0\/\/ \u8c03\u7528join()\u65b9\u6cd5\u83b7\u53d6\u7ed3\u679c&#xff0c;\u65e0\u9700try-catch<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0\u00a0String\u00a0result &#061; future.join();<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0\u00a0System.out.println(result);<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 executor.shutdown();<br \/>\n\u00a0 \u00a0 } <\/p>\n<p>2.get()\u65b9\u6cd5<\/p>\n<p>get()\u65b9\u6cd5\u4e0eFuture\u7684get()\u65b9\u6cd5\u7c7b\u4f3c&#xff0c;\u4f1a\u963b\u585e\u4e3b\u7ebf\u7a0b\u76f4\u5230\u4efb\u52a1\u5b8c\u6210&#xff0c;\u5e76\u8fd4\u56de\u4efb\u52a1\u7ed3\u679c\u3002\u5b83\u4f1a\u629b\u51fa\u4e24\u4e2achecked\u5f02\u5e38&#xff0c;\u5fc5\u987b\u901a\u8fc7try-catch\u6355\u83b7&#xff1a;<\/p>\n<ul>\n<li>\n<p>InterruptedException&#xff1a;\u7ebf\u7a0b\u5728\u963b\u585e\u7b49\u5f85\u8fc7\u7a0b\u4e2d\u88ab\u4e2d\u65ad\u3002<\/p>\n<\/li>\n<li>\n<p>ExecutionException&#xff1a;\u5f02\u6b65\u4efb\u52a1\u6267\u884c\u8fc7\u7a0b\u4e2d\u629b\u51fa\u7684\u5f02\u5e38&#xff08;\u88ab\u5c01\u88c5\u5728ExecutionException\u4e2d&#xff09;\u3002<\/p>\n<\/li>\n<\/ul>\n<p>\u793a\u4f8b\u4ee3\u7801&#xff1a;<\/p>\n<p>import\u00a0java.util.concurrent.CompletableFuture;<br \/>\nimport\u00a0java.util.concurrent.ExecutionException;<br \/>\nimport\u00a0java.util.concurrent.ExecutorService;<br \/>\nimport\u00a0java.util.concurrent.Executors;<br \/>\npublic\u00a0class\u00a0CompletableFutureGetDemo\u00a0{<br \/>\n\u00a0 \u00a0\u00a0private\u00a0static\u00a0final\u00a0ExecutorService\u00a0executor &#061;\u00a0Executors.newFixedThreadPool(2);<br \/>\n\u00a0 \u00a0\u00a0public\u00a0static\u00a0void\u00a0main(String[] args) {<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0\u00a0CompletableFuture&lt;String&gt; future &#061;\u00a0CompletableFuture.supplyAsync(() -&gt; {<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0if\u00a0(true) {<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0throw\u00a0new\u00a0RuntimeException(&#034;\u4efb\u52a1\u6267\u884c\u5931\u8d25&#034;);<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 }<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0return\u00a0&#034;\u4efb\u52a1\u6267\u884c\u6210\u529f&#034;;<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 }, executor);<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0\u00a0try\u00a0{<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0\/\/ \u5fc5\u987b\u6355\u83b7checked\u5f02\u5e38<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0String\u00a0result &#061; future.get();<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0System.out.println(result);<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 }\u00a0catch\u00a0(InterruptedException\u00a0e) {<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0System.out.println(&#034;\u7ebf\u7a0b\u88ab\u4e2d\u65ad&#034;);<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 e.printStackTrace();<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 }\u00a0catch\u00a0(ExecutionException\u00a0e) {<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0System.out.println(&#034;\u5f02\u6b65\u4efb\u52a1\u6267\u884c\u5931\u8d25&#xff0c;\u539f\u59cb\u5f02\u5e38&#xff1a;&#034;\u00a0&#043; e.getCause().getMessage());<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 e.printStackTrace();<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 }\u00a0finally\u00a0{<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 executor.shutdown();<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 }<br \/>\n\u00a0 \u00a0 } <\/p>\n<p>\u8fd0\u884c\u7ed3\u679c&#xff1a;<\/p>\n<p>\u5f02\u6b65\u4efb\u52a1\u6267\u884c\u5931\u8d25&#xff0c;\u539f\u59cb\u5f02\u5e38&#xff1a;\u4efb\u52a1\u6267\u884c\u5931\u8d25<br \/>\njava.util.concurrent.ExecutionException: java.lang.RuntimeException: \u4efb\u52a1\u6267\u884c\u5931\u8d25<br \/>\n\u00a0 \u00a0 at java.util.concurrent.CompletableFuture.reportGet(CompletableFuture.java:357)<br \/>\n\u00a0 \u00a0 at java.util.concurrent.CompletableFuture.get(CompletableFuture.java:1895)<br \/>\n\u00a0 \u00a0 at com.example.CompletableFutureGetDemo.main(CompletableFutureGetDemo.java:15)<br \/>\nCaused by: java.lang.RuntimeException: \u4efb\u52a1\u6267\u884c\u5931\u8d25<br \/>\n\u00a0 \u00a0 at com.example.CompletableFutureGetDemo.lambda$main$0(CompletableFutureGetDemo.java:11)<br \/>\n\u00a0 \u00a0 at java.util.concurrent.CompletableFuture$AsyncSupply.run(CompletableFuture.java:1590)<br \/>\n\u00a0 \u00a0 at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)<br \/>\n\u00a0 \u00a0 at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)<br \/>\n\u00a0 \u00a0 at java.lang.Thread.run(Thread.java:748) <\/p>\n<p>join()\u4e0eget()\u7684\u6838\u5fc3\u533a\u522b\u603b\u7ed3<\/p>\n<table>\n<tr>\n<p>\u7279\u6027<\/p>\n<p>join()<\/p>\n<p>get()<\/p>\n<\/tr>\n<tbody>\n<tr>\n<p>\u5f02\u5e38\u5904\u7406<\/p>\n<td style=\"width:273px\">\n<p>\u629b\u51faUncheckedException&#xff08;\u8fd0\u884c\u65f6\u5f02\u5e38&#xff09;&#xff0c;\u65e0\u9700try-catch<\/p>\n<\/td>\n<td>\n<p>\u629b\u51fachecked\u5f02\u5e38&#xff08;InterruptedException\u3001ExecutionException&#xff09;&#xff0c;\u5fc5\u987btry-catch<\/p>\n<\/td>\n<\/tr>\n<tr>\n<p>\u8fd4\u56de\u503c<\/p>\n<td style=\"width:273px\">\n<p>\u8fd4\u56de\u4efb\u52a1\u7ed3\u679c&#xff08;\u6216Void&#xff09;<\/p>\n<\/td>\n<td>\n<p>\u8fd4\u56de\u4efb\u52a1\u7ed3\u679c&#xff08;\u6216Void&#xff09;<\/p>\n<\/td>\n<\/tr>\n<tr>\n<p>\u963b\u585e\u6027<\/p>\n<td style=\"width:273px\">\n<p>\u963b\u585e\u4e3b\u7ebf\u7a0b\u76f4\u5230\u4efb\u52a1\u5b8c\u6210<\/p>\n<\/td>\n<td>\n<p>\u963b\u585e\u4e3b\u7ebf\u7a0b\u76f4\u5230\u4efb\u52a1\u5b8c\u6210<\/p>\n<\/td>\n<\/tr>\n<tr>\n<p>\u9002\u7528\u573a\u666f<\/p>\n<td style=\"width:273px\">\n<p>\u4e0d\u9700\u8981\u7cbe\u7ec6\u5904\u7406\u5f02\u5e38\u7684\u573a\u666f&#xff0c;\u6216Lambda\u8868\u8fbe\u5f0f\u4e2d&#xff08;\u907f\u514dtry-catch\u7e41\u7410&#xff09;<\/p>\n<\/td>\n<td>\n<p>\u9700\u8981\u7cbe\u7ec6\u5904\u7406\u5f02\u5e38\u7684\u573a\u666f&#xff0c;\u660e\u786e\u611f\u77e5\u4efb\u52a1\u5931\u8d25\u539f\u56e0<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>3\u3001CompletableFuture\u7684demo<\/h3>\n<p>\u7ed3\u5408\u524d\u9762\u7684\u77e5\u8bc6\u70b9&#xff0c;\u6211\u4eec\u5b9e\u73b0\u4e00\u4e2a\u5b8c\u6574\u7684\u573a\u666f&#xff1a;\u5f02\u6b65\u67e5\u8be2\u7528\u6237\u7684\u57fa\u7840\u4fe1\u606f\u548c\u5f85\u652f\u4ed8\u8ba2\u5355&#xff0c;\u7136\u540e\u805a\u5408\u7ed3\u679c\u8fd4\u56de\u3002\u8fd9\u4e2a\u573a\u666f\u7528\u4f20\u7edfFuture\u5b9e\u73b0\u6bd4\u8f83\u7e41\u7410&#xff0c;\u4f46\u7528CompletableFuture\u7684\u56de\u8c03\u548c\u7ec4\u5408\u80fd\u529b&#xff0c;\u4ee3\u7801\u4f1a\u975e\u5e38\u7b80\u6d01\u3002<\/p>\n<p>import\u00a0java.util.concurrent.CompletableFuture;<br \/>\nimport\u00a0java.util.concurrent.ExecutorService;<br \/>\nimport\u00a0java.util.concurrent.Executors;<br \/>\npublic\u00a0class\u00a0CompletableFutureFirstDemo\u00a0{<br \/>\n\u00a0 \u00a0\u00a0\/\/ \u81ea\u5b9a\u4e49\u7ebf\u7a0b\u6c60&#xff08;IO\u5bc6\u96c6\u578b\u4efb\u52a1&#xff0c;\u6838\u5fc3\u7ebf\u7a0b\u6570\u8bbe\u4e3aCPU\u6838\u5fc3\u6570*2&#xff09;<br \/>\n\u00a0 \u00a0\u00a0private\u00a0static\u00a0final\u00a0ExecutorService\u00a0executor &#061;\u00a0Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors() *\u00a02);<br \/>\n\u00a0 \u00a0\u00a0\/\/ \u6a21\u62df\u67e5\u8be2\u7528\u6237\u57fa\u7840\u4fe1\u606f<br \/>\n\u00a0 \u00a0\u00a0public\u00a0static\u00a0CompletableFuture&lt;String&gt;\u00a0queryUserInfo(String\u00a0userId) {<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0\u00a0return\u00a0CompletableFuture.supplyAsync(() -&gt; {<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0\/\/ \u6a21\u62dfIO\u8017\u65f6\u64cd\u4f5c<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0try\u00a0{<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0Thread.sleep(800);<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 }\u00a0catch\u00a0(InterruptedException\u00a0e) {<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0throw\u00a0new\u00a0RuntimeException(&#034;\u67e5\u8be2\u7528\u6237\u4fe1\u606f\u5931\u8d25&#034;, e);<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 }<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0return\u00a0&#034;\u7528\u6237\u4fe1\u606f&#xff1a;userId&#061;&#034;\u00a0&#043; userId &#043;\u00a0&#034;, \u59d3\u540d&#061;\u5f20\u4e09, \u6027\u522b&#061;\u7537&#034;;<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 }, executor);<br \/>\n\u00a0 \u00a0 }<br \/>\n\u00a0 \u00a0\u00a0\/\/ \u6a21\u62df\u67e5\u8be2\u5f85\u652f\u4ed8\u8ba2\u5355<br \/>\n\u00a0 \u00a0\u00a0public\u00a0static\u00a0CompletableFuture&lt;String&gt;\u00a0queryPendingOrders(String\u00a0userId) {<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0\u00a0return\u00a0CompletableFuture.supplyAsync(() -&gt; {<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0\/\/ \u6a21\u62dfIO\u8017\u65f6\u64cd\u4f5c<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0try\u00a0{<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0Thread.sleep(1000);<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 }\u00a0catch\u00a0(InterruptedException\u00a0e) {<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0throw\u00a0new\u00a0RuntimeException(&#034;\u67e5\u8be2\u5f85\u652f\u4ed8\u8ba2\u5355\u5931\u8d25&#034;, e);<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 }<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0return\u00a0&#034;\u5f85\u652f\u4ed8\u8ba2\u5355&#xff1a;userId&#061;&#034;\u00a0&#043; userId &#043;\u00a0&#034;, \u8ba2\u5355\u53f7&#061;ORDER123, \u91d1\u989d&#061;200\u5143, \u72b6\u6001&#061;\u5f85\u652f\u4ed8&#034;;<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 }, executor);<br \/>\n\u00a0 \u00a0 }<br \/>\n\u00a0 \u00a0\u00a0public\u00a0static\u00a0void\u00a0main(String[] args) {<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0\u00a0String\u00a0userId &#061;\u00a0&#034;1001&#034;;<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 long start &#061;\u00a0System.currentTimeMillis();<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0\u00a0\/\/ 1. \u5e76\u884c\u6267\u884c\u4e24\u4e2a\u5f02\u6b65\u4efb\u52a1<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0\u00a0CompletableFuture&lt;String&gt; userInfoFuture &#061;\u00a0queryUserInfo(userId);<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0\u00a0CompletableFuture&lt;String&gt; ordersFuture &#061;\u00a0queryPendingOrders(userId);<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0\u00a0\/\/ 2. \u4e24\u4e2a\u4efb\u52a1\u90fd\u5b8c\u6210\u540e&#xff0c;\u805a\u5408\u7ed3\u679c&#xff08;thenCombine\u7528\u4e8e\u7ec4\u5408\u4e24\u4e2a\u4efb\u52a1\u7684\u7ed3\u679c&#xff09;<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0\u00a0CompletableFuture&lt;String&gt; combinedFuture &#061; userInfoFuture.thenCombine(ordersFuture, (userInfo, orders) -&gt; {<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0return\u00a0&#034;\u7528\u6237\u5b8c\u6574\u6570\u636e\u805a\u5408&#xff1a;\\\\n&#034;\u00a0&#043; userInfo &#043;\u00a0&#034;\\\\n&#034;\u00a0&#043; orders;<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 });<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0\u00a0\/\/ 3. \u6ce8\u518c\u56de\u8c03\u51fd\u6570&#xff0c;\u83b7\u53d6\u805a\u5408\u7ed3\u679c<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 combinedFuture.thenAccept(combinedResult -&gt; {<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0System.out.println(combinedResult);<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0System.out.println(&#034;\u805a\u5408\u4efb\u52a1\u603b\u8017\u65f6&#xff1a;&#034;\u00a0&#043; (System.currentTimeMillis() &#8211; start) &#043;\u00a0&#034;ms&#034;);<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 });<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0\u00a0\/\/ 4. \u5f02\u5e38\u5904\u7406&#xff1a;\u82e5\u4efb\u610f\u4efb\u52a1\u5931\u8d25&#xff0c;\u6253\u5370\u5f02\u5e38\u4fe1\u606f<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 combinedFuture.exceptionally(ex -&gt; {<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0System.out.println(&#034;\u4efb\u52a1\u6267\u884c\u5931\u8d25&#xff1a;&#034;\u00a0&#043; ex.getCause().getMessage());<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0return\u00a0null;<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 });<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0\u00a0\/\/ \u4e3b\u7ebf\u7a0b\u7ee7\u7eed\u6267\u884c\u5176\u4ed6\u64cd\u4f5c<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0\u00a0System.out.println(&#034;\u4e3b\u7ebf\u7a0b\u6267\u884c\u5b8c\u6bd5&#xff0c;\u7b49\u5f85\u5f02\u6b65\u4efb\u52a1\u5b8c\u6210&#8230;&#034;);<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0\u00a0\/\/ \u9632\u6b62\u4e3b\u7ebf\u7a0b\u63d0\u524d\u7ed3\u675f&#xff0c;\u5bfc\u81f4\u7ebf\u7a0b\u6c60\u88ab\u5173\u95ed<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0\u00a0try\u00a0{<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0Thread.sleep(2000);<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 }\u00a0catch\u00a0(InterruptedException\u00a0e) {<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 e.printStackTrace();<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 }\u00a0finally\u00a0{<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 executor.shutdown();<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 }<br \/>\n\u00a0 \u00a0 } <\/p>\n<p>\u8fd0\u884c\u7ed3\u679c&#xff1a;<\/p>\n<p>\u4e3b\u7ebf\u7a0b\u6267\u884c\u5b8c\u6bd5&#xff0c;\u7b49\u5f85\u5f02\u6b65\u4efb\u52a1\u5b8c\u6210&#8230;<br \/>\n\u7528\u6237\u5b8c\u6574\u6570\u636e\u805a\u5408&#xff1a;<br \/>\n\u7528\u6237\u4fe1\u606f&#xff1a;userId&#061;1001, \u59d3\u540d&#061;\u5f20\u4e09, \u6027\u522b&#061;\u7537<br \/>\n\u5f85\u652f\u4ed8\u8ba2\u5355&#xff1a;userId&#061;1001, \u8ba2\u5355\u53f7&#061;ORDER123, \u91d1\u989d&#061;200\u5143, \u72b6\u6001&#061;\u5f85\u652f\u4ed8<br \/>\n\u805a\u5408\u4efb\u52a1\u603b\u8017\u65f6&#xff1a;1008ms <\/p>\n<p>\u4ece\u7ed3\u679c\u53ef\u4ee5\u770b\u51fa&#xff1a;<\/p>\n<ul>\n<li>\n<p>\u4e24\u4e2a\u4efb\u52a1\u5e76\u884c\u6267\u884c&#xff0c;\u603b\u8017\u65f6\u63a5\u8fd1\u6700\u957f\u4efb\u52a1\u7684\u8017\u65f6&#xff08;1000ms&#xff09;&#xff0c;\u6548\u7387\u5f88\u9ad8\u3002<\/p>\n<\/li>\n<\/ul>\n<ul>\n<li>\n<p>\u901a\u8fc7thenCombine()\u65b9\u6cd5\u8f7b\u677e\u5b9e\u73b0\u4e86\u4e24\u4e2a\u4efb\u52a1\u7ed3\u679c\u7684\u805a\u5408&#xff0c;\u65e0\u9700\u624b\u52a8\u7ba1\u7406\u4efb\u52a1\u5b8c\u6210\u987a\u5e8f\u3002<\/p>\n<\/li>\n<\/ul>\n<ul>\n<li>\n<p>\u901a\u8fc7exceptionally()\u65b9\u6cd5\u5b9e\u73b0\u4e86\u5f02\u5e38\u5904\u7406&#xff0c;\u82e5\u4efb\u610f\u4efb\u52a1\u5931\u8d25&#xff0c;\u4f1a\u6253\u5370\u5931\u8d25\u539f\u56e0\u3002<\/p>\n<\/li>\n<\/ul>\n<ul>\n<li>\n<p>\u4e3b\u7ebf\u7a0b\u65e0\u9700\u963b\u585e\u7b49\u5f85&#xff0c;\u800c\u662f\u7ee7\u7eed\u6267\u884c\u5176\u4ed6\u64cd\u4f5c&#xff0c;\u76f4\u5230\u5f02\u6b65\u4efb\u52a1\u5b8c\u6210\u540e&#xff0c;\u56de\u8c03\u51fd\u6570\u81ea\u52a8\u6267\u884c\u3002<\/p>\n<\/li>\n<\/ul>\n<h2>\u4e94\u3001\u603b\u7ed3<\/h2>\n<p>\u672c\u7bc7\u4e3a\u6211\u4eec\u68b3\u7406\u4e86\u4f20\u7edf\u5f02\u6b65\u7f16\u7a0b\u65b9\u6848&#xff08;Thread&#043;Runnable\u3001Future&#xff09;\u7684\u75db\u70b9&#xff1a;Thread\u65e0\u6cd5\u83b7\u53d6\u8fd4\u56de\u503c&#xff0c;Future\u7684get()\u65b9\u6cd5\u963b\u585e\u3001\u7f3a\u4e4f\u56de\u8c03\u548c\u4efb\u52a1\u7ec4\u5408\u80fd\u529b\u3002\u968f\u540e&#xff0c;\u6211\u4eec\u4ecb\u7ecd\u4e86CompletableFuture\u7684\u6838\u5fc3\u4f18\u52bf&#xff1a;\u975e\u963b\u585e\u56de\u8c03\u3001\u5f3a\u5927\u7684\u4efb\u52a1\u7ec4\u5408\u3001\u4e30\u5bcc\u7684\u5f02\u5e38\u5904\u7406\u3001\u652f\u6301\u81ea\u5b9a\u4e49\u7ebf\u7a0b\u6c60\u3002\u6700\u540e&#xff0c;\u6211\u4eec\u5b66\u4e60\u4e86CompletableFuture\u7684\u57fa\u7840\u4f7f\u7528\u65b9\u6cd5&#xff0c;\u5305\u62ec\u6838\u5fc3\u6784\u9020\u65b9\u6cd5supplyAsync()\/runAsync()\u3001\u7ed3\u679c\u83b7\u53d6\u65b9\u6cd5join()\/get()&#xff0c;\u5e76\u901a\u8fc7\u4e00\u4e2a\u5b8c\u6574\u7684\u793a\u4f8b\u5c55\u793a\u4e86\u5b83\u7684\u4f18\u52bf\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u3001\u524d\u8a00<br \/>\nJava\u5f02\u6b65\u7f16\u7a0b\u4eceThread\u3001Runnable\u4e00\u8def\u6f14\u8fdb\u5230Future&#xff0c;\u4f46\u4f20\u7edf\u65b9\u6848\u59cb\u7ec8\u5b58\u5728\u96be\u4ee5\u89e3\u51b3\u7684\u75db\u70b9\u3002\u800cCompletableFuture\u7684\u51fa\u73b0&#xff0c;\u8ba9Java\u5f02\u6b65\u7f16\u7a0b\u8fdb\u5165\u4e86\u66f4\u7075\u6d3b\u3001\u66f4\u9ad8\u6548\u7684\u9636\u6bb5\u3002\u672c\u7bc7\u4f5c\u4e3a\u7cfb\u5217\u7684\u5165\u95e8\u7bc7&#xff0c;\u5c06\u5148\u5e26\u5927\u5bb6\u7406\u6e05\u4f20\u7edf\u5f02\u6b65\u7f16\u7a0b\u7684\u5c40\u9650&#xff0c;\u518d\u6df1\u5165\u8bb2\u89e3CompletableFuture\u7684\u6838\u5fc3\u4f18\u52bf\u4e0e\u57fa\u7840\u4f7f\u7528\u65b9\u6cd5&#xff0c;\u5e2e\u4f60\u5feb\u901f\u5efa\u7acb\u5bf9CompletableFuture\u7684\u8ba4\u77e5\u3002 \u4e8c\u3001\u4f20\u7edf\u5f02\u6b65\u7f16\u7a0b\u7684<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[8807,8808,99,787,586,1637],"topic":[],"class_list":["post-79197","post","type-post","status-publish","format-standard","hentry","category-server","tag-completable","tag-future","tag-java","tag-787","tag-586","tag-1637"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>CompletableFuture\u4e3a\u4f55\u80fd\u66ff\u4ee3Future - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/79197.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"CompletableFuture\u4e3a\u4f55\u80fd\u66ff\u4ee3Future - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u4e00\u3001\u524d\u8a00 Java\u5f02\u6b65\u7f16\u7a0b\u4eceThread\u3001Runnable\u4e00\u8def\u6f14\u8fdb\u5230Future&#xff0c;\u4f46\u4f20\u7edf\u65b9\u6848\u59cb\u7ec8\u5b58\u5728\u96be\u4ee5\u89e3\u51b3\u7684\u75db\u70b9\u3002\u800cCompletableFuture\u7684\u51fa\u73b0&#xff0c;\u8ba9Java\u5f02\u6b65\u7f16\u7a0b\u8fdb\u5165\u4e86\u66f4\u7075\u6d3b\u3001\u66f4\u9ad8\u6548\u7684\u9636\u6bb5\u3002\u672c\u7bc7\u4f5c\u4e3a\u7cfb\u5217\u7684\u5165\u95e8\u7bc7&#xff0c;\u5c06\u5148\u5e26\u5927\u5bb6\u7406\u6e05\u4f20\u7edf\u5f02\u6b65\u7f16\u7a0b\u7684\u5c40\u9650&#xff0c;\u518d\u6df1\u5165\u8bb2\u89e3CompletableFuture\u7684\u6838\u5fc3\u4f18\u52bf\u4e0e\u57fa\u7840\u4f7f\u7528\u65b9\u6cd5&#xff0c;\u5e2e\u4f60\u5feb\u901f\u5efa\u7acb\u5bf9CompletableFuture\u7684\u8ba4\u77e5\u3002 \u4e8c\u3001\u4f20\u7edf\u5f02\u6b65\u7f16\u7a0b\u7684\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/79197.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2026-03-01T03:25:23+00:00\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"8 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/79197.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/79197.html\",\"name\":\"CompletableFuture\u4e3a\u4f55\u80fd\u66ff\u4ee3Future - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-03-01T03:25:23+00:00\",\"dateModified\":\"2026-03-01T03:25:23+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/79197.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/79197.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/79197.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"CompletableFuture\u4e3a\u4f55\u80fd\u66ff\u4ee3Future\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"CompletableFuture\u4e3a\u4f55\u80fd\u66ff\u4ee3Future - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/79197.html","og_locale":"zh_CN","og_type":"article","og_title":"CompletableFuture\u4e3a\u4f55\u80fd\u66ff\u4ee3Future - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u4e00\u3001\u524d\u8a00 Java\u5f02\u6b65\u7f16\u7a0b\u4eceThread\u3001Runnable\u4e00\u8def\u6f14\u8fdb\u5230Future&#xff0c;\u4f46\u4f20\u7edf\u65b9\u6848\u59cb\u7ec8\u5b58\u5728\u96be\u4ee5\u89e3\u51b3\u7684\u75db\u70b9\u3002\u800cCompletableFuture\u7684\u51fa\u73b0&#xff0c;\u8ba9Java\u5f02\u6b65\u7f16\u7a0b\u8fdb\u5165\u4e86\u66f4\u7075\u6d3b\u3001\u66f4\u9ad8\u6548\u7684\u9636\u6bb5\u3002\u672c\u7bc7\u4f5c\u4e3a\u7cfb\u5217\u7684\u5165\u95e8\u7bc7&#xff0c;\u5c06\u5148\u5e26\u5927\u5bb6\u7406\u6e05\u4f20\u7edf\u5f02\u6b65\u7f16\u7a0b\u7684\u5c40\u9650&#xff0c;\u518d\u6df1\u5165\u8bb2\u89e3CompletableFuture\u7684\u6838\u5fc3\u4f18\u52bf\u4e0e\u57fa\u7840\u4f7f\u7528\u65b9\u6cd5&#xff0c;\u5e2e\u4f60\u5feb\u901f\u5efa\u7acb\u5bf9CompletableFuture\u7684\u8ba4\u77e5\u3002 \u4e8c\u3001\u4f20\u7edf\u5f02\u6b65\u7f16\u7a0b\u7684","og_url":"https:\/\/www.wsisp.com\/helps\/79197.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-03-01T03:25:23+00:00","author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"8 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/79197.html","url":"https:\/\/www.wsisp.com\/helps\/79197.html","name":"CompletableFuture\u4e3a\u4f55\u80fd\u66ff\u4ee3Future - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-03-01T03:25:23+00:00","dateModified":"2026-03-01T03:25:23+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/79197.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/79197.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/79197.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"CompletableFuture\u4e3a\u4f55\u80fd\u66ff\u4ee3Future"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/79197","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=79197"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/79197\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=79197"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=79197"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=79197"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=79197"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}