{"id":202581,"date":"2025-05-22T11:08:34","date_gmt":"2025-05-22T03:08:34","guid":{"rendered":"https:\/\/server.hk\/cnblog\/202581\/"},"modified":"2025-05-22T11:08:34","modified_gmt":"2025-05-22T03:08:34","slug":"%e4%bd%bf%e7%94%a8%e7%ba%bf%e7%a8%8b%e6%b1%a0%e4%bc%98%e5%8c%96%e5%87%bd%e6%95%b0%e5%bc%8f%e5%b9%b6%e8%a1%8c%e8%ae%a1%e7%ae%97%e7%9a%84%e6%80%a7%e8%83%bd%e8%a1%a8%e7%8e%b0","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/202581\/","title":{"rendered":"\u4f7f\u7528\u7ebf\u7a0b\u6c60\u4f18\u5316\u51fd\u6570\u5f0f\u5e76\u884c\u8ba1\u7b97\u7684\u6027\u80fd\u8868\u73b0"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>\u4f7f\u7528\u7ebf\u7a0b\u6c60\u4f18\u5316\u51fd\u6570\u5f0f\u5e76\u884c\u8ba1\u7b97\u7684\u6027\u80fd\u8868\u73b0<\/h1>\n<p>\u4e00\u5206\u8015\u8018\uff0c\u4e00\u5206\u6536\u83b7\uff01\u65e2\u7136\u90fd\u6253\u5f00\u8fd9\u7bc7<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u4f7f\u7528\u7ebf\u7a0b\u6c60\u4f18\u5316\u51fd\u6570\u5f0f\u5e76\u884c\u8ba1\u7b97\u7684\u6027\u80fd\u8868\u73b0\u300b<\/span>\uff0c\u5c31\u575a\u6301\u770b\u4e0b\u53bb\uff0c\u5b66\u4e0b\u53bb\u5427\uff01\u672c\u6587\u4e3b\u8981\u4f1a\u7ed9\u5927\u5bb6\u8bb2\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5982\u679c\u5927\u5bb6\u5bf9\u672c\u6587\u6709\u597d\u7684\u5efa\u8bae\u6216\u8005\u770b\u5230\u6709\u4e0d\u8db3\u4e4b\u5904\uff0c\u975e\u5e38\u6b22\u8fce\u5927\u5bb6\u79ef\u6781\u63d0\u51fa\uff01\u5728\u540e\u7eed\u6587\u7ae0\u6211\u4f1a\u7ee7\u7eed\u66f4\u65b0<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\u76f8\u5173\u7684\u5185\u5bb9\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u90fd\u6709\u6240\u5e2e\u52a9\uff01<\/p>\n<p>\u4f7f\u7528\u7ebf\u7a0b\u6c60\u4f18\u5316\u51fd\u6570\u5f0f\u5e76\u884c\u8ba1\u7b97\u53ef\u4ee5\u63d0\u9ad8\u5176\u6027\u80fd\u8868\u73b0\uff0c\u6b65\u9aa4\u5982\u4e0b\uff1a\u521b\u5efa\u7ebf\u7a0b\u6c60\uff1a\u6307\u5b9a\u7ebf\u7a0b\u6570\u91cf\u3001\u6700\u5927\u7ebf\u7a0b\u6570\u91cf\u548c\u961f\u5217\u5927\u5c0f\u3002\u63d0\u4ea4\u4efb\u52a1\uff1a\u4f7f\u7528 submit() \u65b9\u6cd5\u5c06\u5e76\u884c\u8ba1\u7b97\u4efb\u52a1\u63d0\u4ea4\u5230\u7ebf\u7a0b\u6c60\u3002\u83b7\u53d6\u7ed3\u679c\uff1a\u901a\u8fc7 Future \u5bf9\u8c61\u83b7\u53d6\u4efb\u52a1\u7ed3\u679c\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241026\/1729919910671c7ba6e5094.jpg\" class=\"aligncenter\"><\/p>\n<h2>\u4f7f\u7528\u7ebf\u7a0b\u6c60\u4f18\u5316\u51fd\u6570\u5f0f\u5e76\u884c\u8ba1\u7b97\u7684\u6027\u80fd\u8868\u73b0<\/h2>\n<p><strong>\u7b80\u4ecb<\/strong><\/p>\n<p>\u51fd\u6570\u5f0f\u5e76\u884c\u8ba1\u7b97\u662f\u4e00\u79cd\u5229\u7528\u591a\u6838 CPU \u63d0\u9ad8\u8ba1\u7b97\u901f\u5ea6\u7684\u6280\u672f\u3002\u7136\u800c\uff0c\u76f4\u63a5\u4f7f\u7528\u51fd\u6570\u5f0f\u5e76\u884c\u8ba1\u7b97\u53ef\u80fd\u4f1a\u5b58\u5728\u6548\u7387\u95ee\u9898\u3002\u5f15\u5165\u7ebf\u7a0b\u6c60\u53ef\u4ee5\u6709\u6548\u4f18\u5316\u5176\u6027\u80fd\u8868\u73b0\u3002<\/p>\n<p><strong>\u7ebf\u7a0b\u6c60<\/strong><\/p>\n<p>\u7ebf\u7a0b\u6c60\u662f\u4e00\u7ec4\u9884\u5206\u914d\u7684\u7ebf\u7a0b\uff0c\u7528\u4e8e\u5904\u7406\u5f02\u6b65\u4efb\u52a1\u3002\u901a\u8fc7\u5c06\u4efb\u52a1\u63d0\u4ea4\u5230\u7ebf\u7a0b\u6c60\uff0c\u53ef\u4ee5\u907f\u514d\u6bcf\u6b21\u4efb\u52a1\u6267\u884c\u65f6\u521b\u5efa\u65b0\u7ebf\u7a0b\u7684\u5f00\u9500\u3002<\/p>\n<p><strong>\u4f7f\u7528\u7ebf\u7a0b\u6c60\u4f18\u5316<\/strong><\/p>\n<p>\u4e3a\u4e86\u4f7f\u7528\u7ebf\u7a0b\u6c60\u4f18\u5316\u51fd\u6570\u5f0f\u5e76\u884c\u8ba1\u7b97\uff0c\u53ef\u4ee5\u9075\u5faa\u4ee5\u4e0b\u6b65\u9aa4\uff1a<\/p>\n<ol>\n<li><strong>\u521b\u5efa\u7ebf\u7a0b\u6c60\uff1a<\/strong>\u4f7f\u7528 <code>ThreadPoolExecutor<\/code> \u7c7b\u521b\u5efa\u4e00\u4e2a\u7ebf\u7a0b\u6c60\uff0c\u6307\u5b9a\u7ebf\u7a0b\u6570\u91cf\u3001\u6700\u5927\u7ebf\u7a0b\u6570\u91cf\u548c\u961f\u5217\u5927\u5c0f\u3002<\/li>\n<li><strong>\u63d0\u4ea4\u4efb\u52a1\uff1a<\/strong>\u5c06\u5e76\u884c\u8ba1\u7b97\u4efb\u52a1\u63d0\u4ea4\u5230\u7ebf\u7a0b\u6c60\u3002\u53ef\u4ee5\u4f7f\u7528 <code>submit()<\/code> \u65b9\u6cd5\uff0c\u8be5\u65b9\u6cd5\u8fd4\u56de\u4e00\u4e2a <code>Future<\/code> \u5bf9\u8c61\uff0c\u7528\u4e8e\u83b7\u53d6\u4efb\u52a1\u7ed3\u679c\u3002<\/li>\n<\/ol>\n<p><strong>\u5b9e\u6218\u6848\u4f8b<\/strong><\/p>\n<p>\u8003\u8651\u4e00\u4e2a\u7b80\u5355\u7684\u6c42\u548c\u51fd\u6570\uff1a<\/p>\n<pre>int sum(int[] arr) {\n    int total = 0;\n    for (int num : arr) {\n        total += num;\n    }\n    return total;\n}<\/pre>\n<p>\u4ee5\u4e0b\u4ee3\u7801\u6f14\u793a\u4e86\u5982\u4f55\u4f7f\u7528\u7ebf\u7a0b\u6c60\u4f18\u5316 <code>sum()<\/code> \u51fd\u6570\u7684\u5e76\u884c\u8ba1\u7b97\uff1a<\/p>\n<pre>import java.util.concurrent.ExecutorService;\nimport java.util.concurrent.Executors;\nimport java.util.concurrent.Future;\n\npublic class ThreadPoolDemo {\n\n    public static void main(String[] args) {\n        int[] arr = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};\n\n        \/\/ \u521b\u5efa\u7ebf\u7a0b\u6c60\n        ExecutorService pool = Executors.newFixedThreadPool(4);\n\n        \/\/ \u63d0\u4ea4\u4efb\u52a1\u5e76\u83b7\u53d6\u7ed3\u679c\n        Future&lt;Integer&gt; future = pool.submit(() -&gt; sum(arr));\n\n        \/\/ \u5173\u95ed\u7ebf\u7a0b\u6c60\n        pool.shutdown();\n\n        \/\/ \u83b7\u53d6\u7ed3\u679c\n        int total = future.get();\n        System.out.println(\"Total: \" + total);\n    }\n}<\/pre>\n<p><strong>\u7ed3\u8bba<\/strong><\/p>\n<p>\u901a\u8fc7\u4f7f\u7528\u7ebf\u7a0b\u6c60\uff0c\u53ef\u4ee5\u663e\u8457\u4f18\u5316\u51fd\u6570\u5f0f\u5e76\u884c\u8ba1\u7b97\u7684\u6027\u80fd\u8868\u73b0\u3002\u7ebf\u7a0b\u6c60\u901a\u8fc7\u590d\u7528\u7ebf\u7a0b\uff0c\u51cf\u5c11\u4e86\u521b\u5efa\u65b0\u7ebf\u7a0b\u7684\u5f00\u9500\uff0c\u4ece\u800c\u63d0\u9ad8\u4e86\u5e76\u884c\u8ba1\u7b97\u7684\u6548\u7387\u3002<\/p>\n<p>\u597d\u4e86\uff0c\u672c\u6587\u5230\u6b64\u7ed3\u675f\uff0c\u5e26\u5927\u5bb6\u4e86\u89e3\u4e86\u300a\u4f7f\u7528\u7ebf\u7a0b\u6c60\u4f18\u5316\u51fd\u6570\u5f0f\u5e76\u884c\u8ba1\u7b97\u7684\u6027\u80fd\u8868\u73b0\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>\u4f7f\u7528\u7ebf\u7a0b\u6c60\u4f18\u5316\u51fd\u6570\u5f0f\u5e76\u884c\u8ba1\u7b97\u7684\u6027&#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-202581","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/202581","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=202581"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/202581\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=202581"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=202581"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=202581"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}