{"id":215,"date":"2026-07-19T01:00:42","date_gmt":"2026-07-18T17:00:42","guid":{"rendered":"https:\/\/info.ect.fyi\/blog\/?p=215"},"modified":"2026-07-19T01:03:52","modified_gmt":"2026-07-18T17:03:52","slug":"%e5%b9%b6%e5%8f%91%e7%bc%96%e7%a8%8b%e5%b8%b8%e8%a7%81%e6%96%b9%e6%b3%95%ef%bc%88%e4%b8%89%ef%bc%89","status":"publish","type":"post","link":"https:\/\/info.ect.fyi\/blog\/?p=215","title":{"rendered":"\u5e76\u53d1\u7f16\u7a0b\u5e38\u89c1\u65b9\u6cd5\uff08\u4e09\uff09"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">\u662f\u7684\uff0c\u8fd9\u4e2a\u8bdd\u9898\u8fd8\u6ca1\u6709\u8bf4\u5b8c\u3002\u5982\u679c\u80fd\u7528\u4e09\u8a00\u4e24\u8bed\u6982\u62ec\uff0c\u5b83\u5c31\u4e0d\u662f\u5e76\u53d1\u7f16\u7a0b\u4e86\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u63a5\u4e0b\u6765\u662f\u4e00\u4e9b\u96be\u4ee5\u6982\u62ec\u7684\u77e5\u8bc6\u70b9\uff0c\u4f46\u4ecd\u7136\u5f88\u6709\u7528\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">CompletableFuture<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Java 8 \u65b0\u52a0\u5165\u7684\u7279\u6027\uff0c\u6bd4\u8d77Future\uff0c\u5b83\u652f\u6301\u94fe\u5f0f\u8c03\u7528\u548c\u56de\u8c03\u51fd\u6570\uff0c\u8ba9\u590d\u6742\u7684\u5f02\u6b65\u64cd\u4f5c\u53d8\u5f97\u5bb9\u6613\u3002\u7136\u800c\uff0c\u56de\u8c03\u51fd\u6570\u7684\u5e95\u5c42\u7f16\u6392\u6bd4\u8f83\u590d\u6742\uff08\u5b83\u5c5e\u4e8eJava\u6807\u51c6\u5e93\u6700\u96be\u770b\u61c2\uff0c\u4e5f\u662f\u6700&#8221;complicated&#8221;\u7684\u4e00\u4e9b\uff09\uff0c\u4f46\u8c03\u7528\u8005\u4e00\u822c\u65e0\u9700\u8003\u8651\u5b83\u4eec\u7684\u5b9e\u73b0\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5b83\u7684\u58f0\u660e\u65b9\u5f0f\u6bd4\u8f83\u72ec\u7acb\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>CompletableFuture&lt;String&gt; future = CompletableFuture.supplyAsync(() -&gt; \"hello\");<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>future.get()<\/code>\u53ef\u4ee5\u7b49\u5f85\u7ed3\u679c\u3002\u8fd9\u548c\u666e\u901a\u7684Future\u4e00\u6837\u4f1a\u963b\u585e\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u7136\u800c\uff0c\u5b83\u7684\u7279\u70b9\u5728\u4e8e\u7075\u6d3b\u7684\u56de\u8c03\u51fd\u6570\u3002\uff08\u53ef\u80fd\u4f1a\u60f3\u5230JavaScript\u7684Promise\uff0c\u4f46CompletableFuture\u80cc\u540e\u5e76\u6ca1\u6709\u4e8b\u4ef6\u5faa\u73af\uff09\u53ef\u4ee5\u50cf\u8fd9\u6837\u4f18\u96c5\u5730\u94fe\u5f0f\u8c03\u7528\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>CompletableFuture.supplyAsync(() -&gt; 1)\n\t.thenApply(i -&gt; i + 1)\n\t.thenApply(i -&gt; 1 * 2);\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u91cc\u601d\u8def\u5c31\u6253\u5f00\u4e86\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u5e38\u7528\u65b9\u6cd5<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><code>CompletableFuture.runAsync()<\/code>\u521b\u5efa\u7684\u4efb\u52a1\u6ca1\u6709\u8fd4\u56de\u503c\uff0c\u4e00\u822c\u4e0d\u505a\u94fe\u5f0f\u8c03\u7528\u3002<code>CompletableFuture.supplyAsync()<\/code>\u6709\u8fd4\u56de\u503c\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p class=\"wp-block-paragraph\"><code>thenApply()<\/code>\u4f20\u5165\u4e00\u4e2a\u51fd\u6570\uff08<code>Function<\/code>\uff09\uff0c\u5728\u8fd0\u884c\u5230\u6b64\u5904\u65f6\u6267\u884c\u51fd\u6570\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>thenAccept()<\/code>\u4f20\u5165\u4e00\u4e2a<code>Customer<\/code>\uff0c\u5b83\u901a\u5e38\u653e\u5728\u8c03\u7528\u94fe\u7684\u6700\u540e\uff0c\u56e0\u4e3a\u6267\u884c\u540e\u4f1a\u8fd4\u56de<code>CompletableFuture&lt;Void&gt;<\/code>\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>thenRun()<\/code>\u53ea\u662f\u8fd0\u884c\u51fd\u6570\uff0c\u6b64\u51fd\u6570\u4e0d\u4f1a\u5f97\u5230\u53c2\u6570\uff0c\u4f46\u4f1a\u5728\u7279\u5b9a\u65f6\u673a\u8fd0\u884c\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>exceptionally()<\/code>\u63a5\u53d7\u4e00\u4e2a\u51fd\u6570\uff0c\u5b83\u6709\u4e00\u4e2a\u53c2\u6570\uff0c\u662f\u5f02\u5e38\u7684<code>Exception<\/code>\u7c7b\u3002\u5b83\u4f1a\u6355\u83b7\u5b83\u4e4b\u524d\u6574\u4e2a\u8c03\u7528\u94fe\u4e2d\u51fa\u73b0\u7684\u5f02\u5e38\uff0c\u9664\u975e\u5b83\u4eec\u5728\u81ea\u5df1\u7684\u51fd\u6570\u91cc\u5df2\u7ecf\u88ab\u5904\u7406\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p class=\"wp-block-paragraph\"><code>CompletableFuture.allOf(CompletableFuture a, b, c...)<\/code>\u53ef\u4ee5\u7ec4\u5408\u51e0\u4e2a\u4efb\u52a1\uff0c\u5b83\u4eec\u662f\u5e76\u53d1\u7684\u3002\u53ea\u6709\u8fd9\u4e9b\u4efb\u52a1\u5168\u90e8\u5b8c\u6210\uff0c\u5b83\u624d\u4f1a\u6267\u884c\u4e0b\u4e00\u6b65\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>CompletableFuture.anyOf(a, b, c)<\/code>\u4e0e\u524d\u8005\u7c7b\u4f3c\uff0c\u4f46\u4efb\u610f\u4efb\u52a1\u5b8c\u6210\u5c31\u4f1a\u6267\u884c\u4e0b\u4e00\u6b65\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p class=\"wp-block-paragraph\">\u6709\u8da3\u7684\u4e8b\u5b9e\uff08\u4e5f\u8bb8\u4f60\u5df2\u7ecf\u77e5\u9053\u4e86\uff09\uff1a<code>CompletableFuture<\/code>\u662f\u4e00\u4e2a Monad \u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">ReadWriteLock<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e00\u4e2a\u8457\u540d\u7684\u89c4\u5f8b\u662f\uff0c\u6709\u591a\u4e2a\u7ebf\u7a0b\u540c\u65f6\u8bfb\u53d6\u67d0\u4e2a\u8d44\u6e90\uff0c\u6216\u8005\u5355\u4e2a\u7ebf\u7a0b\u5199\u5165\u67d0\u4e2a\u8d44\u6e90\u65f6\uff0c\u662f\u7ebf\u7a0b\u5b89\u5168\u7684\uff1b\u4f46\u4e00\u8bfb\u4e00\u5199\u6216\u8005\u6709\u591a\u4e2a\u5199\u5165\u8005\u65f6\uff0c\u5c31\u53ef\u80fd\u51fa\u73b0\u95ee\u9898\u3002Java\u7684<code>ReadWriteLock<\/code>\u5c31\u662f\u4e3a\u4e86\u89e3\u51b3\u8fd9\u79cd\u95ee\u9898\u7684\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u8bfb\u53d6\u8005\u548c\u5199\u5165\u8005\u52a0\u9501\u7684\u65b9\u5f0f\u4e0d\u4e00\u6837\uff0c\u5206\u522b\u662f<code>lock.readLock().lock()<\/code>\u548c<code>lock.writeLock().lock()<\/code>\u3002\u5b83\u4eec\u53ef\u4ee5\u5206\u522b<code>unlock<\/code>\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0e\u4e00\u822c\u7684\u9501\u4e0d\u540c\uff0c\u5b83\u5141\u8bb8\u591a\u4e2a\u8bfb\u53d6\u8005\u540c\u65f6\u5b58\u5728\uff0c\u5728\u8bfb\u591a\u5199\u5c11\u7684\u60c5\u666f\u4e0b\u80fd\u5927\u5e45\u63d0\u5347\u6548\u7387\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Condition<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u7c7b\u4f3c\u4e8eObject\u7684<code>wait<\/code>\/<code>notify<\/code>\u65b9\u6cd5\uff0cCondition\u7528\u4e8e\u8868\u793a\u4e00\u4e2a\u72b6\u6001\uff0c\u5373\u67d0\u4e2a\u8d44\u6e90\u662f\u5426\u5df2\u5b58\u5728\u3002\u5b83\u4eec\u5f88\u9002\u5408\u751f\u4ea7\u8005\/\u6d88\u8d39\u8005\u6a21\u578b\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Condition\u4e00\u822c\u6d3e\u751f\u81eaLock\uff0c\u5b83\u4eec\u603b\u662f\u548c\u67d0\u4e2a\u9501\u7ed1\u5b9a\u3002\u58f0\u660e\u65b9\u5f0f\u7c7b\u4f3c<code>Condition c = lock.newCondition()<\/code>\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Condition\u62e5\u6709<code>await()<\/code>\uff0c<code>signal()<\/code>\uff0c<code>signalAll()<\/code>\u7b49\u65b9\u6cd5\uff0c\u548c\u7ecf\u5178\u7684P\/V\u64cd\u4f5c\u5982\u51fa\u4e00\u8f99\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u662f\u7684\uff0c\u8fd9\u4e2a\u8bdd\u9898\u8fd8\u6ca1\u6709\u8bf4\u5b8c\u3002\u5982\u679c\u80fd\u7528\u4e09\u8a00\u4e24\u8bed\u6982\u62ec\uff0c\u5b83\u5c31\u4e0d\u662f\u5e76\u53d1\u7f16\u7a0b\u4e86\u3002 \u63a5\u4e0b\u6765\u662f\u4e00\u4e9b\u96be\u4ee5\u6982\u62ec\u7684\u77e5\u8bc6\u70b9\uff0c\u4f46\u4ecd\u7136\u5f88\u6709 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12],"tags":[],"class_list":["post-215","post","type-post","status-publish","format-standard","hentry","category-12"],"_links":{"self":[{"href":"https:\/\/info.ect.fyi\/blog\/index.php?rest_route=\/wp\/v2\/posts\/215","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/info.ect.fyi\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/info.ect.fyi\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/info.ect.fyi\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/info.ect.fyi\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=215"}],"version-history":[{"count":6,"href":"https:\/\/info.ect.fyi\/blog\/index.php?rest_route=\/wp\/v2\/posts\/215\/revisions"}],"predecessor-version":[{"id":223,"href":"https:\/\/info.ect.fyi\/blog\/index.php?rest_route=\/wp\/v2\/posts\/215\/revisions\/223"}],"wp:attachment":[{"href":"https:\/\/info.ect.fyi\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=215"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/info.ect.fyi\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=215"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/info.ect.fyi\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=215"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}