{"id":597,"date":"2020-05-30T18:56:33","date_gmt":"2020-05-30T10:56:33","guid":{"rendered":"http:\/\/weitaoc.com\/?p=597"},"modified":"2020-05-30T19:05:46","modified_gmt":"2020-05-30T11:05:46","slug":"%e4%bd%a0%e7%9c%9f%e7%9a%84%e4%ba%86%e8%a7%a3threadpoolexecutor%e5%90%97-%e9%98%9f%e5%88%97%e7%ad%96%e7%95%a5","status":"publish","type":"post","link":"http:\/\/weitaoc.com\/?p=597","title":{"rendered":"\u4f60\u771f\u7684\u4e86\u89e3ThreadPoolExecutor\u5417-\u961f\u5217\u7b56\u7565"},"content":{"rendered":"\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>\u4f60\u771f\u7684\u4e86\u89e3ThreadPoolExecutor\u5417<\/p><cite> ThreadPoolExecutor \u7f51\u4e0a\u7684\u8bb2\u89e3\u7279\u522b\u591a\uff0c\u4e3b\u8981\u662f\u9488\u5bf9\u5404\u4e2a\u5165\u53c2\u7684\u8bb2\u89e3\uff0c\u4f46\u662f\u6709\u4e00\u4e2a\u975e\u5e38\u91cd\u8981\u70b9\u88ab\u9057\u6f0f\u4e86\uff0c\u90a3\u5c31\u662f\u961f\u5217\u7684\u4e09\u79cd\u7c7b\u578b\uff0c\u6bcf\u4e2a\u7c7b\u578b\u7684\u5b9a\u4e49\u90fd\u975e\u5e38\u54df\u4ee3\u8868\u6027\uff0c\u8bd5\u7528\u7684\u573a\u666f\u4e5f\u662f\u975e\u5e38\u6709\u6307\u5411\u6027\u7684\u3002\u6240\u4ee5\u4e86\u89e3\u8fd9\u4e09\u79cd\u7c7b\u578b\u975e\u5e38\u6709\u5fc5\u8981\u3002<\/cite><\/blockquote>\n\n\n\n<p>\u5728\u5f00\u7bc7\uff0c\u5728\u91cd\u65b0\u63cf\u8ff0\u4e00\u4e0b ThreadPoolExecutor\u6784\u9020\u65b9\u6cd5\u7684\u51e0\u4e2a\u5165\u53c2\u548c\u542b\u4e49\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, \nlong keepAliveTime, TimeUnit unit, \nBlockingQueue&lt;Runnable> workQueue, ThreadFactory threadFactory, RejectedExecutionHandler handler)<\/pre>\n\n\n\n<p><strong>corePoolSize <\/strong>&#8211; \u6c60\u4e2d\u6240\u4fdd\u5b58\u7684\u7ebf\u7a0b\u6570\uff0c\u5305\u62ec\u7a7a\u95f2\u7ebf\u7a0b\u3002 <\/p>\n\n\n\n<p><strong>maximumPoolSize<\/strong>-\u6c60\u4e2d\u5141\u8bb8\u7684\u6700\u5927\u7ebf\u7a0b\u6570\u3002 <\/p>\n\n\n\n<p><strong>keepAliveTime <\/strong>&#8211; \u5f53\u7ebf\u7a0b\u6570\u5927\u4e8e\u6838\u5fc3\u65f6\uff0c\u6b64\u4e3a\u7ec8\u6b62\u524d\u591a\u4f59\u7684\u7a7a\u95f2\u7ebf\u7a0b\u7b49\u5f85\u65b0\u4efb\u52a1\u7684 \u6700\u957f\u65f6\u95f4\u3002 <\/p>\n\n\n\n<p><strong>unit <\/strong>&#8211; keepAliveTime \u53c2\u6570\u7684\u65f6\u95f4\u5355\u4f4d\u3002<\/p>\n\n\n\n<p><strong>workQueue <\/strong>&#8211; \u6267\u884c\u524d\u7528\u4e8e\u4fdd\u6301\u4efb\u52a1\u7684\u961f\u5217\u3002\u6b64\u961f\u5217\u4ec5\u4fdd\u6301\u7531execute\u65b9\u6cd5\u63d0\u4ea4 \u7684 Runnable\u4efb\u52a1\u3002<\/p>\n\n\n\n<p><strong>threadFactory <\/strong>&#8211; \u6267\u884c\u7a0b\u5e8f\u521b\u5efa\u65b0\u7ebf\u7a0b\u65f6\u4f7f\u7528\u7684\u5de5\u5382\u3002 <\/p>\n\n\n\n<p><strong>handler <\/strong>&#8211; \u7531\u4e8e\u8d85\u51fa\u7ebf\u7a0b\u8303\u56f4\u548c\u961f\u5217\u5bb9\u91cf\u800c\u4f7f\u6267\u884c\u88ab\u963b\u585e\u65f6\u6240\u4f7f\u7528\u7684\u5904\u7406\u7a0b\u5e8f\u3002 ThreadPoolExecutor\u662f Executors \u7c7b\u7684\u5e95\u5c42\u5b9e\u73b0\u3002 <\/p>\n\n\n\n<p>\u6211\u4eec\u4e00\u822c\u6765\u8bb2\uff0c\u65b0\u7684\u8bf7\u6c42\u8fc7\u6765\uff0c\u4f1a\u521b\u5efa\u6838\u5fc3\u7ebf\u7a0b\u6570\uff0c\u5982\u679c\u6838\u5fc3\u7ebf\u7a0b\u6570\u6ee1\u4e86\uff0c\u5c31\u4f1a\u8fdb\u5165\u961f\u5217\uff0c\u5982\u679c\u961f\u5217\u4e5f\u6ee1\u4e86\uff0c\u5c31\u4f1a\u521b\u5efa\u65b0\u7684\u7ebf\u7a0b\uff0c\u76f4\u5230\u8fbe\u5230\u8bbe\u5b9a\u7684\u6700\u5927\u7ebf\u7a0b\u6570\u3002\u5982\u679c\u6700\u5927\u7ebf\u7a0b\u6570\u548c\u961f\u5217\u90fd\u6162\u4e86\uff0c\u5219\u4f1a\u6267\u884c\u6307\u5b9a\u7684\u5f02\u5e38\u5904\u7406\u7a0b\u5e8f\u3002<\/p>\n\n\n\n<p>\u4e0d\u7ba1\u54ea\u79cd\u7b56\u7565\u90fd\u5fc5\u987b\u9075\u5faa\u4e0a\u8ff0\u6d41\u7a0b\uff0c\u8fd9\u4e5f\u662f\u4e0b\u6587\u8bb2\u8ff0\u961f\u5217\u7b56\u7565\u7684\u57fa\u7840\u3002<\/p>\n\n\n\n<p><strong>1.\u76f4\u63a5\u63d0\u4ea4<\/strong>\u3002<strong>SynchronousQueue<\/strong>\u3002\u4ed6\u4f1a\u5c06\u4efb\u52a1\u76f4\u63a5\u63d0\u4ea4\u7ed9\u7ebf\u7a0b\uff0c\u4e0d\u4fdd\u6301\u8bf7\u6c42\u3002\u5982\u679c\u7ebf\u7a0b\u6570\u4e0d\u591f\uff0c\u5219\u4f1a\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u7ebf\u7a0b\u3002\u6240\u4ee5\u5728\u6b64\u65f6\uff0c\u5982\u679c\u8bbe\u7f6e\u4e86\u6709\u754c\u7684maximumPoolSize\uff0c\u90a3\u4e48\u521b\u5efa\u5931\u8d25\u7684\u53ef\u80fd\u6027\u5c31\u975e\u5e38\u5927\u4e86\u3002 \u5982\u679c\u9009\u62e9\u4e86\u8fd9\u79cd\u7b56\u7565\uff0c\u90a3\u4e48 maximumPoolSize\u6700\u597d\u8bbe\u7f6e\u6210\u65e0\u9650\u5927\u3002<\/p>\n\n\n\n<p>\u6211\u4eec\u6765\u770b\u4e00\u4e0b ThreadPoolExecutor \u91cc\u7684\u4e00\u6bb5\u4ee3\u7801<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/**\n * Create the BlockingQueue to use for the ThreadPoolExecutor.\n * &lt;p>A LinkedBlockingQueue instance will be created for a positive\n * capacity value; a SynchronousQueue else.\n * @param queueCapacity the specified queue capacity\n * @return the BlockingQueue instance\n * @see java.util.concurrent.LinkedBlockingQueue\n * @see java.util.concurrent.SynchronousQueue\n *\/\nprotected BlockingQueue&lt;Runnable> createQueue(int queueCapacity) {\n   if (queueCapacity > 0) {\n      return new LinkedBlockingQueue&lt;Runnable>(queueCapacity);\n   }\n   else {\n      return new SynchronousQueue&lt;Runnable>();\n   }\n}<\/pre>\n\n\n\n<p>\u8fd9\u662f\u521b\u5efa\u961f\u5217\u7684\u4e00\u4e2a\u65b9\u6cd5\uff0c\u5f53\u4f60\u9884\u8bbe\u7684\u961f\u5217\u5bb9\u91cf\u5c0f\u4e8e\u7b49\u4e8e0\u65f6\uff0c\u90a3\u4e48\u5c31\u662f\u7528SynchronousQueue\u961f\u5217\u7b56\u7565\u3002\u5426\u5219\u4f7f\u7528LinkedBlockingQueue\u3002\u4e3a\u4ec0\u4e48\u5462\uff1f\u56e0\u4e3a SynchronousQueue\u662f\u76f4\u63a5\u63d0\u4ea4\u7684\uff0c\u4ed6\u4e0d\u9700\u8981\u4fdd\u6301\u8bf7\u6c42\u3002<\/p>\n\n\n\n<p><strong>2.\u65e0\u754c\u961f\u5217<\/strong>\u3002\u6bd4\u5982\u524d\u9762\u63d0\u5230\u7684<strong>LinkedBlockingQueue<\/strong>\u5c31\u662f\u65e0\u89e3\u961f\u5217\u3002\u5982\u679c\u4e0d\u624b\u52a8\u6307\u5b9a\u5bb9\u91cf\uff0c\u4ed6\u7684\u6700\u5927\u5bb9\u91cf\u662f <strong>Integer.MAX_VALUE<\/strong>\uff0c\u6240\u4ee5\u4e00\u822c\u4e5f\u5b9a\u4e49\u4e3a\u65e0\u754c\u961f\u5217\u3002\u524d\u9762\u4e5f\u8bf4\u5230\u4e86\uff0c\u5982\u679c\u961f\u5217\u6ee1\u4e86\uff0c\u5c31\u4f1a\u521b\u5efa\u65b0\u7684\u7ebf\u7a0b\uff0c\u76f4\u5230\u5230\u8fbe\u6700\u5927\u7ebf\u7a0b\u6570\u3002\u5982\u679c\u6211\u4eec\u8bbe\u7f6e\u4e86\u4e00\u4e2a\u65e0\u754c\u961f\u5217\uff0c\u90a3\u4e48\u6700\u5927\u7ebf\u7a0b\u6570\uff0c\u662f\u6ca1\u6709\u610f\u4e49\u7684\uff0c\u4ed6\u4f1a\u5c06\u8bf7\u6c42\u4e00\u76f4\u5b58\u653e\u5728\u65e0\u754c\u961f\u5217\uff0c\u7b49\u5f85\u6838\u5fc3\u7ebf\u7a0b\u91ca\u653e\u3002\u5f53\u7136\u8fd9\u79cd\u5b9e\u9645\u573a\u666f\u5e76\u4e0d\u5e38\u89c1\u3002<\/p>\n\n\n\n<p><strong>3.\u6709\u754c\u961f\u5217<\/strong>\u3002<strong>ArrayBlockingQueue<\/strong>\uff0c\u6211\u4eec\u5728\u521b\u5efa\u7ebf\u7a0b\u6c60\u65f6\uff0c\u5982\u679c\u9009\u62e9\u8fd9\u79cd\u961f\u5217\u7b56\u7565\uff0c\u90a3\u4e48\u5fc5\u987b\u5236\u5b9a\u961f\u5217\u7684\u5927\u5c0f\uff0c\u6240\u4ee5\u53eb\u6709\u754c\u961f\u5217\u3002\u8fd9\u79cd\u4f7f\u7528\u573a\u666f\uff0c\u4e00\u822c\u662f\u9632\u6b62\u8d44\u6e90\u8017\u5c3d\u3002\u8fd9\u79cd\u4e00\u822c\u6bd4\u8f83\u9002\u7528\u4e8e\u786c\u4ef6\u914d\u7f6e\u4e0d\u9ad8\uff0c\u4e14\u5bf9\u4e8e\u7a0b\u5e8f\u7684\u5904\u7406\u5931\u8d25\u6709\u4e00\u5b9a\u7684\u5bb9\u5fcd\u3002<\/p>\n\n\n\n<p>\u4e00\u822c\u60c5\u51b5\u4e0b\uff0c\u5728\u6211\u4eec\u7684\u4f7f\u7528\u573a\u666f\u4e2d\u7528\u5230\u7684\u6700\u591a\u7684\uff0c\u5bb9\u9519\u6027\u6700\u5f3a\u7684\uff0c\u662f\u521d\u59cb\u5316\u4e00\u4e2a\u6709\u9650\u5bb9\u91cf\u961f\u5217\u7684\u65e0\u754c\u961f\u5217\u7684\u7ebf\u7a0b\u6c60\uff0c\u8fd9\u79cd\u7ebf\u7a0b\u6c60\u7684\u5f39\u6027\u6700\u597d\uff0c\u5bb9\u9519\u6027\u4e5f\u6bd4\u8f83\u5f3a\u3002\u4f46\u662f\uff0c\u524d\u63d0\u6761\u4ef6\u662f\u9700\u8981\u9884\u4f30\u597d\u6700\u5927\u7ebf\u7a0b\u6570\u548c\u961f\u5217\u957f\u5ea6\u3002\u5177\u4f53\u65b9\u6848\uff0c\u5177\u4f53\u5206\u6790\uff0c\u8fd9\u91cc\u5c31\u4e0d\u505a\u5206\u6790\u3002<\/p>\n\n\n\n<p>\u540e\u9762\u6709\u65f6\u95f4\uff0c\u518d\u628a\u8fd9\u4e09\u79cd\u7b56\u7565\u7684\u5b9e\u73b0\u65b9\u5f0f\u5199\u4e00\u5199\u3002<\/p>\n<p>\u8f6c\u8f7d\u8bf7\u6ce8\u660e\uff1a<a href=\"http:\/\/weitaoc.com\">\u8ff7\u8def\u7684\u8001\u9f20<\/a> &raquo; <a href=\"http:\/\/weitaoc.com\/?p=597\">\u4f60\u771f\u7684\u4e86\u89e3ThreadPoolExecutor\u5417-\u961f\u5217\u7b56\u7565<\/a><\/p>","protected":false},"excerpt":{"rendered":"<p>\u4f60\u771f\u7684\u4e86\u89e3ThreadPoolExecutor\u5417 ThreadPoolExecutor \u7f51\u4e0a\u7684\u8bb2\u89e3\u7279\u522b\u591a\uff0c\u4e3b\u8981\u662f\u9488\u5bf9\u5404\u4e2a\u5165\u53c2\u7684\u8bb2\u89e3\uff0c\u4f46\u662f\u6709\u4e00\u4e2a\u975e\u5e38\u91cd\u8981\u70b9\u88ab\u9057\u6f0f\u4e86\uff0c\u90a3\u5c31\u662f\u961f\u5217\u7684\u4e09\u79cd\u7c7b\u578b\uff0c\u6bcf\u4e2a\u7c7b\u578b\u7684\u5b9a\u4e49\u90fd\u975e\u5e38\u54df\u4ee3\u8868\u6027\uff0c\u8bd5\u7528\u7684\u573a\u666f\u4e5f\u662f\u975e\u5e38\u6709\u6307\u5411\u6027\u7684\u3002\u6240\u4ee5\u4e86 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[57],"tags":[67],"class_list":["post-597","post","type-post","status-publish","format-standard","hentry","category-java","tag-67"],"views":10726,"_links":{"self":[{"href":"http:\/\/weitaoc.com\/index.php?rest_route=\/wp\/v2\/posts\/597","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/weitaoc.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/weitaoc.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/weitaoc.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/weitaoc.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=597"}],"version-history":[{"count":5,"href":"http:\/\/weitaoc.com\/index.php?rest_route=\/wp\/v2\/posts\/597\/revisions"}],"predecessor-version":[{"id":603,"href":"http:\/\/weitaoc.com\/index.php?rest_route=\/wp\/v2\/posts\/597\/revisions\/603"}],"wp:attachment":[{"href":"http:\/\/weitaoc.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=597"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/weitaoc.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=597"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/weitaoc.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=597"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}