{"id":62999,"date":"2026-01-21T05:25:57","date_gmt":"2026-01-20T21:25:57","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/62999.html"},"modified":"2026-01-21T05:25:57","modified_gmt":"2026-01-20T21:25:57","slug":"completablefuture%e5%ae%9e%e6%88%98%e6%8a%80%e5%b7%a7%e5%a4%a7%e6%8f%ad%e7%a7%98","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/62999.html","title":{"rendered":"CompletableFuture\u5b9e\u6218\u6280\u5de7\u5927\u63ed\u79d8"},"content":{"rendered":"<p>\u4e0b\u9762\u662f\u4e00\u4efd\u5173\u4e8e CompletableFuture \u5b9e\u6218\u5e94\u7528\u7684\u6307\u5357&#xff1a;<\/p>\n<hr \/>\n<h4>\u4e00\u3001\u6838\u5fc3\u5e94\u7528\u573a\u666f<\/h4>\n<h5>1. \u5f02\u6b65\u4efb\u52a1\u94fe<\/h5>\n<p>CompletableFuture.supplyAsync(() -&gt; fetchDataFromDB())  \/\/ \u5f02\u6b65\u67e5\u8be2\u6570\u636e\u5e93<br \/>\n    .thenApply(data -&gt; processData(data))               \/\/ \u540c\u6b65\u5904\u7406\u6570\u636e<br \/>\n    .thenAccept(result -&gt; saveResultToCache(result))    \/\/ \u5f02\u6b65\u4fdd\u5b58\u7ed3\u679c<br \/>\n    .exceptionally(ex -&gt; handleError(ex));              \/\/ \u7edf\u4e00\u5f02\u5e38\u5904\u7406<\/p>\n<h5>2. \u591a\u4efb\u52a1\u5e76\u884c\u805a\u5408<\/h5>\n<p>CompletableFuture&lt;String&gt; futureA &#061; CompletableFuture.supplyAsync(() -&gt; callServiceA());<br \/>\nCompletableFuture&lt;String&gt; futureB &#061; CompletableFuture.supplyAsync(() -&gt; callServiceB());<\/p>\n<p>CompletableFuture.allOf(futureA, futureB)<br \/>\n    .thenApply(v -&gt; combineResults(futureA.join(), futureB.join()))<br \/>\n    .thenAccept(System.out::println);<\/p>\n<h5>3. \u8d85\u65f6\u63a7\u5236<\/h5>\n<p>CompletableFuture.supplyAsync(() -&gt; longRunningTask())<br \/>\n    .completeOnTimeout(&#034;fallback_value&#034;, 2, TimeUnit.SECONDS) \/\/ \u8d85\u65f6\u8fd4\u56de\u9ed8\u8ba4\u503c<br \/>\n    .thenAccept(System.out::println);<\/p>\n<hr \/>\n<h4>\u4e8c\u3001\u5e38\u89c1\u9677\u9631\u4e0e\u89c4\u907f<\/h4>\n<li>\n<p>\u963b\u585e\u4e3b\u7ebf\u7a0b<br \/>\n\u907f\u514d\u5728\u5f02\u6b65\u4efb\u52a1\u4e2d\u8c03\u7528 .join() \u6216 .get() \u963b\u585e\u4e3b\u7ebf\u7a0b&#xff0c;\u4f18\u5148\u4f7f\u7528\u56de\u8c03\u94fe\u3002<\/p>\n<\/li>\n<li>\n<p>\u5f02\u5e38\u672a\u5904\u7406<br \/>\n\u52a1\u5fc5\u901a\u8fc7 .exceptionally() \u6216 .handle() \u6355\u83b7\u5f02\u5e38&#xff1a;<\/p>\n<p>.handle((result, ex) -&gt; ex !&#061; null ? &#034;error&#034; : result)<\/p>\n<\/li>\n<li>\n<p>\u7ebf\u7a0b\u6c60\u6ee5\u7528<br \/>\n\u9ed8\u8ba4\u4f7f\u7528 ForkJoinPool.commonPool()&#xff0c;\u9ad8\u5e76\u53d1\u573a\u666f\u9700\u81ea\u5b9a\u4e49\u7ebf\u7a0b\u6c60&#xff1a;<\/p>\n<p>ExecutorService customPool &#061; Executors.newFixedThreadPool(10);<br \/>\nCompletableFuture.supplyAsync(() -&gt; task(), customPool);<\/p>\n<\/li>\n<hr \/>\n<h4>\u4e09\u3001\u9ad8\u9636\u4f18\u5316\u7b56\u7565<\/h4>\n<h5>1. \u7ec4\u5408\u4f9d\u8d56\u4efb\u52a1<\/h5>\n<p>CompletableFuture&lt;String&gt; futureC &#061; futureA.thenCompose(<br \/>\n    a -&gt; CompletableFuture.supplyAsync(() -&gt; callServiceC(a))<br \/>\n);<\/p>\n<h5>2. \u7ed3\u679c\u9009\u62e9\u5668<\/h5>\n<p>CompletableFuture.supplyAsync(() -&gt; callServiceX())<br \/>\n    .applyToEither(<br \/>\n        CompletableFuture.supplyAsync(() -&gt; callServiceY()),<br \/>\n        firstResult -&gt; firstResult \/\/ \u53d6\u6700\u5148\u54cd\u5e94\u7684\u7ed3\u679c<br \/>\n    );<\/p>\n<h5>3. \u81ea\u5b9a\u4e49\u56de\u8c03\u7ebf\u7a0b<\/h5>\n<p>\u901a\u8fc7 thenApplyAsync \u6307\u5b9a\u540e\u7eed\u64cd\u4f5c\u7ebf\u7a0b\u6c60&#xff1a;<\/p>\n<p>.thenApplyAsync(result -&gt; heavyComputation(result), customPool)<\/p>\n<hr \/>\n<h4>\u56db\u3001\u6027\u80fd\u76d1\u63a7<\/h4>\n<li>\u4f7f\u7528 CompletableFuture \u7684 .minimalCompletionStage() \u83b7\u53d6\u8f7b\u91cf\u7ea7\u89c6\u56fe<\/li>\n<li>\u7ed3\u5408 Micrometer \u76d1\u63a7\u4efb\u52a1\u8017\u65f6&#xff1a;<\/li>\n<p>Metrics.timer(&#034;async_task&#034;).record(() -&gt; future.join());<\/p>\n<hr \/>\n<h4>\u4e94\u3001\u6700\u4f73\u5b9e\u8df5\u603b\u7ed3<\/h4>\n<ul>\n<li>\u7ebf\u7a0b\u6c60\u9694\u79bb&#xff1a;IO\u5bc6\u96c6\u578b\u4e0eCPU\u5bc6\u96c6\u578b\u4efb\u52a1\u4f7f\u7528\u4e0d\u540c\u7ebf\u7a0b\u6c60<\/li>\n<li>\u94fe\u5f0f\u62c6\u89e3&#xff1a;\u5355\u4e00\u804c\u8d23\u7684\u94fe\u5f0f\u8c03\u7528&#xff0c;\u907f\u514d\u8d85\u957f\u56de\u8c03\u94fe<\/li>\n<li>\u9632\u5fa1\u5f0f\u7f16\u7a0b&#xff1a;\u6240\u6709\u5f02\u6b65\u64cd\u4f5c\u5fc5\u987b\u5305\u542b\u8d85\u65f6\u548c\u5f02\u5e38\u5904\u7406<\/li>\n<li>\u8d44\u6e90\u91ca\u653e&#xff1a;\u5728 .whenComplete() \u4e2d\u5173\u95ed\u6570\u636e\u5e93\u8fde\u63a5\u7b49\u8d44\u6e90<\/li>\n<\/ul>\n<hr \/>\n<p>\u901a\u8fc7\u4ee5\u4e0a\u7b56\u7565&#xff0c;\u53ef\u663e\u8457\u63d0\u5347\u9ad8\u5e76\u53d1\u573a\u666f\u4e0b\u7684\u54cd\u5e94\u901f\u5ea6\u548c\u7cfb\u7edf\u7a33\u5b9a\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e0b\u9762\u662f\u4e00\u4efd\u5173\u4e8e CompletableFuture \u5b9e\u6218\u5e94\u7528\u7684\u6307\u5357&#xff1a;\u4e00\u3001\u6838\u5fc3\u5e94\u7528\u573a\u666f1. \u5f02\u6b65\u4efb\u52a1\u94feCompletableFuture.supplyAsync(() -&gt; fetchDataFromDB())  \/\/ \u5f02\u6b65\u67e5\u8be2\u6570\u636e\u5e93.thenApply(data -&gt; processData(data))               \/\/ \u540c\u6b65\u5904\u7406\u6570\u636e.thenAccept(result -&gt; saveResultToCache(result))    \/\/ \u5f02<\/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":[99,323,190],"topic":[],"class_list":["post-62999","post","type-post","status-publish","format-standard","hentry","category-server","tag-java","tag-323","tag-190"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>CompletableFuture\u5b9e\u6218\u6280\u5de7\u5927\u63ed\u79d8 - \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\/62999.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"CompletableFuture\u5b9e\u6218\u6280\u5de7\u5927\u63ed\u79d8 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u4e0b\u9762\u662f\u4e00\u4efd\u5173\u4e8e CompletableFuture \u5b9e\u6218\u5e94\u7528\u7684\u6307\u5357&#xff1a;\u4e00\u3001\u6838\u5fc3\u5e94\u7528\u573a\u666f1. \u5f02\u6b65\u4efb\u52a1\u94feCompletableFuture.supplyAsync(() -&gt; fetchDataFromDB()) \/\/ \u5f02\u6b65\u67e5\u8be2\u6570\u636e\u5e93.thenApply(data -&gt; processData(data))        \/\/ \u540c\u6b65\u5904\u7406\u6570\u636e.thenAccept(result -&gt; saveResultToCache(result))  \/\/ \u5f02\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/62999.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2026-01-20T21:25:57+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=\"1 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/62999.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/62999.html\",\"name\":\"CompletableFuture\u5b9e\u6218\u6280\u5de7\u5927\u63ed\u79d8 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-01-20T21:25:57+00:00\",\"dateModified\":\"2026-01-20T21:25:57+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/62999.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/62999.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/62999.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"CompletableFuture\u5b9e\u6218\u6280\u5de7\u5927\u63ed\u79d8\"}]},{\"@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\u5b9e\u6218\u6280\u5de7\u5927\u63ed\u79d8 - \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\/62999.html","og_locale":"zh_CN","og_type":"article","og_title":"CompletableFuture\u5b9e\u6218\u6280\u5de7\u5927\u63ed\u79d8 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u4e0b\u9762\u662f\u4e00\u4efd\u5173\u4e8e CompletableFuture \u5b9e\u6218\u5e94\u7528\u7684\u6307\u5357&#xff1a;\u4e00\u3001\u6838\u5fc3\u5e94\u7528\u573a\u666f1. \u5f02\u6b65\u4efb\u52a1\u94feCompletableFuture.supplyAsync(() -&gt; fetchDataFromDB()) \/\/ \u5f02\u6b65\u67e5\u8be2\u6570\u636e\u5e93.thenApply(data -&gt; processData(data))        \/\/ \u540c\u6b65\u5904\u7406\u6570\u636e.thenAccept(result -&gt; saveResultToCache(result))  \/\/ \u5f02","og_url":"https:\/\/www.wsisp.com\/helps\/62999.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-01-20T21:25:57+00:00","author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"1 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/62999.html","url":"https:\/\/www.wsisp.com\/helps\/62999.html","name":"CompletableFuture\u5b9e\u6218\u6280\u5de7\u5927\u63ed\u79d8 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-01-20T21:25:57+00:00","dateModified":"2026-01-20T21:25:57+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/62999.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/62999.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/62999.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"CompletableFuture\u5b9e\u6218\u6280\u5de7\u5927\u63ed\u79d8"}]},{"@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\/62999","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=62999"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/62999\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=62999"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=62999"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=62999"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=62999"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}