{"id":509,"date":"2025-06-18T17:35:48","date_gmt":"2025-06-18T09:35:48","guid":{"rendered":"https:\/\/trantor.ink\/?p=509"},"modified":"2025-06-18T17:35:49","modified_gmt":"2025-06-18T09:35:49","slug":"java-%e4%b8%ad%e7%9a%84-completablefuture","status":"publish","type":"post","link":"https:\/\/trantor.ink\/?p=509","title":{"rendered":"java \u4e2d\u7684 CompletableFuture"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_81 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/trantor.ink\/?p=509\/#%E5%9F%BA%E6%9C%AC%E7%94%A8%E6%B3%95\" >\u57fa\u672c\u7528\u6cd5<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/trantor.ink\/?p=509\/#%E9%93%BE%E5%BC%8F%E8%B0%83%E7%94%A8\" >\u94fe\u5f0f\u8c03\u7528<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/trantor.ink\/?p=509\/#%E5%BC%82%E5%B8%B8%E5%A4%84%E7%90%86\" >\u5f02\u5e38\u5904\u7406<\/a><\/li><\/ul><\/nav><\/div>\n\n<p class=\"wp-block-paragraph\"><code>CompletableFuture<\/code> \u662f Java 8 \u5f15\u5165\u7684\u65b0\u7c7b\uff0c\u63d0\u4f9b\u4e86\u66f4\u5f3a\u5927\u548c\u7075\u6d3b\u7684\u5f02\u6b65\u7f16\u7a0b\u80fd\u529b\u3002<code>CompletableFuture<\/code> \u53ef\u4ee5\u8ba9\u4f60\u66f4\u8f7b\u677e\u5730\u521b\u5efa\u3001\u7ec4\u5408\u548c\u7ba1\u7406\u5f02\u6b65\u4efb\u52a1\u3002\u4e0b\u9762\u662f\u4e00\u4e9b\u57fa\u672c\u7528\u6cd5\u548c\u793a\u4f8b\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E5%9F%BA%E6%9C%AC%E7%94%A8%E6%B3%95\"><\/span>\u57fa\u672c\u7528\u6cd5<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u521b\u5efa\u4e00\u4e2a\u5f02\u6b65\u4efb\u52a1<\/strong><\/li>\n<\/ol>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import java.util.concurrent.CompletableFuture;\n\npublic class CompletableFutureExample {\n    public static void main(String[] args) {\n        CompletableFuture&lt;Void> future = CompletableFuture.runAsync(() -> {\n            try {\n                \/\/ \u6a21\u62df\u957f\u65f6\u95f4\u7684\u4efb\u52a1\n                Thread.sleep(2000);\n                System.out.println(\"\u4efb\u52a1\u5b8c\u6210!\");\n            } catch (InterruptedException e) {\n                e.printStackTrace();\n            }\n        });\n\n        \/\/ \u7b49\u5f85\u4efb\u52a1\u5b8c\u6210\n        future.join();\n    }\n}<\/pre>\n\n\n\n<ol start=\"2\" class=\"wp-block-list\">\n<li><strong>\u8fd4\u56de\u7ed3\u679c\u7684\u5f02\u6b65\u4efb\u52a1<\/strong><\/li>\n<\/ol>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import java.util.concurrent.CompletableFuture;\n\npublic class CompletableFutureExample {\n    public static void main(String[] args) {\n        CompletableFuture&lt;Integer> future = CompletableFuture.supplyAsync(() -> {\n            try {\n                \/\/ \u6a21\u62df\u957f\u65f6\u95f4\u7684\u4efb\u52a1\n                Thread.sleep(2000);\n                return 123;\n            } catch (InterruptedException e) {\n                e.printStackTrace();\n                return null;\n            }\n        });\n\n        \/\/ \u83b7\u53d6\u7ed3\u679c\n        Integer result = future.join();\n        System.out.println(\"\u8ba1\u7b97\u7ed3\u679c: \" + result);\n    }\n}<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E9%93%BE%E5%BC%8F%E8%B0%83%E7%94%A8\"><\/span>\u94fe\u5f0f\u8c03\u7528<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>thenApply<\/strong>\uff1a\u7528\u4e8e\u5bf9\u7ed3\u679c\u8fdb\u884c\u8f6c\u6362<\/li>\n<\/ol>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import java.util.concurrent.CompletableFuture;\n\npublic class CompletableFutureExample {\n    public static void main(String[] args) {\n        CompletableFuture&lt;Integer> future = CompletableFuture.supplyAsync(() -> {\n            try {\n                Thread.sleep(2000);\n                return 123;\n            } catch (InterruptedException e) {\n                e.printStackTrace();\n                return null;\n            }\n        });\n\n        CompletableFuture&lt;String> future2 = future.thenApply(result -> \"\u7ed3\u679c: \" + result);\n\n        \/\/ \u83b7\u53d6\u8f6c\u6362\u540e\u7684\u7ed3\u679c\n        String result = future2.join();\n        System.out.println(result);\n    }\n}<\/pre>\n\n\n\n<ol start=\"2\" class=\"wp-block-list\">\n<li><strong>thenAccept<\/strong>\uff1a\u7528\u4e8e\u6d88\u8d39\u7ed3\u679c<\/li>\n<\/ol>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import java.util.concurrent.CompletableFuture;\n\npublic class CompletableFutureExample {\n    public static void main(String[] args) {\n        CompletableFuture&lt;Integer> future = CompletableFuture.supplyAsync(() -> {\n            try {\n                Thread.sleep(2000);\n                return 123;\n            } catch (InterruptedException e) {\n                e.printStackTrace();\n                return null;\n            }\n        });\n\n        future.thenAccept(result -> System.out.println(\"\u8ba1\u7b97\u7ed3\u679c: \" + result));\n\n        \/\/ \u7b49\u5f85\u4efb\u52a1\u5b8c\u6210\n        future.join();\n    }\n}<\/pre>\n\n\n\n<ol start=\"3\" class=\"wp-block-list\">\n<li><strong>thenCombine<\/strong>\uff1a\u7528\u4e8e\u7ec4\u5408\u4e24\u4e2a <code>CompletableFuture<\/code><\/li>\n<\/ol>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import java.util.concurrent.CompletableFuture;\n\npublic class CompletableFutureExample {\n    public static void main(String[] args) {\n        CompletableFuture&lt;Integer> future1 = CompletableFuture.supplyAsync(() -> {\n            try {\n                Thread.sleep(2000);\n                return 123;\n            } catch (InterruptedException e) {\n                e.printStackTrace();\n                return null;\n            }\n        });\n\n        CompletableFuture&lt;Integer> future2 = CompletableFuture.supplyAsync(() -> {\n            try {\n                Thread.sleep(3000);\n                return 456;\n            } catch (InterruptedException e) {\n                e.printStackTrace();\n                return null;\n            }\n        });\n\n        CompletableFuture&lt;Integer> combinedFuture = future1.thenCombine(future2, (result1, result2) -> result1 + result2);\n\n        \/\/ \u83b7\u53d6\u7ec4\u5408\u540e\u7684\u7ed3\u679c\n        Integer result = combinedFuture.join();\n        System.out.println(\"\u7ec4\u5408\u7ed3\u679c: \" + result);\n    }\n}<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E5%BC%82%E5%B8%B8%E5%A4%84%E7%90%86\"><\/span>\u5f02\u5e38\u5904\u7406<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>exceptionally<\/strong>\uff1a\u7528\u4e8e\u5904\u7406\u5f02\u5e38<\/li>\n<\/ol>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import java.util.concurrent.CompletableFuture;\n\npublic class CompletableFutureExample {\n    public static void main(String[] args) {\n        CompletableFuture&lt;Integer> future = CompletableFuture.supplyAsync(() -> {\n            try {\n                Thread.sleep(2000);\n                if (true) {\n                    throw new RuntimeException(\"\u8ba1\u7b97\u9519\u8bef!\");\n                }\n                return 123;\n            } catch (InterruptedException e) {\n                e.printStackTrace();\n                return null;\n            }\n        }).exceptionally(ex -> {\n            System.out.println(\"\u5f02\u5e38: \" + ex.getMessage());\n            return -1;\n        });\n\n        \/\/ \u83b7\u53d6\u7ed3\u679c\n        Integer result = future.join();\n        System.out.println(\"\u8ba1\u7b97\u7ed3\u679c: \" + result);\n    }\n}<\/pre>\n\n\n\n<ol start=\"2\" class=\"wp-block-list\">\n<li><strong>handle<\/strong>\uff1a\u7528\u4e8e\u5904\u7406\u7ed3\u679c\u6216\u5f02\u5e38<\/li>\n<\/ol>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import java.util.concurrent.CompletableFuture;\n\npublic class CompletableFutureExample {\n    public static void main(String[] args) {\n        CompletableFuture&lt;Integer> future = CompletableFuture.supplyAsync(() -> {\n            try {\n                Thread.sleep(2000);\n                if (true) {\n                    throw new RuntimeException(\"\u8ba1\u7b97\u9519\u8bef!\");\n                }\n                return 123;\n            } catch (InterruptedException e) {\n                e.printStackTrace();\n                return null;\n            }\n        }).handle((result, ex) -> {\n            if (ex != null) {\n                System.out.println(\"\u5f02\u5e38: \" + ex.getMessage());\n                return -1;\n            }\n            return result;\n        });\n\n        \/\/ \u83b7\u53d6\u7ed3\u679c\n        Integer result = future.join();\n        System.out.println(\"\u8ba1\u7b97\u7ed3\u679c: \" + result);\n    }\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u901a\u8fc7\u4ee5\u4e0a\u793a\u4f8b\uff0c\u4f60\u53ef\u4ee5\u770b\u5230 <code>CompletableFuture<\/code> \u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u529f\u80fd\u6765\u5904\u7406\u5f02\u6b65\u4efb\u52a1\uff0c\u5e76\u4e14\u53ef\u4ee5\u65b9\u4fbf\u5730\u8fdb\u884c\u4efb\u52a1\u7ec4\u5408\u548c\u5f02\u5e38\u5904\u7406\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>CompletableFuture \u662f Java 8 \u5f15\u5165\u7684\u65b0\u7c7b\uff0c\u63d0\u4f9b\u4e86\u66f4\u5f3a\u5927\u548c\u7075\u6d3b\u7684\u5f02\u6b65\u7f16\u7a0b\u80fd\u529b\u3002CompletableFuture \u53ef\u4ee5\u8ba9\u4f60\u66f4\u8f7b\u677e\u5730\u521b\u5efa\u3001\u7ec4\u5408\u548c\u7ba1\u7406\u5f02\u6b65\u4efb\u52a1\u3002\u4e0b\u9762\u662f\u4e00\u4e9b\u57fa\u672c\u7528\u6cd5\u548c\u793a\u4f8b\u3002 \u57fa\u672c\u7528\u6cd5 \u94fe\u5f0f\u8c03\u7528 \u5f02\u5e38\u5904\u7406 \u901a\u8fc7\u4ee5\u4e0a\u793a\u4f8b\uff0c\u4f60\u53ef\u4ee5\u770b\u5230 CompletableFuture \u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u529f\u80fd\u6765\u5904\u7406\u5f02\u6b65\u4efb\u52a1\uff0c\u5e76\u4e14\u53ef\u4ee5\u65b9\u4fbf\u5730\u8fdb\u884c\u4efb\u52a1\u7ec4\u5408\u548c\u5f02\u5e38\u5904\u7406\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1,7],"tags":[39],"class_list":["post-509","post","type-post","status-publish","format-standard","hentry","category-uncategorized","category-appdev","tag-java"],"_links":{"self":[{"href":"https:\/\/trantor.ink\/index.php?rest_route=\/wp\/v2\/posts\/509","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/trantor.ink\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/trantor.ink\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/trantor.ink\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/trantor.ink\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=509"}],"version-history":[{"count":1,"href":"https:\/\/trantor.ink\/index.php?rest_route=\/wp\/v2\/posts\/509\/revisions"}],"predecessor-version":[{"id":510,"href":"https:\/\/trantor.ink\/index.php?rest_route=\/wp\/v2\/posts\/509\/revisions\/510"}],"wp:attachment":[{"href":"https:\/\/trantor.ink\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=509"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/trantor.ink\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=509"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/trantor.ink\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=509"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}