{"id":202735,"date":"2025-05-22T10:09:35","date_gmt":"2025-05-22T02:09:35","guid":{"rendered":"https:\/\/server.hk\/cnblog\/202735\/"},"modified":"2025-05-22T10:09:35","modified_gmt":"2025-05-22T02:09:35","slug":"java-%e5%87%bd%e6%95%b0%e5%92%8c-java-%e6%96%b9%e6%b3%95%e7%9a%84%e5%bc%82%e6%ad%a5%e5%a4%84%e7%90%86","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/202735\/","title":{"rendered":"Java \u51fd\u6570\u548c Java \u65b9\u6cd5\u7684\u5f02\u6b65\u5904\u7406"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>Java \u51fd\u6570\u548c Java \u65b9\u6cd5\u7684\u5f02\u6b65\u5904\u7406<\/h1>\n<p>\u5927\u5bb6\u597d\uff0c\u6211\u4eec\u53c8\u89c1\u9762\u4e86\u554a~\u672c\u6587<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aJava \u51fd\u6570\u548c Java \u65b9\u6cd5\u7684\u5f02\u6b65\u5904\u7406\u300b<\/span>\u7684\u5185\u5bb9\u4e2d\u5c06\u4f1a\u6d89\u53ca\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u3002\u5982\u679c\u4f60\u6b63\u5728\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\u76f8\u5173\u77e5\u8bc6\uff0c\u6b22\u8fce\u5173\u6ce8\u6211\uff0c\u4ee5\u540e\u4f1a\u7ed9\u5927\u5bb6\u5e26\u6765\u66f4\u591a<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\u76f8\u5173\u6587\u7ae0\uff0c\u5e0c\u671b\u6211\u4eec\u80fd\u4e00\u8d77\u8fdb\u6b65\uff01\u4e0b\u9762\u5c31\u5f00\u59cb\u672c\u6587\u7684\u6b63\u5f0f\u5185\u5bb9~<\/p>\n<p>Java \u4e2d\u5f02\u6b65\u5904\u7406\u5141\u8bb8\u4efb\u52a1\u5728\u540e\u53f0\u6267\u884c\uff0c\u800c\u4e0d\u4f1a\u963b\u585e\u8c03\u7528\u7ebf\u7a0b\uff0c\u5305\u62ec Java \u51fd\u6570\u548c\u65b9\u6cd5\u3002\u51fd\u6570\u548c\u65b9\u6cd5\u53ef\u4ee5\u901a\u8fc7 async \u5173\u952e\u5b57\u58f0\u660e\u4e3a\u5f02\u6b65\u3002\u4e00\u4e2a\u5b9e\u6218\u6848\u4f8b\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528\u5f02\u6b65\u5904\u7406\u5e76\u884c\u5904\u7406\u591a\u4e2a\u6587\u4ef6\u7684\u5b57\u6570\u8ba1\u7b97\uff0c\u4ece\u800c\u663e\u8457\u63d0\u9ad8\u6548\u7387\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241026\/1729937566671cc09ed5fe6.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>Java \u51fd\u6570\u548c\u65b9\u6cd5\u7684\u5f02\u6b65\u5904\u7406<\/strong><\/p>\n<p><strong>\u4ecb\u7ecd<\/strong><\/p>\n<p>\u5728 Java \u4e2d\uff0c\u5f02\u6b65\u5904\u7406\u662f\u4e00\u79cd\u6280\u672f\uff0c\u5b83\u5141\u8bb8\u5728\u540e\u53f0\u6267\u884c\u4efb\u52a1\uff0c\u800c\u65e0\u9700\u963b\u585e\u8c03\u7528\u7ebf\u7a0b\u3002\u8fd9\u5bf9\u4e8e\u9700\u8981\u54cd\u5e94\u7528\u6237\u7684\u4ea4\u4e92\u5f0f\u5e94\u7528\u7a0b\u5e8f\u6216\u5fc5\u987b\u6267\u884c\u65f6\u95f4\u957f\u7684\u64cd\u4f5c\u7684\u4efb\u52a1\u975e\u5e38\u6709\u7528\u3002<\/p>\n<p><strong>Java \u51fd\u6570<\/strong><\/p>\n<p>Java \u51fd\u6570\u662f\u4e00\u79cd\u8f7b\u91cf\u7ea7\u7684\u51fd\u6570\u5f0f\u7f16\u7a0b\u7ed3\u6784\uff0c\u5b83\u53ea\u6709\u4e00\u4e2a\u53c2\u6570\u548c\u4e00\u4e2a\u8fd4\u56de\u7c7b\u578b\u3002\u51fd\u6570\u53ef\u4ee5\u88ab\u58f0\u660e\u4e3a\u5f02\u6b65\uff0c\u8fd9\u610f\u5473\u7740\u5b83\u4eec\u5c06\u5728\u4e00\u4e2a\u5355\u72ec\u7684\u7ebf\u7a0b\u4e2d\u6267\u884c\u3002<\/p>\n<pre>import java.util.concurrent.CompletableFuture;\n\npublic class AsyncFunction {\n\n    public static CompletableFuture&lt;Integer&gt; squareAsync(int x) {\n        return CompletableFuture.supplyAsync(() -&gt; x * x);\n    }\n\n}<\/pre>\n<p><strong>Java \u65b9\u6cd5<\/strong><\/p>\n<p>Java \u65b9\u6cd5\u662f\u7c7b\u4e2d\u7684\u4f20\u7edf\u5757\u72b6\u7ed3\u6784\u3002\u65b9\u6cd5\u53ef\u4ee5\u901a\u8fc7 <code>async<\/code> \u5173\u952e\u5b57\u58f0\u660e\u4e3a\u5f02\u6b65\u3002<\/p>\n<pre>import java.util.concurrent.CompletableFuture;\n\npublic class AsyncMethod {\n\n    public CompletableFuture&lt;Integer&gt; squareAsync(int x) {\n        return CompletableFuture.supplyAsync(() -&gt; x * x);\n    }\n\n}<\/pre>\n<p><strong>\u5b9e\u6218\u6848\u4f8b<\/strong><\/p>\n<p>\u8003\u8651\u4e00\u4e2a\u9700\u8981\u5904\u7406\u5927\u91cf\u6587\u4ef6\u5217\u8868\u5e76\u8ba1\u7b97\u6bcf\u4e2a\u6587\u4ef6\u7684\u5b57\u6570\u7684\u4efb\u52a1\u3002\u4f7f\u7528\u5f02\u6b65\u5904\u7406\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u6587\u4ef6\u5904\u7406\u5206\u6563\u5230\u591a\u4e2a\u7ebf\u7a0b\u4e2d\uff0c\u4ece\u800c\u663e\u8457\u52a0\u5feb\u4efb\u52a1\u7684\u6267\u884c\u901f\u5ea6\u3002<\/p>\n<pre>import java.io.File;\nimport java.io.IOException;\nimport java.nio.file.Files;\nimport java.nio.file.Paths;\nimport java.util.concurrent.CompletableFuture;\nimport java.util.stream.Stream;\n\npublic class WordCountAsync {\n\n    public static void main(String[] args) {\n        File[] files = new File(\"path\/to\/files\").listFiles();\n\n        \/\/ \u521b\u5efa\u4e00\u4e2a CompletableFuture \u6570\u7ec4\uff0c\u7528\u4e8e\u8ddf\u8e2a\u6bcf\u4e2a\u6587\u4ef6\u7684\u5b57\u6570\u8ba1\u7b97\n        CompletableFuture&lt;Long&gt;[] futures = new CompletableFuture[files.length];\n\n        \/\/ \u4e3a\u6bcf\u4e2a\u6587\u4ef6\u521b\u5efa\u4e00\u4e2a\u5f02\u6b65\u4efb\u52a1\n        for (int i = 0; i &lt; files.length; i++) {\n            futures[i] = CompletableFuture.supplyAsync(() -&gt; {\n                try {\n                    return Files.lines(Paths.get(files[i].getPath())).count();\n                } catch (IOException e) {\n                    throw new RuntimeException(e);\n                }\n            });\n        }\n\n        \/\/ \u7b49\u5f85\u6240\u6709\u8ba1\u7b97\u5b8c\u6210\n        CompletableFuture.allOf(futures).join();\n\n        \/\/ \u6253\u5370\u6bcf\u4e2a\u6587\u4ef6\u7684\u5b57\u6570\n        for (int i = 0; i &lt; files.length; i++) {\n            System.out.println(files[i].getName() + \": \" + futures[i].get());\n        }\n    }\n\n}<\/pre>\n<p>\u901a\u8fc7\u4f7f\u7528\u5f02\u6b65\u5904\u7406\uff0c\u6b64\u4efb\u52a1\u53ef\u4ee5\u5728\u5e76\u884c\u7ebf\u7a0b\u4e2d\u6267\u884c\uff0c\u4ece\u800c\u6781\u5927\u5730\u63d0\u9ad8\u4e86\u6548\u7387\u3002<\/p>\n<p>\u5230\u8fd9\u91cc\uff0c\u6211\u4eec\u4e5f\u5c31\u8bb2\u5b8c\u4e86\u300aJava \u51fd\u6570\u548c Java \u65b9\u6cd5\u7684\u5f02\u6b65\u5904\u7406\u300b\u7684\u5185\u5bb9\u4e86\u3002\u4e2a\u4eba\u8ba4\u4e3a\uff0c\u57fa\u7840\u77e5\u8bc6\u7684\u5b66\u4e60\u548c\u5de9\u56fa\uff0c\u662f\u4e3a\u4e86\u66f4\u597d\u7684\u5c06\u5176\u8fd0\u7528\u5230\u9879\u76ee\u4e2d\uff0c\u6b22\u8fce\u5173\u6ce8\u516c\u4f17\u53f7\uff0c\u5e26\u4f60\u4e86\u89e3\u66f4\u591a\u5173\u4e8ejava,\u5f02\u6b65\u5904\u7406\u7684\u77e5\u8bc6\u70b9\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Java \u51fd\u6570\u548c Java \u65b9\u6cd5&#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-202735","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/202735","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=202735"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/202735\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=202735"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=202735"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=202735"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}