{"id":202515,"date":"2025-05-22T10:21:15","date_gmt":"2025-05-22T02:21:15","guid":{"rendered":"https:\/\/server.hk\/cnblog\/202515\/"},"modified":"2025-05-22T10:21:15","modified_gmt":"2025-05-22T02:21:15","slug":"java%e5%87%bd%e6%95%b0%e5%bc%8f%e7%bc%96%e7%a8%8b%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8completablefuture%e5%a4%84%e7%90%86%e5%bc%82%e6%ad%a5%e5%bc%82%e5%b8%b8%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/202515\/","title":{"rendered":"Java\u51fd\u6570\u5f0f\u7f16\u7a0b\u5982\u4f55\u4f7f\u7528CompletableFuture\u5904\u7406\u5f02\u6b65\u5f02\u5e38\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>Java\u51fd\u6570\u5f0f\u7f16\u7a0b\u5982\u4f55\u4f7f\u7528CompletableFuture\u5904\u7406\u5f02\u6b65\u5f02\u5e38\uff1f<\/h1>\n<p><span style=\"color: #222222;, sans-serif;background-color: #FFFFFF\">\u5404\u4f4d\u5c0f\u4f19\u4f34\u4eec\uff0c\u5927\u5bb6\u597d\u5440\uff01\u770b\u770b\u4eca\u5929\u6211\u53c8\u7ed9\u5404\u4f4d\u5e26\u6765\u4e86\u4ec0\u4e48\u6587\u7ae0\uff1f\u672c\u6587\u6807\u9898<span style=\"color: #222222;, sans-serif;background-color: #FFFFFF\">\u662f<\/span><span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aJava\u51fd\u6570\u5f0f\u7f16\u7a0b\u5982\u4f55\u4f7f\u7528CompletableFuture\u5904\u7406\u5f02\u6b65\u5f02\u5e38\uff1f\u300b<\/span>\uff0c\u5f88\u660e\u663e\u662f\u5173\u4e8e<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\u7684\u6587\u7ae0\u54c8\u54c8\u54c8\uff0c\u5176\u4e2d\u5185\u5bb9\u4e3b\u8981\u4f1a\u6d89\u53ca\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\uff0c\u5982\u679c\u80fd\u5e2e\u5230\u4f60\uff0c\u89c9\u5f97\u5f88\u4e0d\u9519\u7684\u8bdd\uff0c\u6b22\u8fce\u5404\u4f4d\u591a\u591a\u70b9\u8bc4\u548c\u5206\u4eab\uff01<\/span><\/p>\n<p>Java \u51fd\u6570\u5f0f\u7f16\u7a0b\u4e2d\uff0cCompletableFuture \u53ef\u5904\u7406\u5f02\u6b65\u5f02\u5e38\u3002\u5b83\u63d0\u4f9b\u7684\u65b9\u5f0f\u5305\u62ec\uff1aexceptionally()\uff1a\u5f02\u5e38\u65f6\u5e94\u7528\u6307\u5b9a\u51fd\u6570\u8f6c\u6362\u5f02\u5e38\u3002handle()\uff1a\u65e0\u8bba\u6210\u529f\u5931\u8d25\u90fd\u4f1a\u8c03\u7528\uff0c\u7528\u4e8e\u5904\u7406\u5f02\u5e38\u6216\u6210\u529f\u7ed3\u679c\u3002whenComplete()\uff1a\u65e0\u8bba\u6210\u529f\u5931\u8d25\u90fd\u4f1a\u8c03\u7528\uff0c\u7528\u4e8e\u6267\u884c\u7279\u5b9a\u64cd\u4f5c\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241026\/1729913133671c612d4bd03.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>Java \u51fd\u6570\u5f0f\u7f16\u7a0b\uff1a\u5904\u7406\u5f02\u6b65\u5f02\u5e38\u7684 CompletableFuture<\/strong><\/p>\n<p><strong>\u5f15\u8a00<\/strong><\/p>\n<p>\u5728 Java \u51fd\u6570\u5f0f\u7f16\u7a0b\u4e2d\uff0cCompletableFuture \u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u5de5\u5177\uff0c\u7528\u4e8e\u5904\u7406\u5f02\u6b65\u4efb\u52a1\u548c\u5f02\u5e38\u3002\u5b83\u63d0\u4f9b\u4e86\u4e00\u4e2a\u7b80\u5355\u7684 API \u6765\u7ec4\u5408\u548c\u5904\u7406\u672a\u6765\u7684\u7ed3\u679c\uff0c\u540c\u65f6\u5904\u7406\u5f02\u5e38\u3002<\/p>\n<p><strong>\u4f7f\u7528 CompletableFuture \u5904\u7406\u5f02\u6b65\u5f02\u5e38<\/strong><\/p>\n<p>CompletableFuture \u63d0\u4f9b\u4e86\u51e0\u4e2a\u65b9\u6cd5\u6765\u5904\u7406\u5f02\u5e38\uff1a<\/p>\n<ul>\n<li><strong>exceptionally()\uff1a<\/strong>\u5f53 CompletableFuture \u5b8c\u6210\u65f6\uff0c\u5982\u679c\u5b83\u4ee5\u5f02\u5e38\u5b8c\u6210\uff0c\u5219\u6b64\u65b9\u6cd5\u5c06\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684 CompletableFuture\uff0c\u8be5 CompletableFuture \u5e94\u7528\u4e86\u7ed9\u5b9a\u7684\u51fd\u6570\u6765\u8f6c\u6362\u5f02\u5e38\u3002<\/li>\n<li><strong>handle()\uff1a<\/strong>\u7c7b\u4f3c\u4e8e exceptionally()\uff0c\u4f46\u5b83\u65e0\u8bba CompletableFuture \u5b8c\u6210\u6210\u529f\u8fd8\u662f\u5931\u8d25\u90fd\u4f1a\u88ab\u8c03\u7528\u3002<\/li>\n<li><strong>whenComplete()\uff1a<\/strong>\u65e0\u8bba CompletableFuture \u5b8c\u6210\u6210\u529f\u8fd8\u662f\u5931\u8d25\uff0c\u6b64\u65b9\u6cd5\u90fd\u4f1a\u88ab\u8c03\u7528\u3002<\/li>\n<\/ul>\n<p><strong>\u5b9e\u6218\u6848\u4f8b<\/strong><\/p>\n<p>\u8003\u8651\u4ee5\u4e0b\u4ee3\u7801\uff0c\u5b83\u521b\u5efa\u4e86\u4e00\u4e2a CompletableFuture \u6765\u5f02\u6b65\u83b7\u53d6\u6587\u4ef6\u7684\u5185\u5bb9\uff1a<\/p>\n<pre>CompletableFuture&lt;String&gt; fileContentFuture = CompletableFuture.supplyAsync(() -&gt; {\n    try {\n        \/\/ \u83b7\u53d6\u6587\u4ef6\u5185\u5bb9\n        return Files.readString(Paths.get(\"\/path\/to\/file\"));\n    } catch (IOException e) {\n        throw new RuntimeException(e);\n    }\n});<\/pre>\n<p><strong>\u5904\u7406\u5f02\u5e38<\/strong><\/p>\n<p>\u6211\u4eec\u73b0\u5728\u53ef\u4ee5\u4f7f\u7528 CompletableFuture \u63d0\u4f9b\u7684\u65b9\u6cd5\u6765\u5904\u7406\u6f5c\u5728\u7684\u5f02\u5e38\uff1a<\/p>\n<pre>\/\/ \u4f7f\u7528 exceptionally() \u5904\u7406\u5f02\u5e38\nfileContentFuture.exceptionally(ex -&gt; {\n    \/\/ \u65e5\u5fd7\u5f02\u5e38\u6216\u5bf9\u5176\u8fdb\u884c\u5904\u7406\n    ex.printStackTrace();\n    return null;\n});\n\n\/\/ \u4f7f\u7528 handle() \u5904\u7406\u6210\u529f\u6216\u5931\u8d25\u7684\u60c5\u51b5\nfileContentFuture.handle((result, ex) -&gt; {\n    if (ex != null) {\n        \/\/ \u5904\u7406\u5f02\u5e38\n        ex.printStackTrace();\n        return null;\n    } else {\n        \/\/ \u5904\u7406\u6210\u529f\u7ed3\u679c\n        return result;\n    }\n});\n\n\/\/ \u4f7f\u7528 whenComplete() \u65e0\u8bba\u6210\u529f\u8fd8\u662f\u5931\u8d25\u90fd\u4f1a\u88ab\u8c03\u7528\nfileContentFuture.whenComplete((result, ex) -&gt; {\n    \/\/ \u5f53 CompletableFuture \u5b8c\u6210\u65f6\u6267\u884c\u6b64\u64cd\u4f5c\n    if (ex != null) {\n        \/\/ \u65e5\u5fd7\u5f02\u5e38\u6216\u5bf9\u5176\u8fdb\u884c\u5904\u7406\n        ex.printStackTrace();\n    } else {\n        \/\/ \u5904\u7406\u6210\u529f\u7ed3\u679c\n        System.out.println(result);\n    }\n});<\/pre>\n<p><strong>\u7ed3\u8bba<\/strong><\/p>\n<p>CompletableFuture \u63d0\u4f9b\u4e86\u4e00\u4e2a\u7b80\u5355\u4e14\u5f3a\u5927\u7684\u673a\u5236\u6765\u5904\u7406 Java \u51fd\u6570\u5f0f\u7f16\u7a0b\u4e2d\u7684\u5f02\u6b65\u5f02\u5e38\u3002\u901a\u8fc7\u4f7f\u7528 exceptionally()\u3001handle() \u548c whenComplete() \u65b9\u6cd5\uff0c\u60a8\u53ef\u4ee5\u8f7b\u677e\u5730\u6355\u83b7\u548c\u5904\u7406\u5f02\u5e38\uff0c\u786e\u4fdd\u60a8\u7684\u5e94\u7528\u7a0b\u5e8f\u5065\u58ee\u4e14\u53ef\u9760\u3002<\/p>\n<p>\u4eca\u5929\u5173\u4e8e\u300aJava\u51fd\u6570\u5f0f\u7f16\u7a0b\u5982\u4f55\u4f7f\u7528CompletableFuture\u5904\u7406\u5f02\u6b65\u5f02\u5e38\uff1f\u300b\u7684\u5185\u5bb9\u4ecb\u7ecd\u5c31\u5230\u6b64\u7ed3\u675f\uff0c\u5982\u679c\u6709\u4ec0\u4e48\u7591\u95ee\u6216\u8005\u5efa\u8bae\uff0c\u53ef\u4ee5\u5728\u516c\u4f17\u53f7\u4e0b\u591a\u591a\u56de\u590d\u4ea4\u6d41\uff1b\u6587\u4e2d\u82e5\u6709\u4e0d\u6b63\u4e4b\u5904\uff0c\u4e5f\u5e0c\u671b\u56de\u590d\u7559\u8a00\u4ee5\u544a\u77e5\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Java\u51fd\u6570\u5f0f\u7f16\u7a0b\u5982\u4f55\u4f7f\u7528Com&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-202515","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/202515","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=202515"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/202515\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=202515"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=202515"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=202515"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}