{"id":37492,"date":"2023-12-11T14:41:41","date_gmt":"2023-12-11T06:41:41","guid":{"rendered":"https:\/\/wx.kaifamiao.info\/?p=37492"},"modified":"2023-12-11T14:41:41","modified_gmt":"2023-12-11T06:41:41","slug":"%e8%af%b7%e4%bb%8b%e7%bb%8d%e4%b8%80%e4%b8%8b-threadpoolexecutor-%e7%b1%bb%ef%bc%9f","status":"publish","type":"post","link":"http:\/\/wx.kaifamiao.info\/index.php\/2023\/12\/11\/%e8%af%b7%e4%bb%8b%e7%bb%8d%e4%b8%80%e4%b8%8b-threadpoolexecutor-%e7%b1%bb%ef%bc%9f\/","title":{"rendered":"\u8bf7\u4ecb\u7ecd\u4e00\u4e0b ThreadPoolExecutor \u7c7b\uff1f"},"content":{"rendered":"<p>&#8220;`&#8221;                    \u7ebf\u7a0b\u6c60\u5b9e\u73b0\u7c7b <code>ThreadPoolExecutor<\/code> \u662f <code>Executor<\/code> \u6846\u67b6\u6700\u6838\u5fc3\u7684\u7c7b\u3002**<\/p>\n<p>&lt;h3&gt;ThreadPoolExecutor \u7c7b\u5206\u6790&lt;\/h3&gt;<\/p>\n<p>&lt;code&gt;ThreadPoolExecutor&lt;\/code&gt; \u7c7b\u4e2d\u63d0\u4f9b\u7684\u56db\u4e2a\u6784\u9020\u65b9\u6cd5\u3002\u6211\u4eec\u6765\u770b\u6700\u957f\u7684\u90a3\u4e2a\uff0c\u5176\u4f59\u4e09\u4e2a\u90fd\u662f\u5728\u8fd9\u4e2a\u6784\u9020\u65b9\u6cd5\u7684\u57fa\u7840\u4e0a\u4ea7\u751f\uff08\u5176\u4ed6\u51e0\u4e2a\u6784\u9020\u65b9\u6cd5\u8bf4\u767d\u70b9\u90fd\u662f\u7ed9\u5b9a\u67d0\u4e9b\u9ed8\u8ba4\u53c2\u6570\u7684\u6784\u9020\u65b9\u6cd5\u6bd4\u5982\u9ed8\u8ba4\u5236\u5b9a\u62d2\u7edd\u7b56\u7565\u662f\u4ec0\u4e48\uff09\uff0c\u8fd9\u91cc\u5c31\u4e0d\u8d34\u4ee3\u7801\u8bb2\u4e86\uff0c\u6bd4\u8f83\u7b80\u5355\u3002<\/p>\n<p>&lt;pre&gt;&lt;code&gt;    \/**     * \u7528\u7ed9\u5b9a\u7684\u521d\u59cb\u53c2\u6570\u521b\u5efa\u4e00\u4e2a\u65b0\u7684ThreadPoolExecutor\u3002     *\/    public ThreadPoolExecutor(int corePoolSize,\/\/\u7ebf\u7a0b\u6c60\u7684\u6838\u5fc3\u7ebf\u7a0b\u6570\u91cf                              int maximumPoolSize,\/\/\u7ebf\u7a0b\u6c60\u7684\u6700\u5927\u7ebf\u7a0b\u6570                              long keepAliveTime,\/\/\u5f53\u7ebf\u7a0b\u6570\u5927\u4e8e\u6838\u5fc3\u7ebf\u7a0b\u6570\u65f6\uff0c\u591a\u4f59\u7684\u7a7a\u95f2\u7ebf\u7a0b\u5b58\u6d3b\u7684\u6700\u957f\u65f6\u95f4                              TimeUnit unit,\/\/\u65f6\u95f4\u5355\u4f4d                              BlockingQueue&lt;runnable&gt; workQueue,\/\/\u4efb\u52a1\u961f\u5217\uff0c\u7528\u6765\u50a8\u5b58\u7b49\u5f85\u6267\u884c\u4efb\u52a1\u7684\u961f\u5217                              ThreadFactory threadFactory,\/\/\u7ebf\u7a0b\u5de5\u5382\uff0c\u7528\u6765\u521b\u5efa\u7ebf\u7a0b\uff0c\u4e00\u822c\u9ed8\u8ba4\u5373\u53ef                              RejectedExecutionHandler handler\/\/\u62d2\u7edd\u7b56\u7565\uff0c\u5f53\u63d0\u4ea4\u7684\u4efb\u52a1\u8fc7\u591a\u800c\u4e0d\u80fd\u53ca\u65f6\u5904\u7406\u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u5b9a\u5236\u7b56\u7565\u6765\u5904\u7406\u4efb\u52a1                               ) {        if (corePoolSize &lt; 0 ||            maximumPoolSize &lt;= 0 ||            maximumPoolSize &lt; corePoolSize ||            keepAliveTime &lt; 0)            throw new IllegalArgumentException();        if (workQueue == null || threadFactory == null || handler == null)            throw new NullPointerException();        this.corePoolSize = corePoolSize;        this.maximumPoolSize = maximumPoolSize;        this.workQueue = workQueue;        this.keepAliveTime = unit.toNanos(keepAliveTime);        this.threadFactory = threadFactory;        this.handler = handler;    }&lt;\/runnable&gt;&lt;\/code&gt;&lt;\/pre&gt;<\/p>\n<p>&lt;strong&gt;\u4e0b\u9762\u8fd9\u4e9b\u5bf9\u521b\u5efa \u975e\u5e38\u91cd\u8981\uff0c\u5728\u540e\u9762\u4f7f\u7528\u7ebf\u7a0b\u6c60\u7684\u8fc7\u7a0b\u4e2d\u4f60\u4e00\u5b9a\u4f1a\u7528\u5230\uff01\u6240\u4ee5\uff0c\u52a1\u5fc5\u62ff\u7740\u5c0f\u672c\u672c\u8bb0\u6e05\u695a\u3002&lt;\/strong&gt;<\/p>\n<p>&lt;strong&gt;&lt;code&gt;ThreadPoolExecutor&lt;\/code&gt; 3 \u4e2a\u6700\u91cd\u8981\u7684\u53c2\u6570\uff1a&lt;\/strong&gt;<\/p>\n<p>&lt;ul&gt;&lt;li&gt;&lt;strong&gt;&lt;code&gt;corePoolSize&lt;\/code&gt; :&lt;\/strong&gt; \u6838\u5fc3\u7ebf\u7a0b\u6570\u7ebf\u7a0b\u6570\u5b9a\u4e49\u4e86\u6700\u5c0f\u53ef\u4ee5\u540c\u65f6\u8fd0\u884c\u7684\u7ebf\u7a0b\u6570\u91cf\u3002&lt;\/li&gt;&lt;li&gt;&lt;strong&gt;&lt;code&gt;maximumPoolSize&lt;\/code&gt; :&lt;\/strong&gt; \u5f53\u961f\u5217\u4e2d\u5b58\u653e\u7684\u4efb\u52a1\u8fbe\u5230\u961f\u5217\u5bb9\u91cf\u7684\u65f6\u5019\uff0c\u5f53\u524d\u53ef\u4ee5\u540c\u65f6\u8fd0\u884c\u7684\u7ebf\u7a0b\u6570\u91cf\u53d8\u4e3a\u6700\u5927\u7ebf\u7a0b\u6570\u3002&lt;\/li&gt;&lt;li&gt;&lt;strong&gt;&lt;code&gt;workQueue&lt;\/code&gt;:&lt;\/strong&gt; \u5f53\u65b0\u4efb\u52a1\u6765\u7684\u65f6\u5019\u4f1a\u5148\u5224\u65ad\u5f53\u524d\u8fd0\u884c\u7684\u7ebf\u7a0b\u6570\u91cf\u662f\u5426\u8fbe\u5230\u6838\u5fc3\u7ebf\u7a0b\u6570\uff0c\u5982\u679c\u8fbe\u5230\u7684\u8bdd\uff0c\u4fe1\u4efb\u5c31\u4f1a\u88ab\u5b58\u653e\u5728\u961f\u5217\u4e2d\u3002&lt;\/li&gt;&lt;\/ul&gt;<\/p>\n<p>&lt;code&gt;ThreadPoolExecutor&lt;\/code&gt;\u5176\u4ed6\u5e38\u89c1\u53c2\u6570:<\/p>\n<p>&lt;ol&gt;&lt;li&gt;&lt;strong&gt;&lt;code&gt;keepAliveTime&lt;\/code&gt;&lt;\/strong&gt;:\u5f53\u7ebf\u7a0b\u6c60\u4e2d\u7684\u7ebf\u7a0b\u6570\u91cf\u5927\u4e8e &lt;code&gt;corePoolSize&lt;\/code&gt; \u7684\u65f6\u5019\uff0c\u5982\u679c\u8fd9\u65f6\u6ca1\u6709\u65b0\u7684\u4efb\u52a1\u63d0\u4ea4\uff0c\u6838\u5fc3\u7ebf\u7a0b\u5916\u7684\u7ebf\u7a0b\u4e0d\u4f1a\u7acb\u5373\u9500\u6bc1\uff0c\u800c\u662f\u4f1a\u7b49\u5f85\uff0c\u76f4\u5230\u7b49\u5f85\u7684\u65f6\u95f4\u8d85\u8fc7\u4e86 &lt;code&gt;keepAliveTime&lt;\/code&gt;\u624d\u4f1a\u88ab\u56de\u6536\u9500\u6bc1\uff1b&lt;\/li&gt;&lt;li&gt;&lt;strong&gt;&lt;code&gt;unit&lt;\/code&gt;&lt;\/strong&gt; : &lt;code&gt;keepAliveTime&lt;\/code&gt; \u53c2\u6570\u7684\u65f6\u95f4\u5355\u4f4d\u3002&lt;\/li&gt;&lt;li&gt;&lt;strong&gt;&lt;code&gt;threadFactory&lt;\/code&gt;&lt;\/strong&gt; :executor \u521b\u5efa\u65b0\u7ebf\u7a0b\u7684\u65f6\u5019\u4f1a\u7528\u5230\u3002&lt;\/li&gt;&lt;li&gt;&lt;strong&gt;&lt;code&gt;handler&lt;\/code&gt;&lt;\/strong&gt; :\u9971\u548c\u7b56\u7565\u3002\u5173\u4e8e\u9971\u548c\u7b56\u7565\u4e0b\u9762\u5355\u72ec\u4ecb\u7ecd\u4e00\u4e0b\u3002&lt;\/li&gt;&lt;\/ol&gt;<\/p>\n<p>\u4e0b\u9762\u8fd9\u5f20\u56fe\u53ef\u4ee5\u52a0\u6df1\u4f60\u5bf9\u7ebf\u7a0b\u6c60\u4e2d\u5404\u4e2a\u53c2\u6570\u7684\u76f8\u4e92\u5173\u7cfb\u7684\u7406\u89e3\uff08\u56fe\u7247\u6765\u6e90\uff1a\u300aJava\u6027\u80fd\u8c03\u4f18\u5b9e\u6218\u300b\uff09\uff1a<\/p>\n<p>&lt;img src=&quot;&quot;https:\/\/my-blog-to-use.oss-cn-beijing.aliyuncs.com\/2019-7\/%E7%BA%BF%E7%A8%8B%E6%B1%A0%E5%90%84%E4%B8%AA%E5%8F%82%E6%95%B0%E7%9A%84%E5%85%B3%E7%B3%BB.jpg&quot;&quot; referrerpolicy=&quot;&quot;no-referrer&quot;&quot; alt=&quot;&quot;\u7ebf\u7a0b\u6c60\u5404\u4e2a\u53c2\u6570\u7684\u5173\u7cfb&quot;&quot; style=&quot;&quot;width: 100%;&quot;&quot;&gt;<\/p>\n<p>&lt;strong&gt;&lt;code&gt;ThreadPoolExecutor&lt;\/code&gt; \u9971\u548c\u7b56\u7565\u5b9a\u4e49:&lt;\/strong&gt;<\/p>\n<p>\u5982\u679c\u5f53\u524d\u540c\u65f6\u8fd0\u884c\u7684\u7ebf\u7a0b\u6570\u91cf\u8fbe\u5230\u6700\u5927\u7ebf\u7a0b\u6570\u91cf\u5e76\u4e14\u961f\u5217\u4e5f\u5df2\u7ecf\u88ab\u653e\u6ee1\u4e86\u4efb\u65f6\uff0c&lt;code&gt;ThreadPoolTaskExecutor&lt;\/code&gt; \u5b9a\u4e49\u4e00\u4e9b\u7b56\u7565:<\/p>\n<p>&lt;ul&gt;&lt;li&gt;&lt;strong&gt;&lt;code&gt;ThreadPoolExecutor.AbortPolicy&lt;\/code&gt;&lt;\/strong&gt;\uff1a\u629b\u51fa &lt;code&gt;RejectedExecutionException&lt;\/code&gt;\u6765\u62d2\u7edd\u65b0\u4efb\u52a1\u7684\u5904\u7406\u3002&lt;\/li&gt;&lt;li&gt;&lt;strong&gt;&lt;code&gt;ThreadPoolExecutor.CallerRunsPolicy&lt;\/code&gt;&lt;\/strong&gt;\uff1a\u8c03\u7528\u6267\u884c\u81ea\u5df1\u7684\u7ebf\u7a0b\u8fd0\u884c\u4efb\u52a1\uff0c\u4e5f\u5c31\u662f\u76f4\u63a5\u5728\u8c03\u7528&lt;code&gt;execute&lt;\/code&gt;\u65b9\u6cd5\u7684\u7ebf\u7a0b\u4e2d\u8fd0\u884c(&lt;code&gt;run&lt;\/code&gt;)\u88ab\u62d2\u7edd\u7684\u4efb\u52a1\uff0c\u5982\u679c\u6267\u884c\u7a0b\u5e8f\u5df2\u5173\u95ed\uff0c\u5219\u4f1a\u4e22\u5f03\u8be5\u4efb\u52a1\u3002\u56e0\u6b64\u8fd9\u79cd\u7b56\u7565\u4f1a\u964d\u4f4e\u5bf9\u4e8e\u65b0\u4efb\u52a1\u63d0\u4ea4\u901f\u5ea6\uff0c\u5f71\u54cd\u7a0b\u5e8f\u7684\u6574\u4f53\u6027\u80fd\u3002\u53e6\u5916\uff0c\u8fd9\u4e2a\u7b56\u7565\u559c\u6b22\u589e\u52a0\u961f\u5217\u5bb9\u91cf\u3002\u5982\u679c\u60a8\u7684\u5e94\u7528\u7a0b\u5e8f\u53ef\u4ee5\u627f\u53d7\u6b64\u5ef6\u8fdf\u5e76\u4e14\u4f60\u4e0d\u80fd\u4efb\u52a1\u4e22\u5f03\u4efb\u4f55\u4e00\u4e2a\u4efb\u52a1\u8bf7\u6c42\u7684\u8bdd\uff0c\u4f60\u53ef\u4ee5\u9009\u62e9\u8fd9\u4e2a\u7b56\u7565\u3002&lt;\/li&gt;&lt;li&gt;&lt;strong&gt;&lt;code&gt;ThreadPoolExecutor.DiscardPolicy&lt;\/code&gt;\uff1a&lt;\/strong&gt; \u4e0d\u5904\u7406\u65b0\u4efb\u52a1\uff0c\u76f4\u63a5\u4e22\u5f03\u6389\u3002&lt;\/li&gt;&lt;li&gt;&lt;strong&gt;&lt;code&gt;ThreadPoolExecutor.DiscardOldestPolicy&lt;\/code&gt;\uff1a&lt;\/strong&gt; \u6b64\u7b56\u7565\u5c06\u4e22\u5f03\u6700\u65e9\u7684\u672a\u5904\u7406\u7684\u4efb\u52a1\u8bf7\u6c42\u3002&lt;\/li&gt;&lt;\/ul&gt;<\/p>\n<p>\u4e3e\u4e2a\u4f8b\u5b50\uff1a<\/p>\n<p>&lt;blockquote&gt;&lt;p&gt;Spring \u901a\u8fc7 &lt;code&gt;ThreadPoolTaskExecutor&lt;\/code&gt; \u6216\u8005\u6211\u4eec\u76f4\u63a5\u901a\u8fc7 &lt;code&gt;ThreadPoolExecutor&lt;\/code&gt; \u7684\u6784\u9020\u51fd\u6570\u521b\u5efa\u7ebf\u7a0b\u6c60\u7684\u65f6\u5019\uff0c\u5f53\u6211\u4eec\u4e0d\u6307\u5b9a &lt;code&gt;RejectedExecutionHandler&lt;\/code&gt; \u9971\u548c\u7b56\u7565\u7684\u8bdd\u6765\u914d\u7f6e\u7ebf\u7a0b\u6c60\u7684\u65f6\u5019\u9ed8\u8ba4\u4f7f\u7528\u7684\u662f &lt;code&gt;ThreadPoolExecutor.AbortPolicy&lt;\/code&gt;\u3002\u5728\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c&lt;code&gt;ThreadPoolExecutor&lt;\/code&gt; \u5c06\u629b\u51fa &lt;code&gt;RejectedExecutionException&lt;\/code&gt; \u6765\u62d2\u7edd\u65b0\u6765\u7684\u4efb\u52a1 \uff0c\u8fd9\u4ee3\u8868\u4f60\u5c06\u4e22\u5931\u5bf9\u8fd9\u4e2a\u4efb\u52a1\u7684\u5904\u7406\u3002 \u5bf9\u4e8e\u53ef\u4f38\u7f29\u7684\u5e94\u7528\u7a0b\u5e8f\uff0c\u5efa\u8bae\u4f7f\u7528 &lt;code&gt;ThreadPoolExecutor.CallerRunsPolicy&lt;\/code&gt;\u3002\u5f53\u6700\u5927\u6c60\u88ab\u586b\u6ee1\u65f6\uff0c\u6b64\u7b56\u7565\u4e3a\u6211\u4eec\u63d0\u4f9b\u53ef\u4f38\u7f29\u961f\u5217\u3002\uff08\u8fd9\u4e2a\u76f4\u63a5\u67e5\u770b &lt;code&gt;ThreadPoolExecutor&lt;\/code&gt; \u7684\u6784\u9020\u51fd\u6570\u6e90\u7801\u5c31\u53ef\u4ee5\u770b\u51fa\uff0c\u6bd4\u8f83\u7b80\u5355\u7684\u539f\u56e0\uff0c\u8fd9\u91cc\u5c31\u4e0d\u8d34\u4ee3\u7801\u4e86\u3002\uff09&lt;\/p&gt;&lt;\/blockquote&gt;<\/p>\n<p>&lt;pre&gt;&lt;code&gt;            &quot;&#8220;`<br \/>\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>&#8220;`&#8221; \u7ebf\u7a0b\u6c60\u5b9e\u73b0\u7c7b ThreadPoolExecutor \u662f Executor \u6846\u67b6 [&hellip;]<\/p>\n","protected":false},"author":7,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-37492","post","type-post","status-publish","format-standard","hentry","category-c"],"_links":{"self":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/37492","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/comments?post=37492"}],"version-history":[{"count":1,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/37492\/revisions"}],"predecessor-version":[{"id":37493,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/37492\/revisions\/37493"}],"wp:attachment":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/media?parent=37492"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/categories?post=37492"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/tags?post=37492"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}