{"id":202738,"date":"2025-05-22T10:15:35","date_gmt":"2025-05-22T02:15:35","guid":{"rendered":"https:\/\/server.hk\/cnblog\/202738\/"},"modified":"2025-05-22T10:15:35","modified_gmt":"2025-05-22T02:15:35","slug":"java%e5%b9%b6%e5%8f%91%e5%b7%a5%e5%85%b7%e7%b1%bb%e4%b8%8e%e5%87%bd%e6%95%b0%e5%bc%8f%e7%bc%96%e7%a8%8b%e5%b9%b6%e8%a1%8c%e8%ae%a1%e7%ae%97%e7%9a%84%e6%9c%80%e4%bd%b3%e5%ae%9e%e8%b7%b5","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/202738\/","title":{"rendered":"Java\u5e76\u53d1\u5de5\u5177\u7c7b\u4e0e\u51fd\u6570\u5f0f\u7f16\u7a0b\u5e76\u884c\u8ba1\u7b97\u7684\u6700\u4f73\u5b9e\u8df5"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>Java\u5e76\u53d1\u5de5\u5177\u7c7b\u4e0e\u51fd\u6570\u5f0f\u7f16\u7a0b\u5e76\u884c\u8ba1\u7b97\u7684\u6700\u4f73\u5b9e\u8df5<\/h1>\n<p>\u54c8\u55bd\uff01\u4eca\u5929\u5fc3\u8840\u6765\u6f6e\u7ed9\u5927\u5bb6\u5e26\u6765\u4e86<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aJava\u5e76\u53d1\u5de5\u5177\u7c7b\u4e0e\u51fd\u6570\u5f0f\u7f16\u7a0b\u5e76\u884c\u8ba1\u7b97\u7684\u6700\u4f73\u5b9e\u8df5\u300b<\/span>\uff0c\u60f3\u5fc5\u5927\u5bb6\u5e94\u8be5\u5bf9<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\u90fd\u4e0d\u964c\u751f\u5427\uff0c\u90a3\u4e48\u9605\u8bfb\u672c\u6587\u5c31\u90fd\u4e0d\u4f1a\u5f88\u56f0\u96be\uff0c\u4ee5\u4e0b\u5185\u5bb9\u4e3b\u8981\u6d89\u53ca\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\uff0c\u82e5\u662f\u4f60\u6b63\u5728\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\uff0c\u5343\u4e07\u522b\u9519\u8fc7\u8fd9\u7bc7\u6587\u7ae0~\u5e0c\u671b\u80fd\u5e2e\u52a9\u5230\u4f60\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241026\/1729937848671cc1b8387b0.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>Java\u5e76\u53d1\u5de5\u5177\u7c7b\u4e0e\u51fd\u6570\u5f0f\u7f16\u7a0b\u5e76\u884c\u8ba1\u7b97\u7684\u6700\u4f73\u5b9e\u8df5<\/strong><\/p>\n<p><strong>\u5f15\u8a00<\/strong><\/p>\n<p>\u5728\u73b0\u4ee3\u8f6f\u4ef6\u5f00\u53d1\u4e2d\uff0c\u5e76\u884c\u8ba1\u7b97\u5df2\u6210\u4e3a\u63d0\u5347\u5e94\u7528\u7a0b\u5e8f\u6027\u80fd\u548c\u6269\u5c55\u6027\u7684\u5173\u952e\u3002Java\u63d0\u4f9b\u4e86\u4e00\u7cfb\u5217\u4f18\u96c5\u7684\u5e76\u53d1\u5de5\u5177\u7c7b\u548c\u51fd\u6570\u5f0f\u7f16\u7a0b\u7279\u6027\uff0c\u4f7f\u5f00\u53d1\u4eba\u5458\u80fd\u591f\u9ad8\u6548\u5730\u7f16\u5199\u5e76\u884c\u4ee3\u7801\u3002\u672c\u6587\u5c06\u63a2\u7d22\u8fd9\u4e9b\u5de5\u5177\u7684\u4f7f\u7528\u6700\u4f73\u5b9e\u8df5\uff0c\u5e76\u901a\u8fc7\u5b9e\u6218\u6848\u4f8b\u52a0\u4ee5\u8bf4\u660e\u3002<\/p>\n<p><strong>\u5e76\u53d1\u5de5\u5177\u7c7b<\/strong><\/p>\n<ul>\n<li><strong>ExecutorService\uff1a<\/strong>\u7528\u4e8e\u7ba1\u7406\u7ebf\u7a0b\u6c60\u5e76\u6267\u884c\u5f02\u6b65\u4efb\u52a1\u7684\u63a5\u53e3\u3002<\/li>\n<li><strong>Future\uff1a<\/strong>\u4ee3\u8868\u5f02\u6b65\u64cd\u4f5c\u7684\u7ed3\u679c\uff0c\u53ef\u7528\u4e8e\u68c0\u67e5\u4efb\u52a1\u72b6\u6001\u5e76\u83b7\u53d6\u7ed3\u679c\u3002<\/li>\n<li><strong>Callable\uff1a<\/strong>\u53ef\u8c03\u7528\u5bf9\u8c61\uff0c\u7528\u4e8e\u5728\u5f02\u6b65\u4efb\u52a1\u4e2d\u6267\u884c\u590d\u6742\u903b\u8f91\u3002<\/li>\n<li><strong>BlockingQueue\uff1a<\/strong>\u4e00\u4e2a\u7ebf\u7a0b\u5b89\u5168\u7684\u961f\u5217\uff0c\u5141\u8bb8\u7ebf\u7a0b\u5728\u751f\u4ea7\u8005\u548c\u6d88\u8d39\u8005\u4e4b\u95f4\u901a\u4fe1\u3002<\/li>\n<li><strong>AtomicClasses\uff1a<\/strong>\u7528\u4e8e\u64cd\u4f5c\u5171\u4eab\u72b6\u6001\u53d8\u91cf\uff0c\u786e\u4fdd\u7ebf\u7a0b\u5b89\u5168\u6027\u3002<\/li>\n<\/ul>\n<p><strong>\u51fd\u6570\u5f0f\u7f16\u7a0b<\/strong><\/p>\n<ul>\n<li><strong>Lambda\u8868\u8fbe\u5f0f\uff1a<\/strong>\u5141\u8bb8\u533f\u540d\u5b9a\u4e49\u51fd\u6570\uff0c concise \u4ee3\u7801\u5e76\u63d0\u9ad8\u53ef\u8bfb\u6027\u3002<\/li>\n<li>\n<p><strong>\u6d41\uff08Stream\uff09\uff1a<\/strong>\u8868\u793a\u5143\u7d20\u5e8f\u5217\uff0c\u652f\u6301\u5e76\u884c\u64cd\u4f5c\u3002<\/p>\n<ul>\n<li><code>parallel()\uff1a<\/code>\u5c06\u6d41\u8f6c\u6362\u4e3a\u5e76\u884c\u6d41\uff0c\u4f7f\u5176\u53ef\u4ee5\u5728\u591a\u4e2a\u7ebf\u7a0b\u4e0a\u5904\u7406\u3002<\/li>\n<li><code>forEachParallel()\uff1a<\/code>\u5bf9\u6d41\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u5e76\u884c\u6267\u884c\u64cd\u4f5c\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p><strong>\u6700\u4f73\u5b9e\u8df5<\/strong><\/p>\n<p><strong>1. \u9009\u62e9\u5408\u9002\u7684\u6267\u884c\u6a21\u578b<\/strong><\/p>\n<p>\u6839\u636e\u4efb\u52a1\u7279\u6027\uff0c\u9009\u62e9\u6b63\u786e\u7684\u5e76\u53d1\u5de5\u5177\u6216\u51fd\u6570\u5f0f\u7f16\u7a0b\u7279\u6027\u3002\u5bf9\u4e8e\u5bc6\u96c6\u8ba1\u7b97\u4efb\u52a1\uff0c\u51fd\u6570\u5f0f\u7f16\u7a0b\u5f80\u5f80\u66f4\u4e3a\u9ad8\u6548\uff0c\u800c\u5bf9\u4e8e\u9700\u8981\u7ebf\u7a0b\u534f\u8c03\u7684\u4efb\u52a1\uff0c\u5e76\u53d1\u5de5\u5177\u7c7b\u66f4\u5408\u9002\u3002<\/p>\n<p><strong>2. \u907f\u514d\u7ebf\u7a0b\u963b\u585e<\/strong><\/p>\n<p>\u5c3d\u53ef\u80fd\u5c06\u4efb\u52a1\u8bbe\u8ba1\u4e3a\u975e\u963b\u585e\u7684\uff0c\u907f\u514d\u7ebf\u7a0b\u957f\u65f6\u95f4\u7b49\u5f85\u3002\u4f7f\u7528<code>Future<\/code>\u548c\u56de\u8c03\u673a\u5236\u6765\u5904\u7406\u5f02\u6b65\u4efb\u52a1\u7684\u7ed3\u679c\u3002<\/p>\n<p><strong>3. \u8003\u8651\u7ebf\u7a0b\u5b89\u5168<\/strong><\/p>\n<p>\u786e\u4fdd\u5171\u4eab\u72b6\u6001\u53d8\u91cf\u5728\u5e76\u53d1\u73af\u5883\u4e0b\u662f\u7ebf\u7a0b\u5b89\u5168\u7684\u3002\u4f7f\u7528<code>AtomicClasses<\/code>\u6216\u9002\u5f53\u7684\u540c\u6b65\u673a\u5236\u6765\u4fdd\u62a4\u8fd9\u4e9b\u53d8\u91cf\u3002<\/p>\n<p><strong>4. \u4f7f\u7528\u5e76\u884c\u6d41<\/strong><\/p>\n<p>\u5728\u5904\u7406\u5927\u91cf\u6570\u636e\u65f6\uff0c\u5229\u7528<code>Stream<\/code>\u7684\u5e76\u884c\u7279\u6027\u53ef\u4ee5\u663e\u7740\u63d0\u9ad8\u6027\u80fd\u3002\u901a\u8fc7\u8c03\u7528<code>parallel()<\/code>, \u6d41\u4e0a\u7684\u64cd\u4f5c\u5c06\u81ea\u52a8\u5728\u591a\u4e2a\u7ebf\u7a0b\u4e0a\u6267\u884c\u3002<\/p>\n<p><strong>5. \u9650\u5236\u5e76\u884c\u6027<\/strong><\/p>\n<p>\u8fc7\u5ea6\u7684\u5e76\u884c\u6027\u53ef\u80fd\u4f1a\u5bfc\u81f4\u8d44\u6e90\u4e89\u7528\u548c\u6027\u80fd\u4e0b\u964d\u3002\u6839\u636e\u7cfb\u7edf\u8d44\u6e90\u9650\u5236\u5408\u7406\u8bbe\u7f6e\u7ebf\u7a0b\u6c60\u5927\u5c0f\u6216\u6d41\u5e76\u884c\u5ea6\u3002<\/p>\n<p><strong>\u5b9e\u6218\u6848\u4f8b\uff1a\u56fe\u50cf\u5904\u7406<\/strong><\/p>\n<p>\u8003\u8651\u4e00\u4e2a\u9700\u8981\u5904\u7406\u5927\u91cf\u56fe\u50cf\u5e76\u751f\u6210\u7f29\u7565\u56fe\u7684\u4efb\u52a1\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5e76\u884c\u6d41\u6765\u5e76\u884c\u5904\u7406\u6bcf\u4e2a\u56fe\u50cf\uff1a<\/p>\n<pre>import java.util.concurrent.ForkJoinPool;\nimport java.util.stream.Stream;\n\npublic class ImageProcessing {\n\n  public static void main(String[] args) {\n    Image[] images = getImages();\n\n    \/\/ \u4f7f\u7528\u5e76\u884c\u6d41\u5e76\u884c\u5904\u7406\u56fe\u50cf\n    Stream&lt;Image&gt; processedImages = Stream.of(images)\n        .parallel()\n        .map(Image::createThumbnail);\n\n    \/\/ \u6253\u5370\u7f29\u7565\u56fe\u7684\u8def\u5f84\n    processedImages.forEach(image -&gt; System.out.println(image.getThumbnailPath()));\n  }\n\n  \/\/ \u83b7\u53d6\u56fe\u50cf\u5217\u8868\uff08\u793a\u4f8b\u4ee3\u7801\uff09\n  private static Image[] getImages() {\n    \/\/ \u5047\u8bbe\u4ece\u6587\u4ef6\u7cfb\u7edf\u6216\u6570\u636e\u5e93\u4e2d\u83b7\u53d6\u56fe\u50cf\n    return new Image[1000];\n  }\n}<\/pre>\n<p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5c06\u56fe\u50cf\u5217\u8868\u8f6c\u6362\u4e3a\u5e76\u884c\u6d41\u5e76\u5e94\u7528<code>createThumbnail<\/code>\u65b9\u6cd5\u4ee5\u751f\u6210\u6bcf\u4e2a\u56fe\u50cf\u7684\u7f29\u7565\u56fe\u3002<code>ForkJoinPool<\/code>\u662f\u4e00\u4e2a\u9ed8\u8ba4\u7684\u5e76\u53d1\u6846\u67b6\uff0c\u7528\u4e8e\u5e76\u884c\u6267\u884c\u6d41\u4e2d\u7684\u64cd\u4f5c\u3002<\/p>\n<p><strong>\u7ed3\u8bba<\/strong><\/p>\n<p>\u719f\u7ec3\u4f7f\u7528Java\u5e76\u53d1\u5de5\u5177\u7c7b\u548c\u51fd\u6570\u5f0f\u7f16\u7a0b\u7279\u6027\u5bf9\u4e8e\u7f16\u5199\u9ad8\u6548\u4e14\u53ef\u4f38\u7f29\u7684\u5e76\u884c\u4ee3\u7801\u81f3\u5173\u91cd\u8981\u3002\u9075\u5faa\u8fd9\u4e9b\u6700\u4f73\u5b9e\u8df5\uff0c\u5f00\u53d1\u4eba\u5458\u53ef\u4ee5\u5145\u5206\u5229\u7528\u8fd9\u4e9b\u5de5\u5177\u6765\u63d0\u9ad8\u5e94\u7528\u7a0b\u5e8f\u6027\u80fd\u5e76\u6ee1\u8db3\u73b0\u4ee3\u8f6f\u4ef6\u5f00\u53d1\u7684\u4e25\u82db\u9700\u6c42\u3002<\/p>\n<p>\u597d\u4e86\uff0c\u672c\u6587\u5230\u6b64\u7ed3\u675f\uff0c\u5e26\u5927\u5bb6\u4e86\u89e3\u4e86\u300aJava\u5e76\u53d1\u5de5\u5177\u7c7b\u4e0e\u51fd\u6570\u5f0f\u7f16\u7a0b\u5e76\u884c\u8ba1\u7b97\u7684\u6700\u4f73\u5b9e\u8df5\u300b\uff0c\u5e0c\u671b\u672c\u6587\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff01\u5173\u6ce8\u516c\u4f17\u53f7\uff0c\u7ed9\u5927\u5bb6\u5206\u4eab\u66f4\u591a\u6587\u7ae0\u77e5\u8bc6\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Java\u5e76\u53d1\u5de5\u5177\u7c7b\u4e0e\u51fd\u6570\u5f0f\u7f16\u7a0b\u5e76&#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-202738","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/202738","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=202738"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/202738\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=202738"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=202738"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=202738"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}