{"id":3666,"date":"2025-04-26T19:12:36","date_gmt":"2025-04-26T11:12:36","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=3666"},"modified":"2025-04-26T19:12:36","modified_gmt":"2025-04-26T11:12:36","slug":"threadpoolexecutor-execute-%e6%a0%b8%e5%bf%83%e6%ba%90%e7%a0%81%e6%b5%81%e7%a8%8b%e8%a7%a3%e6%9e%90","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/04\/26\/threadpoolexecutor-execute-%e6%a0%b8%e5%bf%83%e6%ba%90%e7%a0%81%e6%b5%81%e7%a8%8b%e8%a7%a3%e6%9e%90\/","title":{"rendered":"ThreadPoolExecutor.execute() \u6838\u5fc3\u6e90\u7801\u6d41\u7a0b\u89e3\u6790"},"content":{"rendered":"\n<p>\u5982\u679c\u4f60\u771f\u7684\u8bfb\u8fc7ThreadPoolExecutor.execute() \u6e90\u7801\uff0c\u8fd8\u80fd\u5e94\u7528\u5230\u9762\u8bd5\u4e0a\uff0c\u9762\u8bd5\u5b98\u4e00\u5b9a\u5bf9\u4f60\u53e6\u773c\u76f8\u770b\uff01<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">1. \u6e90\u7801\u5165\u53e3\uff08\u57fa\u4e8e JDK 8\uff09<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>public void execute(Runnable command) {\n    if (command == null)\n        throw new NullPointerException();\n    int c = ctl.get();\n    if (workerCountOf(c) &lt; corePoolSize) {\n        if (addWorker(command, true))\n            return;\n        c = ctl.get();\n    }\n    if (isRunning(c) &amp;&amp; workQueue.offer(command)) {\n        int recheck = ctl.get();\n        if (!isRunning(recheck) &amp;&amp; remove(command))\n            reject(command);\n        else if (workerCountOf(recheck) == 0)\n            addWorker(null, false);\n    }\n    else if (!addWorker(command, false))\n        reject(command);\n}<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u7b2c\u4e00\u6b65\uff1a\u53c2\u6570\u6821\u9a8c<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>if (command == null)\n    throw new NullPointerException();<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5982\u679c\u63d0\u4ea4\u7684\u4efb\u52a1\u662f <code>null<\/code>\uff0c\u76f4\u63a5\u629b\u51fa\u7a7a\u6307\u9488\u5f02\u5e38\u3002<\/li>\n\n\n\n<li><strong>\u76ee\u7684\uff1a\u4fdd\u8bc1\u7ebf\u7a0b\u6c60\u5904\u7406\u7684\u662f\u6709\u6548\u4efb\u52a1\u3002<\/strong><\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u7b2c\u4e8c\u6b65\uff1a\u83b7\u53d6\u5f53\u524d\u7ebf\u7a0b\u6c60\u72b6\u6001<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>int c = ctl.get();<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>ctl<\/code> \u662f\u4e00\u4e2a\u9ad8\u4f4d\u5b58\u7ebf\u7a0b\u6c60\u8fd0\u884c\u72b6\u6001\uff0c\u4f4e\u4f4d\u5b58\u7ebf\u7a0b\u6570\u91cf\u7684\u590d\u5408\u53d8\u91cf\uff08int \u578b\uff0c\u4f4d\u64cd\u4f5c\u7f16\u7801\uff09\u3002<\/li>\n\n\n\n<li><code>workerCountOf(c)<\/code> \u83b7\u53d6\u5f53\u524d\u6709\u6548\u7ebf\u7a0b\u6570\u91cf\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u7b2c\u4e09\u6b65\uff1a\u6838\u5fc3\u7ebf\u7a0b\u662f\u5426\u80fd\u5904\u7406\uff1f<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>if (workerCountOf(c) &lt; corePoolSize) {\n    if (addWorker(command, true))\n        return;\n    c = ctl.get(); \/\/ \u5931\u8d25\u540e\u91cd\u65b0\u83b7\u53d6\u72b6\u6001\n}<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5f53\u524d\u8fd0\u884c\u7ebf\u7a0b\u6570\u5c0f\u4e8e <code>corePoolSize<\/code>\uff1f<\/li>\n\n\n\n<li>\u662f \u2192 \u5c1d\u8bd5\u521b\u5efa\u4e00\u4e2a<strong>\u65b0\u7684\u6838\u5fc3\u7ebf\u7a0b<\/strong>\u6765\u6267\u884c\u8fd9\u4e2a\u4efb\u52a1\u3002<\/li>\n\n\n\n<li>\u8c03\u7528 <code>addWorker(command, true)<\/code>\uff0c\u53c2\u6570 <code>true<\/code> \u8868\u793a\u521b\u5efa<strong>\u6838\u5fc3\u7ebf\u7a0b<\/strong>\u3002<\/li>\n\n\n\n<li>\u521b\u5efa\u6210\u529f\uff0c\u4efb\u52a1\u5c31\u76f4\u63a5\u4ea4\u7ed9\u65b0\u7ebf\u7a0b\u5904\u7406\uff0c\u65b9\u6cd5\u8fd4\u56de\uff0c\u7ed3\u675f\u3002<\/li>\n<\/ul>\n\n\n\n<p>\uff08\u6ce8\u610f\uff1a\u5982\u679c\u521b\u5efa\u6838\u5fc3\u7ebf\u7a0b\u5931\u8d25\uff0c\u6bd4\u5982\u7ebf\u7a0b\u6c60\u72b6\u6001\u53d8\u5316\u4e86\uff0c\u9700\u8981\u7ee7\u7eed\u5904\u7406\u3002\uff09<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u7b2c\u56db\u6b65\uff1a\u7ebf\u7a0b\u6c60\u6b63\u5728\u8fd0\u884c\u4e14\u961f\u5217\u80fd\u653e\u8fdb\u53bb\uff1f<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>if (isRunning(c) &amp;&amp; workQueue.offer(command)) {<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5982\u679c\u7ebf\u7a0b\u6c60\u72b6\u6001\u662f RUNNING\uff08\u6b63\u5e38\u8fd0\u884c\u4e2d\uff09\uff0c\u5e76\u4e14<\/li>\n\n\n\n<li>\u6210\u529f\u628a\u4efb\u52a1\u52a0\u5165\u5230\u5de5\u4f5c\u961f\u5217 <code>workQueue<\/code>\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u2705 \u8bf4\u660e\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u6ca1\u5fc5\u8981\u65b0\u5efa\u7ebf\u7a0b\u3002<\/li>\n\n\n\n<li>\u4efb\u52a1\u4f1a\u88ab\u5df2\u6709\u7ebf\u7a0b\u4ece\u961f\u5217\u53d6\u51fa\u6267\u884c\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u7b2c\u4e94\u6b65\uff1a\u4e8c\u6b21\u6821\u9a8c\uff08\u9632\u6b62\u5165\u961f\u540e\uff0c\u7ebf\u7a0b\u6c60\u5173\u95ed\uff09<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>int recheck = ctl.get();\nif (!isRunning(recheck) &amp;&amp; remove(command))\n    reject(command);\nelse if (workerCountOf(recheck) == 0)\n    addWorker(null, false);<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u4efb\u52a1\u521a\u52a0\u8fdb\u961f\u5217\uff0c<strong>\u7ebf\u7a0b\u6c60\u53ef\u80fd\u5728\u77ac\u95f4\u88ab\u5173\u95ed<\/strong>\u4e86\uff08\u6bd4\u5982\u88ab shutdown() \u8c03\u7528\u4e86\uff09\u3002<\/li>\n\n\n\n<li>\u6240\u4ee5\u9700\u8981<strong>\u518d\u6b21\u68c0\u67e5<\/strong>\u7ebf\u7a0b\u6c60\u72b6\u6001\u3002<\/li>\n\n\n\n<li>\u5982\u679c\u7ebf\u7a0b\u6c60\u4e0d\u662f\u8fd0\u884c\u72b6\u6001\u4e86\uff0c\u800c\u4e14\u80fd\u628a\u521a\u52a0\u8fdb\u53bb\u7684\u4efb\u52a1\u79fb\u9664\u6210\u529f \u2192 \u76f4\u63a5\u62d2\u7edd\u4efb\u52a1\u3002<\/li>\n\n\n\n<li>\u5982\u679c\u7ebf\u7a0b\u6c60\u662f\u7a7a\u7684\uff08worker\u6570\u4e3a 0\uff09\u2192 \u521b\u5efa\u4e00\u4e2a\u7ebf\u7a0b\u53bb\u4fdd\u8bc1\u81f3\u5c11\u6709\u7ebf\u7a0b\u6267\u884c\u961f\u5217\u91cc\u7684\u4efb\u52a1\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u7b2c\u516d\u6b65\uff1a\u961f\u5217\u653e\u4e0d\u4e0b\uff0c\u6269\u5145\u7ebf\u7a0b\uff08\u6269\u5c55\u7ebf\u7a0b\uff09<\/h2>\n\n\n\n<p>\u5982\u679c\u961f\u5217\u6ee1\u4e86\uff08offer \u5931\u8d25\uff09\u600e\u4e48\u529e\uff1f<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>else if (!addWorker(command, false))\n    reject(command);<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u4e0d\u80fd\u76f4\u63a5\u4e22\u5f03\uff01<\/li>\n\n\n\n<li>\u7ee7\u7eed\u5c1d\u8bd5\u521b\u5efa\u65b0\u7684\u7ebf\u7a0b\u6765\u6267\u884c\u8fd9\u4e2a\u4efb\u52a1\u3002<\/li>\n\n\n\n<li>\u8c03\u7528 <code>addWorker(command, false)<\/code>\uff0c<code>false<\/code> \u53c2\u6570\u8868\u793a\u521b\u5efa\u7684\u662f<strong>\u975e\u6838\u5fc3\u7ebf\u7a0b<\/strong>\uff08maximumPoolSize \u8303\u56f4\u5185\uff09\u3002<\/li>\n\n\n\n<li>\u5982\u679c\u52a0\u7ebf\u7a0b\u4e5f\u5931\u8d25\uff08\u7ebf\u7a0b\u6c60\u6ee1\u4e86\u6216\u8005\u5173\u95ed\u4e86\uff09 \u2192 \u8c03\u7528\u62d2\u7edd\u7b56\u7565 <code>reject(command)<\/code>\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u3010\u603b\u7ed3\u4e00\u53e5\u8bdd\u7248\u3011<\/h2>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>execute() \u7684\u6838\u5fc3\u6d41\u7a0b\u662f\uff1a<strong>\u5148\u770b\u80fd\u4e0d\u80fd\u52a0\u6838\u5fc3\u7ebf\u7a0b \u2794 \u52a0\u4e0d\u4e86\u5c31\u5165\u961f\u5217 \u2794 \u5165\u4e0d\u4e86\u961f\u5217\u5c31\u52a0\u6269\u5c55\u7ebf\u7a0b \u2794 \u8fd8\u4e0d\u884c\u5c31\u62d2\u7edd\u4efb\u52a1\u3002<\/strong><\/p>\n<\/blockquote>\n\n\n\n<p><strong>\u8fd9\u4e5f\u662f\u9762\u8bd5\u5e38\u95ee\u7684\u7ebf\u7a0b\u6c60\u6267\u884c\u987a\u5e8f\u53e3\u8bc0\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\u6838\u5fc3\u7ebf\u7a0b \u2794 \u5de5\u4f5c\u961f\u5217 \u2794 \u975e\u6838\u5fc3\u7ebf\u7a0b \u2794 \u62d2\u7edd\u5904\u7406<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\uff08\u62d3\u5c55\uff09\u9762\u8bd5\u8865\u5145\uff1aaddWorker() \u91cc\u9762\u5e72\u4e86\u5565\uff1f<\/h2>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\u5982\u679c\u9762\u8bd5\u5b98\u8ffd\u95ee\uff1a\u201caddWorker\u5185\u90e8\u662f\u600e\u4e48\u4fdd\u8bc1\u7ebf\u7a0b\u5b89\u5168\u7684\uff1f\u201d<\/p>\n<\/blockquote>\n\n\n\n<p>\u7b80\u5355\u56de\u7b54\u5c31\u53ef\u4ee5\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>addWorker() \u5185\u90e8\u7528\u4e86\u4e00\u628a\u91cd\u5165\u9501\uff08<code>mainLock<\/code>\uff09\u4fdd\u62a4 Worker \u7684\u6dfb\u52a0\u3002<\/li>\n\n\n\n<li>\u5148\u68c0\u67e5\u7ebf\u7a0b\u6c60\u72b6\u6001\u5408\u6cd5\u3002<\/li>\n\n\n\n<li>\u7136\u540e\u771f\u6b63\u542f\u52a8\u4e00\u4e2a\u65b0\u7684 Worker \u7ebf\u7a0b\uff08\u5c01\u88c5 Runnable\uff0c\u6267\u884c runTask\uff09\u3002<\/li>\n\n\n\n<li>\u7ebf\u7a0b\u5b89\u5168\u3001\u72b6\u6001\u68c0\u67e5\u90fd\u5728\u91cc\u9762\u505a\u5230\u4e86\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u66f4\u591a\u8be6\u7ec6\u5185\u5bb9\u8bf7\u5173\u6ce8\u5176\u4ed6\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u679c\u4f60\u771f\u7684\u8bfb\u8fc7ThreadPoolExecutor.execute() \u6e90\u7801\uff0c\u8fd8\u80fd\u5e94\u7528\u5230\u9762\u8bd5\u4e0a\uff0c\u9762\u8bd5\u5b98\u4e00\u5b9a\u5bf9\u4f60 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":3667,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[86],"tags":[],"class_list":["post-3666","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java-"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3666","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=3666"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3666\/revisions"}],"predecessor-version":[{"id":3668,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3666\/revisions\/3668"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media\/3667"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=3666"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=3666"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=3666"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}