{"id":202528,"date":"2025-05-22T10:05:08","date_gmt":"2025-05-22T02:05:08","guid":{"rendered":"https:\/\/server.hk\/cnblog\/202528\/"},"modified":"2025-05-22T10:05:08","modified_gmt":"2025-05-22T02:05:08","slug":"%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8%e5%b9%b6%e8%a1%8c%e6%b5%81%e5%9c%a8-java-%e4%b8%ad%e5%ae%9e%e7%8e%b0%e5%b9%b6%e5%8f%91%e5%87%bd%e6%95%b0%e5%bc%8f%e7%bc%96%e7%a8%8b%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/202528\/","title":{"rendered":"\u5982\u4f55\u4f7f\u7528\u5e76\u884c\u6d41\u5728 Java \u4e2d\u5b9e\u73b0\u5e76\u53d1\u51fd\u6570\u5f0f\u7f16\u7a0b\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>\u5982\u4f55\u4f7f\u7528\u5e76\u884c\u6d41\u5728 Java \u4e2d\u5b9e\u73b0\u5e76\u53d1\u51fd\u6570\u5f0f\u7f16\u7a0b\uff1f<\/h1>\n<p>\u4eca\u5929\u7ed9\u5927\u5bb6\u5e26\u6765\u4e86<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u5982\u4f55\u4f7f\u7528\u5e76\u884c\u6d41\u5728 Java \u4e2d\u5b9e\u73b0\u5e76\u53d1\u51fd\u6570\u5f0f\u7f16\u7a0b\uff1f\u300b<\/span>\uff0c\u5176\u4e2d\u6d89\u53ca\u5230\u7684\u77e5\u8bc6\u70b9\u5305\u62ec<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\uff0c\u65e0\u8bba\u4f60\u662f\u5c0f\u767d\u8fd8\u662f\u8001\u624b\uff0c\u90fd\u9002\u5408\u770b\u4e00\u770b\u54e6~\u6709\u597d\u7684\u5efa\u8bae\u4e5f\u6b22\u8fce\u5927\u5bb6\u5728\u8bc4\u8bba\u7559\u8a00\uff0c\u82e5\u662f\u770b\u5b8c\u6709\u6240\u6536\u83b7\uff0c\u4e5f\u5e0c\u671b\u5927\u5bb6\u80fd\u591a\u591a\u70b9\u8d5e\u652f\u6301\u5440\uff01\u4e00\u8d77\u52a0\u6cb9\u5b66\u4e60~<\/p>\n<p>\u4f7f\u7528\u5e76\u884c\u6d41\u5728 Java \u4e2d\u5b9e\u73b0\u5e76\u53d1\u51fd\u6570\u5f0f\u7f16\u7a0b\uff1a\u4f7f\u7528 parallel() \u521b\u5efa\u5e76\u884c\u6d41\u3002\u4f7f\u7528 map()\u3001filter() \u548c reduce() \u7b49\u65b9\u6cd5\u5e76\u884c\u8f6c\u6362\u5143\u7d20\u3002\u5e76\u884c\u6d41\u53ef\u80fd\u63d0\u9ad8\u6027\u80fd\uff0c\u4f46\u5e76\u975e\u5728\u6240\u6709\u60c5\u51b5\u4e0b\u90fd\u6bd4\u987a\u5e8f\u6d41\u5feb\uff0c\u4e14\u53ef\u80fd\u5bfc\u81f4\u975e\u786e\u5b9a\u6027\u7ed3\u679c\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241026\/1729915229671c695d0c3a5.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>\u5982\u4f55\u4f7f\u7528\u5e76\u884c\u6d41\u5728 Java \u4e2d\u5b9e\u73b0\u5e76\u53d1\u51fd\u6570\u5f0f\u7f16\u7a0b<\/strong><\/p>\n<p><strong>\u6982\u8ff0<\/strong><\/p>\n<p>\u5e76\u884c\u6d41\u662f Java \u5e76\u53d1\u5e93\u4e2d\u7684\u4e00\u4e2a\u9ad8\u7ea7\u7279\u6027\uff0c\u7528\u4e8e\u5728\u591a\u6838\u5904\u7406\u5668\u4e0a\u5e76\u884c\u5904\u7406\u5143\u7d20\u96c6\u5408\u3002\u5b83\u63d0\u4f9b\u4e86\u4e00\u79cd\u7b80\u5355\u4e14\u9ad8\u6548\u7684\u65b9\u5f0f\u6765\u4ee5\u51fd\u6570\u5f0f\u65b9\u5f0f\u5e76\u884c\u5316\u8ba1\u7b97\u3002<\/p>\n<p><strong>\u4f7f\u7528\u5e76\u884c\u6d41<\/strong><\/p>\n<p>\u8981\u521b\u5efa\u5e76\u884c\u6d41\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>parallel()<\/code> \u65b9\u6cd5\uff1a<\/p>\n<pre>Stream&lt;T&gt; stream = collection.parallelStream();<\/pre>\n<p>\u8fd9\u5c06\u521b\u5efa\u4e00\u4e2a\u5e76\u884c\u6d41\uff0c\u8be5\u6d41\u53ef\u4ee5\u5728\u591a\u4e2a\u7ebf\u7a0b\u4e0a\u5904\u7406\u5143\u7d20\u3002<\/p>\n<p><strong>\u4f7f\u7528\u5e76\u884c\u6d41\u8f6c\u6362\u5143\u7d20<\/strong><\/p>\n<p>\u5e76\u884c\u6d41\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u6765\u5e76\u884c\u8f6c\u6362\u5143\u7d20\uff0c\u5305\u62ec <code>map()<\/code>\u3001<code>filter()<\/code> \u548c <code>reduce()<\/code>\u3002\u8fd9\u4e9b\u65b9\u6cd5\u4e0e\u987a\u5e8f\u6d41\u4e2d\u76f8\u540c\u7684\u65b9\u6cd5\u7c7b\u4f3c\uff0c\u4f46\u5b83\u4eec\u4f1a\u5728\u591a\u4e2a\u7ebf\u7a0b\u4e0a\u5e76\u884c\u6267\u884c\u3002<\/p>\n<p>\u4f8b\u5982\uff0c\u4ee5\u4e0b\u4ee3\u7801\u4f7f\u7528 <code>map()<\/code> \u65b9\u6cd5\u5c06\u96c6\u5408\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u4e58\u4ee5 2\uff1a<\/p>\n<pre>List&lt;Integer&gt; numbers = Arrays.asList(1, 2, 3, 4, 5);\n\nnumbers.parallelStream()\n       .map(n -&gt; n * 2)\n       .forEach(System.out::println);<\/pre>\n<p><strong>\u5b9e\u6218\u6848\u4f8b<\/strong><\/p>\n<p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528\u5e76\u884c\u6d41\u7684\u5b9e\u6218\u6848\u4f8b\uff1a<\/p>\n<p><strong>\u95ee\u9898\uff1a<\/strong>\u8ba1\u7b97\u5927\u6570\u7ec4\u4e2d\u5076\u6570\u5143\u7d20\u7684\u603b\u548c\u3002<\/p>\n<p><strong>\u89e3\u51b3\u65b9\u6848\uff1a<\/strong><\/p>\n<pre>int[] numbers = new int[1_000_000];\n\/\/ ... \u521d\u59cb\u5316\u6570\u7ec4 ...\n\nint sum = Arrays.stream(numbers)\n               .parallel()\n               .filter(n -&gt; n % 2 == 0)\n               .sum();\n\nSystem.out.println(\"\u5076\u6570\u5143\u7d20\u7684\u603b\u548c\uff1a\" + sum);<\/pre>\n<p><strong>\u6ce8\u610f\uff1a<\/strong><\/p>\n<ul>\n<li>\u5c3d\u7ba1\u5e76\u884c\u6d41\u901a\u5e38\u53ef\u4ee5\u63d0\u9ad8\u6027\u80fd\uff0c\u4f46\u5b83\u53ef\u80fd\u4e0d\u4f1a\u59cb\u7ec8\u6bd4\u987a\u5e8f\u6d41\u66f4\u5feb\u3002\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u987a\u5e8f\u6d41\u7684\u5f00\u9500\u8f83\u4f4e\uff0c\u53ef\u80fd\u4f1a\u66f4\u6709\u6548\u7387\u3002<\/li>\n<li>\u5e76\u884c\u6d41\u53ef\u80fd\u4f1a\u5bfc\u81f4\u975e\u786e\u5b9a\u6027\u7ed3\u679c\uff0c\u56e0\u4e3a\u7ebf\u7a0b\u6267\u884c\u987a\u5e8f\u53ef\u80fd\u4e0d\u540c\u3002\u5982\u679c\u60a8\u9700\u8981\u786e\u4fdd\u7ed3\u679c\u7684\u786e\u5b9a\u6027\uff0c\u8bf7\u4f7f\u7528\u987a\u5e8f\u6d41\u3002<\/li>\n<\/ul>\n<p>\u4eca\u5929\u5e26\u5927\u5bb6\u4e86\u89e3\u4e86\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u5e0c\u671b\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff1b\u5173\u4e8e\u6587\u7ae0\u7684\u6280\u672f\u77e5\u8bc6\u6211\u4eec\u4f1a\u4e00\u70b9\u70b9\u6df1\u5165\u4ecb\u7ecd\uff0c\u6b22\u8fce\u5927\u5bb6\u5173\u6ce8\u516c\u4f17\u53f7\uff0c\u4e00\u8d77\u5b66\u4e60\u7f16\u7a0b~<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u4f7f\u7528\u5e76\u884c\u6d41\u5728 Java \u4e2d\u5b9e&#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-202528","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/202528","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=202528"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/202528\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=202528"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=202528"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=202528"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}