{"id":1793,"date":"2025-12-30T08:06:06","date_gmt":"2025-12-30T00:06:06","guid":{"rendered":"https:\/\/eve2333.top\/?p=1793"},"modified":"2025-12-30T08:06:06","modified_gmt":"2025-12-30T00:06:06","slug":"%e5%8a%a8%e6%80%81%e7%ba%bf%e7%a8%8b%e6%b1%a0onethread%e7%b3%bb%e7%bb%9f-%e7%ac%ac%e5%8d%81%e4%ba%94%e9%83%a8%e5%88%86-%e5%9f%ba%e4%ba%8etomcat%e7%ad%89web%e5%ae%b9%e5%99%a8%e7%ba%bf","status":"publish","type":"post","link":"https:\/\/eve2333.top\/?p=1793","title":{"rendered":"\u52a8\u6001\u7ebf\u7a0b\u6c60oneThread\u7cfb\u7edf \u2014 \u7b2c\u5341\u4e94\u90e8\u5206\u00a0\u57fa\u4e8eTomcat\u7b49Web\u5bb9\u5668\u7ebf\u7a0b\u6c60\u9002\u914d"},"content":{"rendered":"\n<p>\u8fd9\u662f\u4e00\u4e2a\u975e\u5e38\u786c\u6838\u4e14\u5b9e\u7528\u7684\u6280\u672f\u67b6\u6784\u6587\u6863\uff0c\u4e3b\u8981\u89e3\u51b3\u4e86 SpringBoot \u5e94\u7528\u5728\u751f\u4ea7\u73af\u5883\u4e2d<strong>Web \u5bb9\u5668\uff08Tomcat\/Jetty\uff09\u7ebf\u7a0b\u6c60\u65e0\u6cd5\u52a8\u6001\u8c03\u6574<\/strong>\u7684\u75db\u70b9\u3002<\/p>\n\n\n\n<p>\u8003\u8651\u5230\u5185\u5bb9\u6d89\u53ca<strong>\u539f\u7406\u673a\u5236\u3001\u67b6\u6784\u8bbe\u8ba1\u3001\u6e90\u7801\u5b9e\u73b0<\/strong>\u4e09\u4e2a\u5c42\u9762\uff0c\u4e3a\u4e86\u8ba9\u4f60\u5f7b\u5e95\u901a\u900f\u5730\u7406\u89e3\uff0c\u6211\u5c06\u5185\u5bb9\u62c6\u89e3\u4e3a<strong>\u4e09\u7bc7<\/strong>\u8fdb\u884c\u8be6\u7ec6\u8bb2\u89e3\u3002<\/p>\n\n\n\n<p>\u8fd9\u662f<strong>\u7b2c\u4e00\u7bc7<\/strong>\uff0c\u6211\u4eec\u5c06\u805a\u7126\u4e8e<strong>\u6838\u5fc3\u75db\u70b9\u3001\u8bf7\u6c42\u5904\u7406\u94fe\u8def\u4ee5\u53ca Tomcat \u4e0e Jetty \u7ebf\u7a0b\u6c60\u7684\u5e95\u5c42\u5dee\u5f02<\/strong>\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\u7b2c\u4e00\u7bc7\uff1a\u6838\u5fc3\u80cc\u666f\u4e0e Web \u5bb9\u5668\u7ebf\u7a0b\u6c60\u5e95\u5c42\u539f\u7406<\/h1>\n\n\n\n<h2 class=\"wp-block-heading\">1. \u4e3a\u4ec0\u4e48\u6211\u4eec\u9700\u8981\u52a8\u6001\u8c03\u6574 Web \u7ebf\u7a0b\u6c60\uff1f<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">1.1 \u75db\u70b9\u573a\u666f\u590d\u73b0<\/h3>\n\n\n\n<p>\u6587\u6863\u5f00\u7bc7\u63d0\u5230\u7684\u573a\u666f\u975e\u5e38\u7ecf\u5178\uff1a<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\u5927\u4fc3\u671f\u95f4\uff0c\u8ba2\u5355\u670d\u52a1\u8d85\u65f6\uff0cCPU\/DB \u6b63\u5e38\uff0c\u4f46\u8bf7\u6c42\u5728\u6392\u961f\u3002<\/p>\n<\/blockquote>\n\n\n\n<p><strong>\u539f\u56e0\u5206\u6790\uff1a<\/strong><br>SpringBoot \u5185\u7f6e\u7684 Tomcat \u9ed8\u8ba4\u6700\u5927\u7ebf\u7a0b\u6570\u901a\u5e38\u662f 200\uff08<code>server.tomcat.threads.max<\/code>\uff09\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u5e73\u65f6\uff1a<\/strong> \u6d41\u91cf\u5c0f\uff0c200 \u4e2a\u7ebf\u7a0b\u7ef0\u7ef0\u6709\u4f59\u3002<\/li>\n\n\n\n<li><strong>\u5927\u4fc3\uff1a<\/strong> \u6d41\u91cf\u77ac\u95f4\u7206\u53d1\uff08\u5982 QPS \u4ece 100 \u98d9\u5347\u5230 2000\uff09\uff0c200 \u4e2a\u7ebf\u7a0b\u5904\u7406\u4e0d\u8fc7\u6765\uff0c\u540e\u7eed\u8bf7\u6c42\u53ea\u80fd\u5728\u961f\u5217\u91cc\u7b49\u5f85\uff0c\u751a\u81f3\u8d85\u65f6\u62a5\u9519\u3002<\/li>\n<\/ul>\n\n\n\n<p><strong>\u4f20\u7edf\u89e3\u51b3\u5c34\u5c2c\uff1a<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u6539\u914d\u7f6e\u91cd\u542f\uff1a<\/strong> \u4fee\u6539 <code>application.yml<\/code> -> \u91cd\u542f\u670d\u52a1\u3002\n<ul class=\"wp-block-list\">\n<li><em>\u4ee3\u4ef7\uff1a<\/em> \u670d\u52a1\u4e2d\u65ad\uff0c\u91cd\u542f\u671f\u95f4\u6d41\u91cf\u4e22\u5931\uff0c\u751a\u81f3\u5f15\u8d77\u96c6\u7fa4\u96ea\u5d29\u3002<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>\u9884\u8bbe\u8fc7\u5927\uff1a<\/strong> \u5e73\u65f6\u5c31\u5f00 1000 \u4e2a\u7ebf\u7a0b\u3002\n<ul class=\"wp-block-list\">\n<li><em>\u4ee3\u4ef7\uff1a<\/em> \u6d6a\u8d39\u5185\u5b58\uff0c\u4e0a\u4e0b\u6587\u5207\u6362\uff08Context Switch\uff09\u5f00\u9500\u5927\uff0cCPU \u865a\u9ad8\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<p><strong>\u7ed3\u8bba\uff1a<\/strong> \u6211\u4eec\u9700\u8981\u50cf\u201c\u8c03\u8282\u97f3\u91cf\u201d\u4e00\u6837\uff0c\u5728\u670d\u52a1\u8fd0\u884c\u65f6\u52a8\u6001\u4fee\u6539\u7ebf\u7a0b\u6c60\u53c2\u6570\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1.2 Web \u5bb9\u5668\u7ebf\u7a0b\u6c60\u5728\u94fe\u8def\u4e2d\u7684\u4f4d\u7f6e<\/h3>\n\n\n\n<p>SpringBoot Web \u5bb9\u5668\u7ebf\u7a0b\u6c60\u662f Web \u670d\u52a1\u5668\uff08\u5982Tomcat\u3001Jetty\u3001Undertow\uff09\u7528\u6765\u5904\u7406 HTTP \u8bf7\u6c42\u7684\u6838\u5fc3\u7ebf\u7a0b\u6c60\u3002\u5f53\u5ba2\u6237\u7aef\u53d1\u8d77 HTTP \u8bf7\u6c42\u65f6\uff0cWeb \u5bb9\u5668\u4f1a\u4ece\u7ebf\u7a0b\u6c60\u4e2d\u5206\u914d\u4e00\u4e2a\u5de5\u4f5c\u7ebf\u7a0b\u6765\u5904\u7406\u8fd9\u4e2a\u8bf7\u6c42\uff0c\u5305\u62ec\u89e3\u6790 HTTP \u534f\u8bae\u3001\u8c03\u7528 SpringMVC \u63a7\u5236\u5668\u3001\u8fd4\u56de\u54cd\u5e94\u7b49\u5168\u8fc7\u7a0b\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"654\" src=\"https:\/\/eve2333.top\/wp-content\/uploads\/2025\/12\/1767047879-image-1024x654.png\" alt=\"\" class=\"wp-image-1796\" srcset=\"https:\/\/eve2333.top\/wp-content\/uploads\/2025\/12\/1767047879-image-1024x654.png 1024w, https:\/\/eve2333.top\/wp-content\/uploads\/2025\/12\/1767047879-image-300x192.png 300w, https:\/\/eve2333.top\/wp-content\/uploads\/2025\/12\/1767047879-image-768x490.png 768w, https:\/\/eve2333.top\/wp-content\/uploads\/2025\/12\/1767047879-image-1536x981.png 1536w, https:\/\/eve2333.top\/wp-content\/uploads\/2025\/12\/1767047879-image.png 1568w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><strong>Web \u5bb9\u5668\u7ebf\u7a0b\u6c60\u662f\u6d41\u91cf\u7684\u201c\u7b2c\u4e00\u9053\u9600\u95e8\u201d\u3002<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u8fde\u63a5\u5668\uff08Connector\uff09\uff1a<\/strong> \u63a5\u6536 TCP \u8fde\u63a5\u3002<\/li>\n\n\n\n<li><strong>\u7ebf\u7a0b\u6c60\uff08\u6838\u5fc3\u74f6\u9888\uff09\uff1a<\/strong> \u8fd9\u91cc\u662f HTTP \u89e3\u6790\u548cServlet \u8c03\u7528\u7684\u8d77\u70b9\u3002\n<ul class=\"wp-block-list\">\n<li>\u5982\u679c\u8fd9\u91cc\u5835\u4e86\uff0c\u540e\u7eed\u7684 Controller\u3001Service\u3001DB \u6027\u80fd\u518d\u597d\u4e5f\u6ca1\u7528\uff0c\u56e0\u4e3a\u8bf7\u6c42\u6839\u672c\u8fdb\u4e0d\u6765\u3002<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>\u4e1a\u52a1\u903b\u8f91\uff1a<\/strong> \u624d\u662f\u6211\u4eec\u5e73\u65f6\u5199\u7684 <code>@RequestMapping<\/code> \u4ee3\u7801\u3002<\/li>\n<\/ol>\n\n\n\n<p>\u5728\u4f20\u7edf\u7684 SpringBoot \u5e94\u7528\u4e2d\uff0cWeb \u5bb9\u5668\u7ebf\u7a0b\u6c60\u7684\u914d\u7f6e\u901a\u5e38\u901a\u8fc7 <code>application.yml<\/code> \u6587\u4ef6\u8fdb\u884c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>server:tomcat:\n &nbsp;  threads:\n &nbsp; &nbsp;  max: 200 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# \u6700\u5927\u7ebf\u7a0b\u6570\n &nbsp; &nbsp;  min-spare: 10 &nbsp; &nbsp; &nbsp; # \u6700\u5c0f\u7a7a\u95f2\u7ebf\u7a0b\u6570\n &nbsp;  accept-count: 100 &nbsp; &nbsp; # \u961f\u5217\u5bb9\u91cf\n &nbsp;  max-connections:8192# \u6700\u5927\u8fde\u63a5\u6570<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">2. \u6df1\u5ea6\u89e3\u6790\uff1aTomcat \u4e0e Jetty \u7684\u7ebf\u7a0b\u6c60\u5dee\u5f02<\/h2>\n\n\n\n<p>\u8fd9\u662f\u5b9e\u73b0\u52a8\u6001\u8c03\u53c2\u6700\u96be\u7684\u5730\u65b9\uff0c\u56e0\u4e3a SpringBoot \u5c4f\u853d\u4e86\u5e95\u5c42\u7ec6\u8282\uff0c\u4f46 <code>oneThread<\/code> \u5fc5\u987b\u7a7f\u900f\u5c4f\u853d\u5c42\u53bb\u64cd\u4f5c\u5e95\u5c42\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">2.1 Tomcat \u7ebf\u7a0b\u6c60\u539f\u7406\uff08\u201c\u6025\u813e\u6c14\u201d\u7b56\u7565\uff09<\/h3>\n\n\n\n<p>Tomcat \u4f7f\u7528\u7684\u662f\u6269\u5c55\u81ea JDK <code>ThreadPoolExecutor<\/code> \u7684\u5b9e\u73b0\uff0c\u4f46\u5b83\u4fee\u6539\u4e86\u6838\u5fc3\u903b\u8f91\u3002<\/p>\n\n\n\n<p><strong>JDK \u6807\u51c6\u7ebf\u7a0b\u6c60\u903b\u8f91\uff1a<\/strong><br>\u6838\u5fc3\u7ebf\u7a0b\u6ee1 -&gt; <strong>\u5148\u5165\u961f\u5217<\/strong> -&gt; \u961f\u5217\u6ee1 -&gt; <strong>\u624d\u521b\u5efa\u65b0\u7ebf\u7a0b<\/strong>\uff08\u76f4\u5230\u6700\u5927\u503c\uff09\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><em>\u7f3a\u70b9\uff1a<\/em> \u53ea\u6709\u961f\u5217\u6ee1\u4e86\u624d\u4f1a\u901a\u8fc7\u589e\u52a0\u7ebf\u7a0b\u6765\u6551\u706b\uff0c\u54cd\u5e94\u504f\u6162\u3002<\/li>\n<\/ul>\n\n\n\n<p><strong>Tomcat \u7ebf\u7a0b\u6c60\u903b\u8f91\uff08StandardThreadExecutor\uff09\uff1a<\/strong><br>\u6838\u5fc3\u7ebf\u7a0b\u6ee1 -&gt; <strong>\u5148\u521b\u5efa\u65b0\u7ebf\u7a0b<\/strong>\uff08\u76f4\u5230\u6700\u5927\u503c\uff09 -&gt; \u7ebf\u7a0b\u6ee1 -&gt; <strong>\u624d\u5165\u961f\u5217<\/strong>\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><em>\u4f18\u70b9\uff1a<\/em> \u9762\u5bf9\u7a81\u53d1\u6d41\u91cf\uff0c\u4f18\u5148\u62c9\u8d77\u4eba\u624b\uff08\u7ebf\u7a0b\uff09\u5e72\u6d3b\uff0c\u5c3d\u91cf\u4e0d\u8ba9\u8bf7\u6c42\u6392\u961f\u3002<\/li>\n<\/ul>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"948\" src=\"https:\/\/eve2333.top\/wp-content\/uploads\/2025\/12\/1767047946-image-1024x948.png\" alt=\"\" class=\"wp-image-1797\" style=\"width:433px;height:auto\" srcset=\"https:\/\/eve2333.top\/wp-content\/uploads\/2025\/12\/1767047946-image-1024x948.png 1024w, https:\/\/eve2333.top\/wp-content\/uploads\/2025\/12\/1767047946-image-300x278.png 300w, https:\/\/eve2333.top\/wp-content\/uploads\/2025\/12\/1767047946-image-768x711.png 768w, https:\/\/eve2333.top\/wp-content\/uploads\/2025\/12\/1767047946-image-1536x1421.png 1536w, https:\/\/eve2333.top\/wp-content\/uploads\/2025\/12\/1767047946-image.png 1636w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Tomcat \u901a\u8fc7\u81ea\u5b9a\u4e49 TaskQueue \u5b9e\u73b0\u4e86\u8fd9\u4e00\u903b\u8f91\u3002\u5f53\u5b83\u53d1\u73b0\u5f53\u524d\u7ebf\u7a0b\u6570 &lt; \u6700\u5927\u7ebf\u7a0b\u6570\u65f6\uff0c\u4f1a\u6b3a\u9a97 JDK \u7ebf\u7a0b\u6c60\u8bf4\u201c\u961f\u5217\u6ee1\u4e86\u201d\uff0c\u8feb\u4f7f JDK \u521b\u5efa\u65b0\u7ebf\u7a0b<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">2.2 Jetty \u7ebf\u7a0b\u6c60\u539f\u7406\uff08\u201c\u5927\u9505\u996d\u201d\u7b56\u7565\uff09<\/h3>\n\n\n\n<p>Jetty \u6ca1\u6709\u7528 JDK \u7684 <code>ThreadPoolExecutor<\/code>\uff0c\u800c\u662f\u81ea\u5df1\u9020\u4e86\u4e2a\u8f6e\u5b50\u53eb <code>QueuedThreadPool<\/code>\uff1a<strong>\u7ebf\u7a0b\u6c60\u4e2d\u7684\u7ebf\u7a0b\u5faa\u73af\u6c38\u8fdc\u53ea\u4ece <code>_jobs<\/code> \u961f\u5217\u4e2d\u53d6\u4efb\u52a1<\/strong>\u3002<\/p>\n\n\n\n<p><strong>\u6838\u5fc3\u673a\u5236\uff1a<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u751f\u4ea7\u8005-\u6d88\u8d39\u8005\u6a21\u578b\uff1a<\/strong> \u6240\u6709\u4efb\u52a1\uff08HTTP \u8bf7\u6c42\uff09\u65e0\u5dee\u522b\u5730\u653e\u5165\u4e00\u4e2a\u516c\u5171\u961f\u5217 <code>_jobs<\/code>\u3002\u907f\u514d\u4e86\u201c\u76f4\u63a5 handoff \u7ed9\u7ebf\u7a0b\u201d\u5e26\u6765\u7684\u5e76\u53d1\u590d\u6742\u5ea6\uff08\u6bd4\u5982\u76f4\u63a5\u63d0\u4ea4\u4efb\u52a1\u5230\u7279\u5b9a\u7ebf\u7a0b\u9700\u8981\u66f4\u591a\u540c\u6b65\u903b\u8f91\uff09\u3002<\/li>\n\n\n\n<li><strong>\u62a2\u5360\u5f0f\u6267\u884c\uff1a<\/strong> \u6240\u6709\u7684\u7ebf\u7a0b\uff08\u65e0\u8bba\u662f\u7a7a\u95f2\u7684\u8fd8\u662f\u65b0\u542f\u52a8\u7684\uff09\u90fd\u6b7b\u5faa\u73af\u5730\u4ece\u8fd9\u4e2a <code>_jobs<\/code> \u961f\u5217\u91cc\u62a2\u4efb\u52a1\u505a\u3002\u91c7\u7528\u4f20\u7edf\u7684\"\u5148\u6392\u961f\u540e\u6269\u5bb9\"\u7b56\u7565\uff0c\u8fd9\u5728\u67d0\u4e9b\u573a\u666f\u4e0b\u53ef\u80fd\u5bfc\u81f4\u54cd\u5e94\u5ef6\u8fdf\u3002<\/li>\n<\/ul>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"996\" src=\"https:\/\/eve2333.top\/wp-content\/uploads\/2025\/12\/1767048241-image-1024x996.png\" alt=\"\" class=\"wp-image-1801\" style=\"aspect-ratio:1.0281428853364911;width:492px;height:auto\" srcset=\"https:\/\/eve2333.top\/wp-content\/uploads\/2025\/12\/1767048241-image-1024x996.png 1024w, https:\/\/eve2333.top\/wp-content\/uploads\/2025\/12\/1767048241-image-300x292.png 300w, https:\/\/eve2333.top\/wp-content\/uploads\/2025\/12\/1767048241-image-768x747.png 768w, https:\/\/eve2333.top\/wp-content\/uploads\/2025\/12\/1767048241-image-1536x1494.png 1536w, https:\/\/eve2333.top\/wp-content\/uploads\/2025\/12\/1767048241-image.png 1557w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><strong>\u4e0e Tomcat \u7684\u533a\u522b\uff1a<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u53c2\u6570\u540d\u4e0d\u540c\uff1a<\/strong> Tomcat \u53eb <code>corePoolSize\/maxPoolSize<\/code>\uff0cJetty \u53eb <code>minThreads\/maxThreads<\/code>\u3002<\/li>\n\n\n\n<li><strong>\u5355\u4f4d\u4e0d\u540c\uff1a<\/strong> \u7a7a\u95f2\u8d85\u65f6 <code>idleTimeout<\/code>\uff0cJetty \u7528<strong>\u6beb\u79d2<\/strong>\uff0cTomcat (JDK) \u901a\u5e38\u7528<strong>\u79d2<\/strong>\u3002<\/li>\n\n\n\n<li><strong>API \u5c01\u95ed\uff1a<\/strong> Jetty \u7684\u5185\u90e8\u961f\u5217 <code>_jobs<\/code> \u662f\u79c1\u6709\u7684\uff0c\u666e\u901a API \u62ff\u4e0d\u5230\uff0c\u540e\u7eed\u9700\u8981\u7528<strong>\u53cd\u5c04<\/strong>\u66b4\u529b\u83b7\u53d6\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\">3. \u7b2c\u4e00\u7bc7\u603b\u7ed3\u4e0e\u601d\u8003<\/h2>\n\n\n\n<p>\u5728\u8fdb\u5165\u4ee3\u7801\u5b9e\u73b0\u4e4b\u524d\uff0c\u6211\u4eec\u9700\u8981\u660e\u786e\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u76ee\u6807\uff1a<\/strong> \u5b9e\u73b0\u4e00\u5957\u4ee3\u7801\uff0c\u540c\u65f6\u517c\u5bb9 Tomcat \u548c Jetty\u3002<\/li>\n\n\n\n<li><strong>\u96be\u70b9\uff1a<\/strong>\n<ul class=\"wp-block-list\">\n<li>\u5982\u4f55\u62ff\u5230 SpringBoot \u542f\u52a8\u540e\u7684\u90a3\u4e2a <code>WebServer<\/code> \u5b9e\u4f8b\uff1f<\/li>\n\n\n\n<li>\u5982\u4f55\u62b9\u5e73 Tomcat <code>ThreadPoolExecutor<\/code> \u548c Jetty <code>QueuedThreadPool<\/code> \u7684 API \u5dee\u5f02\uff08\u6bd4\u5982 setCorePoolSize vs setMinThreads\uff09\uff1f<\/li>\n\n\n\n<li>\u5982\u4f55\u5728\u8c03\u6574\u53c2\u6570\u65f6\u4e0d\u62a5\u9519\uff08\u6bd4\u5982\u6838\u5fc3\u7ebf\u7a0b\u6570\u4e0d\u80fd\u5927\u4e8e\u6700\u5927\u7ebf\u7a0b\u6570\uff09\uff1f<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<p><strong>oneThread \u7684\u89e3\u51b3\u65b9\u6848\u662f\uff1a<\/strong> \u91c7\u7528<strong>\u9002\u914d\u5668\u6a21\u5f0f\uff08Adapter Pattern\uff09<\/strong> + <strong>Spring \u6761\u4ef6\u88c5\u914d<\/strong>\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><strong>\u4e0b\u4e00\u7bc7\u9884\u544a\uff1a\u67b6\u6784\u8bbe\u8ba1\u4e0e\u81ea\u52a8\u88c5\u914d<\/strong><br>\u63a5\u4e0b\u6765\u6211\u5c06\u8bb2\u89e3 <code>oneThread<\/code> \u662f\u5982\u4f55\u5229\u7528 SpringBoot \u7684 <code>@Conditional<\/code> \u9b54\u6cd5\u81ea\u52a8\u8bc6\u522b\u4f60\u7528\u7684\u662f Tomcat \u8fd8\u662f Jetty\uff0c\u4ee5\u53ca\u5982\u4f55\u901a\u8fc7\u7edf\u4e00\u7684\u63a5\u53e3\u5c42 <code>WebThreadPoolService<\/code> \u5c4f\u853d\u5e95\u5c42\u5dee\u5f02\u3002<\/p>\n\n\n\n<p>\u597d\u7684\uff0c\u6211\u4eec\u8fdb\u5165<strong>\u7b2c\u4e8c\u7bc7\uff1a\u67b6\u6784\u8bbe\u8ba1\u4e0e\u81ea\u52a8\u88c5\u914d\u9b54\u6cd5<\/strong>\u3002<\/p>\n\n\n\n<p>\u5728\u4e0a\u4e00\u7bc7\u4e2d\uff0c\u6211\u4eec\u641e\u6e05\u695a\u4e86 Tomcat \u548c Jetty \u50cf\u4e24\u4e2a\u6027\u683c\u8fe5\u5f02\u7684\u201c\u5de5\u4eba\u201d\uff08\u5e95\u5c42\u539f\u7406\u4e0d\u540c\uff09\u3002\u8fd9\u4e00\u7bc7\uff0c\u6211\u4eec\u8981\u8bb2\u89e3 <code>oneThread<\/code> \u6846\u67b6\u662f\u5982\u4f55\u5145\u5f53\u201c\u5305\u5de5\u5934\u201d\u7684\u89d2\u8272\uff0c\u628a\u8fd9\u4e24\u4e2a\u6027\u683c\u8fe5\u5f02\u7684\u5de5\u4eba\u7ba1\u7406\u8d77\u6765\uff0c\u8ba9\u4e0a\u5c42\u4e1a\u52a1\u611f\u89c9\u4e0d\u5230\u5dee\u5f02\u7684\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\u7b2c\u4e8c\u7bc7\uff1a\u67b6\u6784\u8bbe\u8ba1\u4e0e\u7edf\u4e00\u62bd\u8c61<\/h1>\n\n\n\n<h2 class=\"wp-block-heading\">1. \u6574\u4f53\u67b6\u6784\uff1a\u5206\u5c42\u8bbe\u8ba1\u7684\u667a\u6167<\/h2>\n\n\n\n<p>\u8bf7\u770b\u6587\u6863\u4e2d\u7684<strong>UML \u7c7b\u56fe<\/strong>\uff08\u7b2c\u4e8c\u5f20\u56fe\uff09\uff0c\u8fd9\u662f\u4e00\u4e2a\u5178\u578b\u7684<strong>\u9002\u914d\u5668\u6a21\u5f0f\uff08Adapter Pattern\uff09<\/strong> \u843d\u5730\u5b9e\u8df5\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"328\" src=\"https:\/\/eve2333.top\/wp-content\/uploads\/2025\/12\/1767048513-image-1024x328.png\" alt=\"\" class=\"wp-image-1802\" srcset=\"https:\/\/eve2333.top\/wp-content\/uploads\/2025\/12\/1767048513-image-1024x328.png 1024w, https:\/\/eve2333.top\/wp-content\/uploads\/2025\/12\/1767048513-image-300x96.png 300w, https:\/\/eve2333.top\/wp-content\/uploads\/2025\/12\/1767048513-image-768x246.png 768w, https:\/\/eve2333.top\/wp-content\/uploads\/2025\/12\/1767048513-image-1536x492.png 1536w, https:\/\/eve2333.top\/wp-content\/uploads\/2025\/12\/1767048513-image.png 1940w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>\u67b6\u6784\u53ef\u4ee5\u5206\u4e3a\u4e09\u5c42\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u914d\u7f6e\u5c42 (Configuration Layer)\uff1a<\/strong>\n<ul class=\"wp-block-list\">\n<li><code>WebAdapterConfiguration<\/code>\uff1a\u8fd9\u662f\u5165\u53e3\u3002\u5b83\u8d1f\u8d23\u4fa6\u63a2\u5de5\u4f5c\uff0c\u5224\u65ad\u5f53\u524d\u73af\u5883\u91cc\u5230\u5e95\u662f\u8c01\u5728\u5e72\u6d3b\uff08Tomcat \u8fd8\u662f Jetty\uff1f\uff09\uff0c\u7136\u540e\u628a\u5bf9\u5e94\u7684\u7ba1\u7406\u670d\u52a1\u52a0\u8f7d\u8fdb\u6765\u3002<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>\u62bd\u8c61\u5c42 (Abstraction Layer)\uff1a<\/strong>\n<ul class=\"wp-block-list\">\n<li><code>WebThreadPoolService<\/code> (\u63a5\u53e3) &amp; <code>AbstractWebThreadPoolService<\/code> (\u62bd\u8c61\u7c7b)\uff1a\u5236\u5b9a\u201c\u5bb6\u89c4\u201d\u3002\u4e0d\u7ba1\u4f60\u662f\u8c01\uff0c\u90fd\u8981\u80fd\u6c47\u62a5\u72b6\u6001(<code>RuntimeState<\/code>)\uff0c\u90fd\u8981\u80fd\u8c03\u6574\u53c2\u6570(<code>updateThreadPool<\/code>)\u3002<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>\u5b9e\u73b0\u5c42 (Implementation Layer)\uff1a<\/strong>\n<ul class=\"wp-block-list\">\n<li><code>TomcatWebThreadPoolService<\/code>\uff1a\u61c2 Tomcat \u7684\u201c\u65b9\u8a00\u201d\uff0c\u8d1f\u8d23\u7ffb\u8bd1\u6307\u4ee4\u3002<\/li>\n\n\n\n<li><code>JettyWebThreadPoolService<\/code>\uff1a\u61c2 Jetty \u7684\u201c\u65b9\u8a00\u201d\uff0c\u8d1f\u8d23\u7ffb\u8bd1\u6307\u4ee4\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">2. \u6838\u5fc3\u9b54\u6cd5\uff1aSpring Boot \u6761\u4ef6\u88c5\u914d<\/h2>\n\n\n\n<p>\u5f88\u591a\u540c\u5b66\u770b\u5f00\u6e90\u6846\u67b6\u6e90\u7801\uff0c\u6700\u5bb9\u6613\u6655\u7684\u5c31\u662f\uff1a\u201c\u4e3a\u4ec0\u4e48\u4ed6\u5f15\u5165\u4e86\u90a3\u4e48\u591a\u4f9d\u8d56\uff0c\u8fd0\u884c\u8d77\u6765\u5374\u4e0d\u62a5\u9519\uff1f\u201d<\/p>\n\n\n\n<p><code>oneThread<\/code> \u540c\u65f6\u652f\u6301 Tomcat\u3001Jetty\u3001Undertow\uff0c\u4f46\u5728\u4f60\u7684\u9879\u76ee\u4e2d\uff0c\u4f60\u53ef\u80fd\u53ea\u7528\u4e86 Tomcat\u3002\u5982\u679c\u4ee3\u7801\u91cc\u76f4\u63a5 <code>new JettyWebThreadPoolService()<\/code>\uff0cJVM \u9a6c\u4e0a\u5c31\u4f1a\u629b\u51fa <code>ClassNotFoundException<\/code>\uff0c\u56e0\u4e3a\u4f60\u7684 classpath \u4e0b\u6839\u672c\u6ca1\u6709 Jetty \u7684 jar \u5305\u3002<\/p>\n\n\n\n<p><strong>\u89e3\u51b3\u65b9\u6848\uff1a<code>@Conditional<\/code> \u7cfb\u5217\u6ce8\u89e3<\/strong><\/p>\n\n\n\n<p>oneThread \u4f7f\u7528 SpringBoot \u7684\u6761\u4ef6\u88c5\u914d\u673a\u5236\u6765\u5b9e\u73b0\u591a\u5bb9\u5668\u7684\u81ea\u52a8\u9002\u914d\uff1b\u8ba9\u6211\u4eec\u5256\u6790\u6587\u6863\u4e2d\u7684\u8fd9\u6bb5\u6838\u5fc3\u4ee3\u7801\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>@Bean\n@ConditionalOnClass(name = {\n    \"org.apache.catalina.startup.Tomcat\", \n    \"org.apache.coyote.UpgradeProtocol\", \n    \"jakarta.servlet.Servlet\"\n})\n@ConditionalOnBean(value = ConfigurableTomcatWebServerFactory.class, search = SearchStrategy.CURRENT)\npublic TomcatWebThreadPoolService tomcatWebThreadPoolService() {\n    return new TomcatWebThreadPoolService();\n}<\/code><\/pre>\n\n\n\n<p>\u8fd9\u91cc\u6709<strong>\u4e24\u9053\u9632\u7ebf<\/strong>\u786e\u4fdd\u5b89\u5168\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u7b2c\u4e00\u9053\u9632\u7ebf <code>@ConditionalOnClass<\/code>\uff08\u7c7b\u5b58\u5728\u5417\uff1f\uff09\uff1a<\/strong>\n<ul class=\"wp-block-list\">\n<li>Spring \u4f1a\u5148\u68c0\u67e5\uff1a<code>org.apache.catalina.startup.Tomcat<\/code> \u8fd9\u4e2a\u7c7b\u5728\u4e0d\u5728\uff1f<\/li>\n\n\n\n<li>\u5982\u679c\u4f60\u6ca1\u5f15\u5165 <code>spring-boot-starter-tomcat<\/code>\uff0c\u8fd9\u4e2a\u7c7b\u5c31\u4e0d\u5b58\u5728\u3002Spring \u76f4\u63a5\u8df3\u8fc7\u8fd9\u4e2a Bean \u7684\u521b\u5efa\uff0cJVM \u5c31\u4e0d\u4f1a\u53bb\u52a0\u8f7d <code>TomcatWebThreadPoolService<\/code>\uff0c\u4ece\u800c\u907f\u514d\u62a5\u9519\u3002<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>\u7b2c\u4e8c\u9053\u9632\u7ebf <code>@ConditionalOnBean<\/code>\uff08\u5b9e\u4f8b\u5b58\u5728\u5417\uff1f\uff09\uff1a<\/strong>\n<ul class=\"wp-block-list\">\n<li>\u9632\u6b62\u867d\u7136\u6709 jar \u5305\uff0c\u4f46\u662f\u7528\u6237\u624b\u52a8\u5173\u95ed\u4e86 Web \u529f\u80fd\uff08\u6bd4\u5982\u8dd1\u8dd1\u6279\u4efb\u52a1\uff09\u3002<\/li>\n\n\n\n<li>\u53ea\u6709 Spring \u5bb9\u5668\u91cc\u771f\u6b63\u8dd1\u8d77\u6765\u4e86 <code>ConfigurableTomcatWebServerFactory<\/code>\uff0c\u8bf4\u660e\u771f\u7684\u662f\u4e2a Web \u73af\u5883\uff0c\u624d\u521b\u5efa\u670d\u52a1\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<p><strong>Maven \u4f9d\u8d56\u7ba1\u7406\u7684\u6280\u5de7\uff1a<\/strong><br>\u6587\u6863\u4e2d\u63d0\u5230\u7684 <code>&lt;optional&gt;true&lt;\/optional&gt;<\/code> \u975e\u5e38\u5173\u952e\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>oneThread<\/code> \u7f16\u8bd1\u65f6\uff1a\u6211\u6709 Tomcat \u548c Jetty \u7684 jar \u5305\uff0c\u6240\u4ee5\u4ee3\u7801\u80fd\u7f16\u8bd1\u901a\u8fc7\u3002<\/li>\n\n\n\n<li><code>\u4f60\u7684\u9879\u76ee<\/code> \u5f15\u7528 <code>oneThread<\/code> \u65f6\uff1aMaven \u770b\u5230 <code>optional<\/code>\uff0c\u5c31\u4e0d\u4f1a\u628a Tomcat\/Jetty \u7684\u4f9d\u8d56\u4f20\u9012\u7ed9\u4f60\u3002\u4f60\u539f\u672c\u7528\u4ec0\u4e48\uff0c\u5c31\u662f\u4ec0\u4e48\uff0c\u4fdd\u6301\u4e86\u9879\u76ee\u7684\u7eaf\u51c0\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\">3. \u7edf\u4e00\u62bd\u8c61\uff1a\u5236\u5b9a\u201c\u666e\u901a\u8bdd\u201d<\/h2>\n\n\n\n<p>\u5e95\u5c42\u5dee\u5f02\u8fd9\u4e48\u5927\uff0c\u5fc5\u987b\u5f3a\u5236\u7edf\u4e00\u63a5\u53e3\u3002<code>WebThreadPoolService<\/code> \u63a5\u53e3\u5c31\u662f\u201c\u666e\u901a\u8bdd\u201d\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public interface WebThreadPoolService {\n    \/\/ \u4f60\u7684\u9879\u76ee\u53ea\u7ba1\u8c03\u8fd9\u4e2a\u65b9\u6cd5\uff0c\u4e0d\u7528\u7ba1\u5e95\u5c42\u662f setCorePoolSize \u8fd8\u662f setMinThreads\n    void updateThreadPool(WebThreadPoolConfig config);\n\n    \/\/ \u7edf\u4e00\u8fd4\u56de\u8fd9\u4e2a\u5bf9\u8c61\uff0c\u4e0d\u7ba1\u5e95\u5c42\u6307\u6807\u53eb\u4ec0\n\n    \/\/ \u83b7\u53d6\u57fa\u7840\u6307\u6807\uff08\u8f7b\u91cf\u7ea7\uff0c\u9002\u5408\u9ad8\u9891\u8c03\u7528\uff09\n    WebThreadPoolBaseMetrics getBasicMetrics();\n    \n    \/\/ \u83b7\u53d6\u5b8c\u6574\u8fd0\u884c\u72b6\u6001\uff08\u53ef\u80fd\u6d89\u53ca\u9501\uff0c\u4e0d\u5efa\u8bae\u9ad8\u9891\u8c03\u7528\uff09\n    WebThreadPoolState getRuntimeState();\n    \n    \/\/ \u83b7\u53d6\u8fd0\u884c\u72b6\u6001\u63cf\u8ff0\n    String getRunningStatus();\n    \n    \/\/ \u83b7\u53d6\u5bb9\u5668\u7c7b\u578b\n    WebContainerEnum getWebContainerType();\n}<\/code><\/pre>\n\n\n\n<p><strong>\u8bbe\u8ba1\u54f2\u5b66\uff1a<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u4e0a\u5c42\u8c03\u7528\u65e0\u611f\uff1a<\/strong> \u5f53\u4f60\u9700\u8981\u4fee\u6539\u7ebf\u7a0b\u6c60\u65f6\uff0c\u4f60\u53ea\u9700\u8981\u6784\u5efa\u4e00\u4e2a <code>WebThreadPoolConfig<\/code> \u5bf9\u8c61\uff0c\u91cc\u9762\u5305\u542b\u901a\u7528\u7684 <code>corePoolSize<\/code>, <code>maxPoolSize<\/code>\u3002<\/li>\n\n\n\n<li><strong>\u4e0b\u5c42\u8d1f\u8d23\u7ffb\u8bd1\uff1a<\/strong> \u5177\u4f53\u7684 Service \u5b9e\u73b0\u7c7b\u8d1f\u8d23\u628a\u8fd9\u4e9b\u901a\u7528\u53c2\u6570\uff0c\u8f6c\u6362\u6210\u5e95\u5c42\u5bb9\u5668\u80fd\u542c\u61c2\u7684\u6307\u4ee4\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\">4. \u62bd\u4e1d\u5265\u8327\uff1a\u5982\u4f55\u62ff\u5230\u5e95\u5c42\u7684\u7ebf\u7a0b\u6c60\u5bf9\u8c61\uff1f<\/h2>\n\n\n\n<p>\u8fd9\u662f\u672c\u7bc7\u6700\u7cbe\u5f69\u7684<strong>\u6e90\u7801\u6316\u6398<\/strong>\u90e8\u5206\u3002Spring Boot \u5c01\u88c5\u5f97\u592a\u597d\u4e86\uff0c\u5bfc\u81f4\u6211\u4eec\u5f88\u96be\u62ff\u5230\u6700\u91cc\u9762\u7684 <code>ThreadPoolExecutor<\/code>\u3002<\/p>\n\n\n\n<p>\u6211\u4eec\u9700\u8981\u50cf\u201c\u5265\u6d0b\u8471\u201d\u4e00\u6837\u4e00\u5c42\u5c42\u5265\u5f00 Spring \u7684\u5c01\u88c5\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">4.1 Tomcat \u7684\u201c\u6d0b\u8471\u7ed3\u6784\u201d<\/h3>\n\n\n\n<p>\u5728 <code>TomcatWebThreadPoolService<\/code> \u4e2d\uff0c\u83b7\u53d6\u7ebf\u7a0b\u6c60\u7684\u8def\u5f84\u662f\u8fd9\u6837\u7684\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>@Override\nprotected Executor getExecutor(WebServer webServer) {\n    return  \n\/\/ \u5165\u53e3\u662f Spring \u7684 WebServer \u63a5\u53e3\n((TomcatWebServer) webServer)  \/\/ 1. \u5f3a\u8f6c\u4e3a TomcatWebServer (Spring \u7684\u5c01\u88c5)\n    .getTomcat()               \/\/ 2. \u83b7\u53d6 Tomcat \u5b9e\u4f8b (org.apache.catalina.startup.Tomcat)\n    .getConnector()            \/\/ 3. \u83b7\u53d6\u8fde\u63a5\u5668 (\u5904\u7406\u7aef\u53e3\u8fde\u63a5\u7684)\n    .getProtocolHandler()      \/\/ 4. \u83b7\u53d6\u534f\u8bae\u5904\u7406\u5668 (\u5904\u7406 HTTP\/AJP \u534f\u8bae\u7684)\n    .getExecutor();            \/\/ 5. \u7ec8\u4e8e\u62ff\u5230\u4e86 java.util.concurrent.Executor !\n}<\/code><\/pre>\n\n\n\n<p>\u62ff\u5230\u8fd9\u4e2a <code>Executor<\/code> \u540e\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u628a\u5b83\u5f3a\u8f6c\u4e3a <code>ThreadPoolExecutor<\/code>\uff0c\u7136\u540e\u5c31\u53ef\u4ee5\u968f\u5fc3\u6240\u6b32\u5730\u8c03\u7528 <code>setCorePoolSize<\/code> \u4e86\u3002<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\u5728\u52a8\u6001\u8c03\u6574 Tomcat \u7ebf\u7a0b\u6c60\u53c2\u6570\u65f6\uff0c\u9700\u8981\u7279\u522b\u6ce8\u610f\u53c2\u6570\u66f4\u65b0\u7684\u987a\u5e8f\uff0c\u907f\u514d\u51fa\u73b0\u914d\u7f6e\u51b2\u7a81\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>@Override\npublic void updateThreadPool(WebThreadPoolConfig config) {\n    try {\n        ThreadPoolExecutor tomcatExecutor = (ThreadPoolExecutor) executor;\n        int originalCorePoolSize = tomcatExecutor.getCorePoolSize();\n        int originalMaximumPoolSize = tomcatExecutor.getMaximumPoolSize();\n        long originalKeepAliveTime = tomcatExecutor.getKeepAliveTime(TimeUnit.SECONDS);\n\u200b\n        \/\/ \u5173\u952e\uff1a\u53c2\u6570\u66f4\u65b0\u987a\u5e8f\u5f88\u91cd\u8981\n        if (config.getCorePoolSize() > originalMaximumPoolSize) {\n            \/\/ \u5982\u679c\u65b0\u7684\u6838\u5fc3\u7ebf\u7a0b\u6570\u5927\u4e8e\u5f53\u524d\u6700\u5927\u7ebf\u7a0b\u6570\uff0c\u5148\u8c03\u6574\u6700\u5927\u7ebf\u7a0b\u6570\n            tomcatExecutor.setMaximumPoolSize(config.getMaximumPoolSize());\n            tomcatExecutor.setCorePoolSize(config.getCorePoolSize());\n        } else {\n            \/\/ \u5426\u5219\u5148\u8c03\u6574\u6838\u5fc3\u7ebf\u7a0b\u6570\n            tomcatExecutor.setCorePoolSize(config.getCorePoolSize());\n            tomcatExecutor.setMaximumPoolSize(config.getMaximumPoolSize());\n        }\n        \n        tomcatExecutor.setKeepAliveTime(config.getKeepAliveTime(), TimeUnit.SECONDS);\n\u200b\n        log.info(\"&#91;Tomcat] Changed web thread pool. corePoolSize: {}, maximumPoolSize: {}, keepAliveTime: {}\",\n            String.format(Constants.CHANGE_DELIMITER, originalCorePoolSize, config.getCorePoolSize()),\n            String.format(Constants.CHANGE_DELIMITER, originalMaximumPoolSize, config.getMaximumPoolSize()),\n            String.format(Constants.CHANGE_DELIMITER, originalKeepAliveTime, config.getKeepAliveTime()));\n    } catch (Exception ex) {\n        log.error(\"Failed to modify the Tomcat thread pool parameter.\", ex);\n    }\n}<\/code><\/pre>\n\n\n\n<p>ThreadPoolExecutor\u6709\u4e00\u4e2a\u91cd\u8981\u7684\u7ea6\u675f\uff1a<strong>\u6838\u5fc3\u7ebf\u7a0b\u6570\u4e0d\u80fd\u5927\u4e8e\u6700\u5927\u7ebf\u7a0b\u6570<\/strong> \u3002\u8fd9\u91cc\u524d\u9762\u5df2\u7ecf\u8bb2\u89e3\u8fc7\uff0c\u5c31\u4e0d\u518d\u8d58\u8ff0\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"3-tomcat\">3. Tomcat \u7ebf\u7a0b\u6c60\u76d1\u63a7\u6307\u6807\u83b7\u53d6<\/h3>\n\n\n\n<p>Tomcat \u57fa\u4e8e\u6807\u51c6\u7684 ThreadPoolExecutor\uff0c\u56e0\u6b64\u53ef\u4ee5\u83b7\u53d6\u5230\u4e30\u5bcc\u7684\u8fd0\u884c\u65f6\u6307\u6807\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>@Override\npublic WebThreadPoolState getRuntimeState() {\n    ThreadPoolExecutor tomcatExecutor = (ThreadPoolExecutor) executor;\n    \n    \/\/ \u57fa\u7840\u914d\u7f6e\u53c2\u6570\n    int corePoolSize = tomcatExecutor.getCorePoolSize();\n    int maximumPoolSize = tomcatExecutor.getMaximumPoolSize();\n    long keepAliveTime = tomcatExecutor.getKeepAliveTime(TimeUnit.SECONDS);\n    \n    \/\/ \u8fd0\u884c\u65f6\u72b6\u6001\u6307\u6807\n    int activeCount = tomcatExecutor.getActiveCount();           \/\/ \u5f53\u524d\u6d3b\u8dc3\u7ebf\u7a0b\u6570\n    long completedTaskCount = tomcatExecutor.getCompletedTaskCount(); \/\/ \u5df2\u5b8c\u6210\u4efb\u52a1\u6570\n    int largestPoolSize = tomcatExecutor.getLargestPoolSize();   \/\/ \u5386\u53f2\u6700\u5927\u7ebf\u7a0b\u6570\n    int currentPoolSize = tomcatExecutor.getPoolSize();         \/\/ \u5f53\u524d\u7ebf\u7a0b\u6570\n    \n    \/\/ \u961f\u5217\u76f8\u5173\u6307\u6807\n    BlockingQueue&lt;?> blockingQueue = tomcatExecutor.getQueue();\n    int blockingQueueSize = blockingQueue.size();               \/\/ \u961f\u5217\u4e2d\u7b49\u5f85\u7684\u4efb\u52a1\u6570\n    int remainingCapacity = blockingQueue.remainingCapacity();  \/\/ \u961f\u5217\u5269\u4f59\u5bb9\u91cf\n    int queueCapacity = blockingQueueSize + remainingCapacity;  \/\/ \u961f\u5217\u603b\u5bb9\u91cf\n    \n    \/\/ \u62d2\u7edd\u7b56\u7565\n    String rejectedExecutionHandlerName = tomcatExecutor\n        .getRejectedExecutionHandler()\n        .getClass()\n        .getSimpleName();\n    \n    return WebThreadPoolState.builder()\n        .corePoolSize(corePoolSize)\n        .maximumPoolSize(maximumPoolSize)\n        .activePoolSize(activeCount)\n        .completedTaskCount(completedTaskCount)\n        .largestPoolSize(largestPoolSize)\n        .currentPoolSize(currentPoolSize)\n        .keepAliveTime(keepAliveTime)\n        .workQueueName(blockingQueue.getClass().getSimpleName())\n        .workQueueSize(blockingQueueSize)\n        .workQueueRemainingCapacity(remainingCapacity)\n        .workQueueCapacity(queueCapacity)\n        .rejectedHandlerName(rejectedExecutionHandlerName)\n        .build();\n}<\/code><\/pre>\n<\/blockquote>\n\n\n\n<p><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">4.2 Jetty \u7684\u201c\u6d0b\u8471\u7ed3\u6784\u201d<\/h3>\n\n\n\n<p>Jetty \u7a0d\u5fae\u7b80\u5355\u4e00\u70b9\uff0c\u4f46\u4e5f\u9700\u8981\u8f6c\u6362\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>((JettyWebServer) webServer)   \/\/ 1. \u5f3a\u8f6c\u4e3a JettyWebServer\n    .getServer()               \/\/ 2. \u83b7\u53d6 Jetty Server \u6838\u5fc3\u5bf9\u8c61\n    .getThreadPool();          \/\/ 3. \u83b7\u53d6 org.eclipse.jetty.util.thread.ThreadPool<\/code><\/pre>\n\n\n\n<p>\u62ff\u5230 <code>ThreadPool<\/code> \u540e\uff0c\u5f3a\u8f6c\u4e3a <code>QueuedThreadPool<\/code>\uff0c\u5c31\u53ef\u4ee5\u8c03\u7528 <code>setMinThreads<\/code> \u7b49 Jetty \u7279\u6709\u7684 API \u4e86\u3002<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>1Jetty \u53c2\u6570\u66f4\u65b0\u5b9e\u73b0<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>@Override\npublic void updateThreadPool(WebThreadPoolConfig config) {\n    try {\n        QueuedThreadPool jettyExecutor = (QueuedThreadPool) executor;\n        int originalCorePoolSize = jettyExecutor.getMinThreads();\n        int originalMaximumPoolSize = jettyExecutor.getMaxThreads();\n        long originalKeepAliveTime = jettyExecutor.getIdleTimeout();\n\u200b\n        \/\/ Jetty\u4e5f\u9700\u8981\u6ce8\u610f\u53c2\u6570\u66f4\u65b0\u987a\u5e8f\n        if (config.getCorePoolSize() > originalMaximumPoolSize) {\n            jettyExecutor.setMaxThreads(config.getMaximumPoolSize());\n            jettyExecutor.setMinThreads(config.getCorePoolSize());\n        } else {\n            jettyExecutor.setMinThreads(config.getCorePoolSize());\n            jettyExecutor.setMaxThreads(config.getMaximumPoolSize());\n        }\n        \n        \/\/ \u6ce8\u610f\uff1aJetty\u7684idleTimeout\u4f7f\u7528\u6beb\u79d2\u5355\u4f4d\uff0c\u9700\u8981\u8f6c\u6362\n        jettyExecutor.setIdleTimeout(config.getKeepAliveTime().intValue());\n\u200b\n        log.info(\"&#91;Jetty] Changed web thread pool. corePoolSize: {}, maximumPoolSize: {}, keepAliveTime: {}\",\n            String.format(Constants.CHANGE_DELIMITER, originalCorePoolSize, config.getCorePoolSize()),\n            String.format(Constants.CHANGE_DELIMITER, originalMaximumPoolSize, config.getMaximumPoolSize()),\n            String.format(Constants.CHANGE_DELIMITER, originalKeepAliveTime, config.getKeepAliveTime()));\n    } catch (Exception ex) {\n        log.error(\"Failed to modify the Jetty thread pool parameter.\", ex);\n    }\n}<\/code><\/pre>\n\n\n\n<p><strong>Jetty\u53c2\u6570\u8c03\u6574\u7684\u6ce8\u610f\u4e8b\u9879<\/strong> \uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>1.<strong>\u65b9\u6cd5\u540d\u6620\u5c04<\/strong> \uff1a\n<ul class=\"wp-block-list\">\n<li><code>setCorePoolSize()<\/code> \u2192 <code>setMinThreads()<\/code><\/li>\n\n\n\n<li><code>setMaximumPoolSize()<\/code> \u2192 <code>setMaxThreads()<\/code><\/li>\n\n\n\n<li><code>setKeepAliveTime()<\/code> \u2192 <code>setIdleTimeout()<\/code><\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>2.<strong>\u65f6\u95f4\u5355\u4f4d\u8f6c\u6362<\/strong> \uff1aJetty \u7684 <code>idleTimeout<\/code> \u4f7f\u7528\u6beb\u79d2\uff0c\u800c\u6211\u4eec\u7684\u914d\u7f6e\u4f7f\u7528\u79d2\uff0c\u9700\u8981\u8fdb\u884c\u5355\u4f4d\u8f6c\u6362\u3002<\/li>\n\n\n\n<li>3.<strong>\u53c2\u6570\u7ea6\u675f<\/strong> \uff1aJetty \u540c\u6837\u6709 <code>minThreads &lt;= maxThreads<\/code> \u7684\u7ea6\u675f\uff0c\u9700\u8981\u6ce8\u610f\u66f4\u65b0\u987a\u5e8f\u3002<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"3-jetty\">3. Jetty \u961f\u5217\u4fe1\u606f\u83b7\u53d6\u7684\u7279\u6b8a\u5904\u7406<\/h3>\n\n\n\n<p>\u7531\u4e8eJetty\u7684\u961f\u5217\u5bf9\u8c61\u662f\u79c1\u6709\u5b57\u6bb5\uff0c\u9700\u8981\u901a\u8fc7\u53cd\u5c04\u6765\u83b7\u53d6\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>@SneakyThrows\n@Override\npublic WebThreadPoolBaseMetrics getBasicMetrics() {\n    QueuedThreadPool jettyExecutor = (QueuedThreadPool) executor;\n    int corePoolSize = jettyExecutor.getMinThreads();\n    int maximumPoolSize = jettyExecutor.getMaxThreads();\n    long keepAliveTime = jettyExecutor.getIdleTimeout();\n\u200b\n    \/\/ \u901a\u8fc7\u53cd\u5c04\u83b7\u53d6\u79c1\u6709\u7684_jobs\u961f\u5217\n    BlockingQueue jobs = (BlockingQueue) ReflectUtil.getFieldValue(jettyExecutor, \"_jobs\");\n    int blockingQueueSize = jettyExecutor.getQueueSize();\n    int remainingCapacity = jobs.remainingCapacity();\n    int queueCapacity = blockingQueueSize + remainingCapacity;\n    \n    \/\/ Jetty \u6ca1\u6709\u6807\u51c6\u7684\u62d2\u7edd\u7b56\u7565\uff0c\u4f7f\u7528\u56fa\u5b9a\u540d\u79f0\n    String rejectedExecutionHandlerName = \"JettyRejectedExecutionHandler\";\n\u200b\n    return WebThreadPoolBaseMetrics.builder()\n        .corePoolSize(corePoolSize)\n        .maximumPoolSize(maximumPoolSize)\n        .keepAliveTime(keepAliveTime)\n        .workQueueName(jobs.getClass().getSimpleName())\n        .workQueueSize(blockingQueueSize)\n        .workQueueRemainingCapacity(remainingCapacity)\n        .workQueueCapacity(queueCapacity)\n        .rejectedHandlerName(rejectedExecutionHandlerName)\n        .build();\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"4-jetty\">4. Jetty \u8fd0\u884c\u72b6\u6001\u7684\u5c40\u9650\u6027<\/h3>\n\n\n\n<p>\u7531\u4e8e Jetty \u7ebf\u7a0b\u6c60\u8bbe\u8ba1\u7684\u5dee\u5f02\uff0c\u67d0\u4e9b ThreadPoolExecutor \u63d0\u4f9b\u7684\u6307\u6807\u5728Jetty\u4e2d\u65e0\u6cd5\u83b7\u53d6\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>@Override\npublic WebThreadPoolState getRuntimeState() {\n    QueuedThreadPool jettyExecutor = (QueuedThreadPool) executor;\n    int corePoolSize = jettyExecutor.getMinThreads();\n    int maximumPoolSize = jettyExecutor.getMaxThreads();\n    int activeCount = jettyExecutor.getBusyThreads();        \/\/ \u5bf9\u5e94activeCount\n    int currentPoolSize = jettyExecutor.getThreads();       \/\/ \u5bf9\u5e94currentPoolSize\n    long keepAliveTime = jettyExecutor.getIdleTimeout();\n\u200b\n    BlockingQueue jobs = (BlockingQueue) ReflectUtil.getFieldValue(jettyExecutor, \"_jobs\");\n    int blockingQueueSize = jettyExecutor.getQueueSize();\n    int remainingCapacity = jobs.remainingCapacity();\n    int queueCapacity = blockingQueueSize + remainingCapacity;\n    String rejectedExecutionHandlerName = \"JettyRejectedExecutionHandler\";\n\u200b\n    \/\/ \u6ce8\u610f\uff1aJetty\u65e0\u6cd5\u63d0\u4f9bcompletedTaskCount\u548clargestPoolSize\n    return WebThreadPoolState.builder()\n        .corePoolSize(corePoolSize)\n        .maximumPoolSize(maximumPoolSize)\n        .activePoolSize(activeCount)\n        .currentPoolSize(currentPoolSize)\n        .keepAliveTime(keepAliveTime)\n        .workQueueName(jobs.getClass().getSimpleName())\n        .workQueueSize(blockingQueueSize)\n        .workQueueRemainingCapacity(remainingCapacity)\n        .workQueueCapacity(queueCapacity)\n        .rejectedHandlerName(rejectedExecutionHandlerName)\n        \/\/ completedTaskCount\u548clargestPoolSize\u5728Jetty\u4e2d\u4e0d\u53ef\u7528\uff0c\u8bbe\u4e3a\u9ed8\u8ba4\u503c\n        .completedTaskCount(0L)\n        .largestPoolSize(0)\n        .build();\n}<\/code><\/pre>\n\n\n\n<p>\u8fd9\u79cd\u5c40\u9650\u6027\u5728\u8bbe\u8ba1\u76d1\u63a7\u7cfb\u7edf\u65f6\u9700\u8981\u7279\u522b\u6ce8\u610f\uff0c\u4e0d\u80fd\u5047\u8bbe\u6240\u6709\u5bb9\u5668\u90fd\u80fd\u63d0\u4f9b\u76f8\u540c\u7684\u6307\u6807\u3002<\/p>\n<\/blockquote>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">5. \u7b2c\u4e8c\u7bc7\u603b\u7ed3<\/h2>\n\n\n\n<p>\u5230\u8fd9\u91cc\uff0c\u6211\u4eec\u5df2\u7ecf\u642d\u5efa\u597d\u4e86\u821e\u53f0\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u627e\u5230\u4e86\u4eba<\/strong>\uff1a\u901a\u8fc7 Spring \u6e90\u7801\u5206\u6790\uff0c\u6211\u4eec\u4ece\u6df1\u5c42\u6316\u51fa\u4e86 <code>Executor<\/code> \u5b9e\u4f8b\u3002<\/li>\n\n\n\n<li><strong>\u9009\u5bf9\u4e86\u4eba<\/strong>\uff1a\u901a\u8fc7 <code>@Conditional<\/code>\uff0c\u81ea\u52a8\u8bc6\u522b\u5f53\u524d\u662f Tomcat \u8fd8\u662f Jetty \u73af\u5883\u3002<\/li>\n\n\n\n<li><strong>\u7edf\u4e00\u4e86\u8bed\u8a00<\/strong>\uff1a\u901a\u8fc7 <code>WebThreadPoolService<\/code> \u63a5\u53e3\uff0c\u5c4f\u853d\u4e86\u5e95\u5c42 API \u7684\u5dee\u5f02\u3002<\/li>\n<\/ol>\n\n\n\n<p><strong>\u8fd8\u6ca1\u89e3\u51b3\u7684\u95ee\u9898\uff08\u4f0f\u7b14\uff09\uff1a<\/strong><br>\u867d\u7136\u6211\u4eec\u62ff\u5230\u4e86\u7ebf\u7a0b\u6c60\u5bf9\u8c61\uff0c\u4e5f\u80fd\u8c03\u7528 set \u65b9\u6cd5\u4e86\uff0c\u4f46\u662f<strong>\u76f4\u63a5\u8c03\u7528\u5b89\u5168\u5417\uff1f<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5982\u679c\u6211\u628a\u6838\u5fc3\u7ebf\u7a0b\u6570\u8bbe\u5f97\u6bd4\u6700\u5927\u7ebf\u7a0b\u6570\u8fd8\u5927\uff0cTomcat \u4f1a\u62a5\u9519\u5417\uff1f<\/li>\n\n\n\n<li>Jetty \u7684\u961f\u5217\u662f\u79c1\u6709\u7684\uff0c\u600e\u4e48\u76d1\u63a7\u5b83\u7684\u961f\u5217\u957f\u5ea6\uff1f<\/li>\n\n\n\n<li>\u76d1\u63a7\u6570\u636e\u5982\u4f55\u5b9e\u65f6\u83b7\u53d6\uff1f<\/li>\n<\/ul>\n\n\n\n<p><strong>\u4e0b\u4e00\u7bc7\u9884\u544a\uff1a\u5b89\u5168\u8c03\u53c2\u7b56\u7565\u4e0e\u9ed1\u79d1\u6280\u76d1\u63a7<\/strong><br>\u8fd9\u662f\u6700\u540e\u4e00\u7bc7\uff0c\u4e5f\u662f\u6700\u5b9e\u6218\u7684\u4e00\u7bc7\u3002\u6211\u4eec\u5c06\u6df1\u5165\u4ee3\u7801\u7ec6\u8282\uff0c\u8bb2\u89e3\u5982\u4f55<strong>\u9632\u6b62\u53c2\u6570\u66f4\u65b0\u5bfc\u81f4\u7684\u62a5\u9519<\/strong>\uff0c\u4ee5\u53ca\u5982\u4f55\u5229\u7528<strong>\u53cd\u5c04<\/strong>\u53bb\u5077\u7aa5 Jetty \u7684\u79c1\u6709\u961f\u5217\u72b6\u6001\u3002<\/p>\n\n\n\n<p>\u597d\uff0c\u6211\u4eec\u8fdb\u5165<strong>\u7b2c\u4e09\u7bc7\uff08\u7ec8\u7bc7\uff09\uff1a\u5b89\u5168\u8c03\u53c2\u7b56\u7565\u4e0e\u9ed1\u79d1\u6280\u76d1\u63a7<\/strong>\u3002<\/p>\n\n\n\n<p>\u5728\u524d\u4e24\u7bc7\u4e2d\uff0c\u6211\u4eec\u641e\u61c2\u4e86\u539f\u7406\uff0c\u4e5f\u62ff\u5230\u4e86\u5e95\u5c42\u7684\u63a7\u5236\u6743\u3002\u73b0\u5728\uff0c\u6211\u4eec\u8981\u9762\u5bf9\u6700\u68d8\u624b\u7684<strong>\u5b9e\u6218\u7ec6\u8282<\/strong>\uff1a\u5982\u4f55\u5728\u201c\u9ad8\u901f\u516c\u8def\u4e0a\u6362\u8f6e\u80ce\u201d\uff08\u8fd0\u884c\u65f6\u4fee\u6539\u53c2\u6570\uff09\u800c\u4e0d\u7ffb\u8f66\uff1f\u4ee5\u53ca\u5982\u4f55\u7aa5\u63a2 Jetty \u90a3\u201c\u5c01\u95ed\u201d\u7684\u5185\u5fc3\uff1f<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\u7b2c\u4e09\u7bc7\uff1a\u5b9e\u6218\u7ec6\u8282\u4e0e\u9ed1\u79d1\u6280\u5b9e\u73b0<\/h1>\n\n\n\n<h2 class=\"wp-block-heading\">1. \u5b89\u5168\u8c03\u53c2\u7b56\u7565\uff1a\u89e3\u5f00\u201c\u9e21\u751f\u86cb\uff0c\u86cb\u751f\u9e21\u201d\u7684\u6b7b\u9501<\/h2>\n\n\n\n<p>\u5728\u4fee\u6539\u7ebf\u7a0b\u6c60\u53c2\u6570\u65f6\uff0cJava \u7684 <code>ThreadPoolExecutor<\/code> \u6709\u4e00\u4e2a\u786c\u6027\u89c4\u5b9a\uff08Invariants\uff09\uff1a<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p><strong>\u6838\u5fc3\u7ebf\u7a0b\u6570 (Core) \u5fc5\u987b \u5c0f\u4e8e\u7b49\u4e8e \u6700\u5927\u7ebf\u7a0b\u6570 (Max)<\/strong><\/p>\n<\/blockquote>\n\n\n\n<p>\u5982\u679c\u4e0d\u9075\u5b88\u8fd9\u4e2a\u89c4\u5219\uff0c\u76f4\u63a5\u8c03\u7528 <code>set<\/code> \u65b9\u6cd5\uff0c\u4ee3\u7801\u4f1a\u629b\u51fa <code>IllegalArgumentException<\/code>\uff0c\u5bfc\u81f4\u4f60\u7684\u8c03\u53c2\u5931\u8d25\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1.1 \u573a\u666f\u63a8\u6f14\uff1a\u6269\u5bb9\u4e0e\u7f29\u5bb9\u7684\u9677\u9631<\/h3>\n\n\n\n<p>\u5047\u8bbe\u5f53\u524d\u914d\u7f6e\uff1aCore = 10, Max = 20\u3002<\/p>\n\n\n\n<p><strong>\u573a\u666f A\uff1a\u6211\u8981\u6269\u5bb9\uff08\u5e94\u5bf9\u5927\u4fc3\uff09<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u76ee\u6807\uff1aCore = 50, Max = 100\u3002<\/li>\n\n\n\n<li><strong>\u9519\u8bef\u64cd\u4f5c\uff1a<\/strong> \u5148\u8bbe\u7f6e <code>setCorePoolSize(50)<\/code>\u3002\n<ul class=\"wp-block-list\">\n<li><em>\u7ed3\u679c\uff1a<\/em> \u5d29\u4e86\uff01\u56e0\u4e3a\u6b64\u65f6 Max \u8fd8\u662f 20\uff0c50 > 20\uff0c\u8fdd\u53cd\u89c4\u5219\u3002<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>\u6b63\u786e\u64cd\u4f5c\uff1a<\/strong><strong>\u5148\u6269 Max\uff0c\u518d\u6269 Core\u3002<\/strong>\n<ol class=\"wp-block-list\">\n<li><code>setMaximumPoolSize(100)<\/code> (\u6b64\u65f6 Core=10, Max=100\uff0c\u5408\u6cd5)<\/li>\n\n\n\n<li><code>setCorePoolSize(50)<\/code> (\u6b64\u65f6 Core=50, Max=100\uff0c\u5408\u6cd5)<\/li>\n<\/ol>\n<\/li>\n<\/ul>\n\n\n\n<p><strong>\u573a\u666f B\uff1a\u6211\u8981\u7f29\u5bb9\uff08\u5927\u4fc3\u7ed3\u675f\uff09<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5f53\u524d\u72b6\u6001\uff1aCore = 50, Max = 100\u3002<\/li>\n\n\n\n<li>\u76ee\u6807\uff1aCore = 10, Max = 20\u3002<\/li>\n\n\n\n<li><strong>\u9519\u8bef\u64cd\u4f5c\uff1a<\/strong> \u5148\u8bbe\u7f6e <code>setMaximumPoolSize(20)<\/code>\u3002\n<ul class=\"wp-block-list\">\n<li><em>\u7ed3\u679c\uff1a<\/em> \u5d29\u4e86\uff01\u56e0\u4e3a\u6b64\u65f6 Core \u8fd8\u662f 50\uff0c50 > 20\uff0c\u8fdd\u53cd\u89c4\u5219\u3002<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>\u6b63\u786e\u64cd\u4f5c\uff1a<\/strong><strong>\u5148\u7f29 Core\uff0c\u518d\u7f29 Max\u3002<\/strong>\n<ol class=\"wp-block-list\">\n<li><code>setCorePoolSize(10)<\/code> (\u6b64\u65f6 Core=10, Max=100\uff0c\u5408\u6cd5)<\/li>\n\n\n\n<li><code>setMaximumPoolSize(20)<\/code> (\u6b64\u65f6 Core=10, Max=20\uff0c\u5408\u6cd5)<\/li>\n<\/ol>\n<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">1.2 \u4ee3\u7801\u5b9e\u73b0\u7684\u827a\u672f<\/h3>\n\n\n\n<p>\u6587\u6863\u4e2d\u7684 <code>TomcatWebThreadPoolService.updateThreadPool<\/code> \u65b9\u6cd5\u5b8c\u7f8e\u5730\u5b9e\u73b0\u4e86\u8fd9\u4e2a\u903b\u8f91\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ \u8fd9\u662f\u4e00\u4e2a\u975e\u5e38\u7ecf\u5178\u7684\u9632\u5751\u903b\u8f91\nif (config.getCorePoolSize() &gt; originalMaximumPoolSize) {\n    \/\/ 1. \u5982\u679c\u65b0\u7684\u6838\u5fc3\u6570 &gt; \u65e7\u7684\u6700\u5927\u6570\uff0c\u8bf4\u660e\u662f\u5927\u6b65\u6269\u5bb9\uff0c\u5fc5\u987b\u5148\u62c9\u9ad8\u5929\u82b1\u677f\uff08Max\uff09\n    tomcatExecutor.setMaximumPoolSize(config.getMaximumPoolSize());\n    tomcatExecutor.setCorePoolSize(config.getCorePoolSize());\n} else {\n    \/\/ 2. \u5176\u4ed6\u60c5\u51b5\uff08\u5305\u62ec\u7f29\u5bb9\uff0c\u6216\u8005\u5c0f\u5e45\u6269\u5bb9\uff09\uff0c\u5148\u8c03\u6574\u6838\u5fc3\u6570\u6bd4\u8f83\u5b89\u5168\n    tomcatExecutor.setCorePoolSize(config.getCorePoolSize());\n    tomcatExecutor.setMaximumPoolSize(config.getMaximumPoolSize());\n}<\/code><\/pre>\n\n\n\n<p><strong>Jetty \u7684\u6ce8\u610f\u4e8b\u9879\uff1a<\/strong><br>Jetty \u7684 <code>QueuedThreadPool<\/code> \u540c\u6837\u6709 <code>minThreads &lt;= maxThreads<\/code> \u7684\u7ea6\u675f\u3002\u903b\u8f91\u4e0e\u4e0a\u9762\u5b8c\u5168\u4e00\u81f4\uff0c\u53ea\u662f\u65b9\u6cd5\u540d\u6362\u6210\u4e86 <code>setMinThreads<\/code> \u548c <code>setMaxThreads<\/code>\u3002<\/p>\n\n\n\n<p><strong>\u5355\u4f4d\u7684\u5927\u5751\uff1a<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Tomcat:<\/strong> <code>setKeepAliveTime<\/code> \u5141\u8bb8\u4f60\u6307\u5b9a\u5355\u4f4d\uff08\u5982 <code>TimeUnit.SECONDS<\/code>\uff09\u3002<\/li>\n\n\n\n<li><strong>Jetty:<\/strong> <code>setIdleTimeout<\/code> \u53ea\u8981\u4e00\u4e2a <code>int<\/code>\uff0c\u9ed8\u8ba4\u5355\u4f4d\u662f<strong>\u6beb\u79d2<\/strong>\u3002<\/li>\n\n\n\n<li><em>\u4ee3\u7801\u7ec6\u8282\uff1a<\/em> \u5982\u679c\u4f60\u7684\u914d\u7f6e\u4e2d\u5fc3\u914d\u7684\u662f\u201c60\u79d2\u201d\uff0c\u4f20\u7ed9 Jetty \u65f6\u5fc5\u987b <code>* 1000<\/code>\u3002\u6e90\u7801\u4e2d\u6709\u4e00\u884c <code>config.getKeepAliveTime().intValue()<\/code>\uff0c\u8fd9\u91cc\u9700\u7ed3\u5408\u4e1a\u52a1\u914d\u7f6e\u786e\u8ba4\u5355\u4f4d\u662f\u5426\u5339\u914d\uff0c\u901a\u5e38\u5efa\u8bae\u5728 Service \u5c42\u505a\u7edf\u4e00\u7684\u65f6\u95f4\u5355\u4f4d\u8f6c\u6362\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\">2. \u9ed1\u79d1\u6280\u76d1\u63a7\uff1a\u53cd\u5c04\u7834\u89e3 Jetty \u7684\u201c\u79c1\u6709\u9886\u5730\u201d<\/h2>\n\n\n\n<p>\u5bf9\u4e8e\u76d1\u63a7\uff0c\u6211\u4eec\u8981\u89e3\u51b3\u7684\u662f<strong>\u53ef\u89c1\u6027<\/strong>\u95ee\u9898\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">2.1 Tomcat\uff1a\u5149\u660e\u6b63\u5927<\/h3>\n\n\n\n<p>Tomcat \u7528\u7684\u662f JDK \u6807\u51c6 API\uff0c\u6240\u4ee5\u83b7\u53d6\u76d1\u63a7\u6307\u6807\u975e\u5e38\u987a\u6ed1\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>getActiveCount()<\/code>: \u6b63\u5728\u5e72\u6d3b\u7684\u7ebf\u7a0b\u3002<\/li>\n\n\n\n<li><code>getQueue().size()<\/code>: \u6b63\u5728\u6392\u961f\u7684\u8bf7\u6c42\u3002<\/li>\n\n\n\n<li><code>getCompletedTaskCount()<\/code>: \u5386\u53f2\u603b\u5904\u7406\u91cf\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">2.2 Jetty\uff1a\u66b4\u529b\u53cd\u5c04<\/h3>\n\n\n\n<p>Jetty \u7684 <code>QueuedThreadPool<\/code> \u8bbe\u8ba1\u6bd4\u8f83\u5c01\u95ed\u3002\u5b83\u7684\u4efb\u52a1\u961f\u5217\u5b9a\u4e49\u662f\u8fd9\u6837\u7684\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>private final BlockingQueue&lt;Runnable&gt; _jobs;<\/code><\/pre>\n\n\n\n<p>\u5b83\u662f <code>private<\/code> \u7684\uff0c\u800c\u4e14\u6ca1\u6709 <code>getQueue()<\/code> \u8fd9\u6837\u7684\u516c\u5f00\u65b9\u6cd5\u8fd4\u56de\u6574\u4e2a\u961f\u5217\u5bf9\u8c61\u3002\u867d\u7136 Jetty \u63d0\u4f9b\u4e86 <code>getQueueSize()<\/code>\uff0c\u4f46\u5982\u679c\u6211\u4eec\u60f3\u77e5\u9053\u961f\u5217\u7684<strong>\u5269\u4f59\u5bb9\u91cf (remainingCapacity)<\/strong> \u6216\u8005<strong>\u62d2\u7edd\u7b56\u7565<\/strong>\uff0c\u516c\u5f00 API \u5c31\u4e0d\u591f\u7528\u4e86\u3002<\/p>\n\n\n\n<p><strong>oneThread \u7684\u89e3\u51b3\u65b9\u6848\uff1a<\/strong><br>\u4f7f\u7528 Java <strong>\u53cd\u5c04 (Reflection)<\/strong> \u66b4\u529b\u8bfb\u53d6\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ \u5bf9\u5e94\u6587\u6863 JettyWebThreadPoolService \u4e2d\u7684 getBasicMetrics \u65b9\u6cd5\n@SneakyThrows\npublic WebThreadPoolBaseMetrics getBasicMetrics() {\n    \/\/ 1. \u5f3a\u8f6c\u4e3a Jetty \u7ebf\u7a0b\u6c60\n    QueuedThreadPool jettyExecutor = (QueuedThreadPool) executor;\n\n    \/\/ 2. \u3010\u9ed1\u79d1\u6280\u3011\u5229\u7528\u53cd\u5c04\u5de5\u5177\uff0c\u5f3a\u884c\u8bfb\u53d6\u79c1\u6709\u5b57\u6bb5 \"_jobs\"\n    \/\/ \u6ce8\u610f\uff1a\u8fd9\u91cc\u7684\u5b57\u6bb5\u540d \"_jobs\" \u662f\u786c\u7f16\u7801\u7684\uff0c\u4f9d\u8d56\u4e8e Jetty \u6e90\u7801\u5b9e\u73b0\n    BlockingQueue jobs = (BlockingQueue) ReflectUtil.getFieldValue(jettyExecutor, \"_jobs\");\n\n    \/\/ 3. \u62ff\u5230\u961f\u5217\u5bf9\u8c61\u540e\uff0c\u5c31\u53ef\u4ee5\u4e3a\u6240\u6b32\u4e3a\u4e86\n    int remainingCapacity = jobs.remainingCapacity(); \/\/ \u8fd8\u80fd\u585e\u591a\u5c11\u8bf7\u6c42\n    \/\/ ...\n}<\/code><\/pre>\n\n\n\n<p><strong>\u98ce\u9669\u63d0\u793a\uff1a<\/strong><br>\u8fd9\u79cd\u505a\u6cd5\u867d\u7136\u5f3a\u5927\uff0c\u4f46\u6709\u4e00\u4e2a\u7f3a\u70b9\uff1a<strong>\u5bf9 Jetty \u7248\u672c\u654f\u611f<\/strong>\u3002\u5982\u679c\u54ea\u5929 Jetty \u5347\u7ea7\uff0c\u628a <code>_jobs<\/code> \u6539\u540d\u4e3a <code>_tasks<\/code>\uff0c\u8fd9\u6bb5\u4ee3\u7801\u5c31\u4f1a\u62a5\u9519\u3002\u4f46\u5728\u89e3\u51b3\u751f\u4ea7\u75db\u70b9\u9762\u524d\uff0c\u8fd9\u4e2a\u6536\u76ca\u901a\u5e38\u662f\u503c\u5f97\u7684\uff08\u800c\u4e14 Jetty \u6838\u5fc3\u5b57\u6bb5\u540d\u5f88\u5c11\u53d8\uff09\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">2.3 \u6307\u6807\u5bf9\u9f50<\/h3>\n\n\n\n<p>\u4e3a\u4e86\u8ba9\u76d1\u63a7\u5927\u76d8\u7edf\u4e00\uff0c\u4ee3\u7801\u505a\u4e86\u4e00\u5c42<strong>\u8bed\u4e49\u6620\u5c04<\/strong>\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u6807\u51c6\u8bed\u4e49 <code>activeCount<\/code> &lt;--> Jetty <code>getBusyThreads()<\/code><\/li>\n\n\n\n<li>\u6807\u51c6\u8bed\u4e49 <code>currentPoolSize<\/code> &lt;--> Jetty <code>getThreads()<\/code><\/li>\n<\/ul>\n\n\n\n<p>\u8fd9\u6837\u8fd0\u7ef4\u4eba\u5458\u5728\u770b Grafana \u9762\u677f\u65f6\uff0c\u4e0d\u9700\u8981\u5173\u5fc3\u5e95\u5c42\u662f\u54ea\u4e2a\u5bb9\u5668\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">3. \u6700\u540e\u4e00\u5757\u62fc\u56fe\uff1a\u8c01\u6765\u89e6\u53d1\uff1f<\/h2>\n\n\n\n<p>\u73b0\u5728\u8f66\u9020\u597d\u4e86\uff08\u52a8\u6001\u8c03\u53c2\u903b\u8f91\uff09\uff0c\u4eea\u8868\u76d8\u4e5f\u88c5\u597d\u4e86\uff08\u76d1\u63a7\u6307\u6807\uff09\uff0c\u8fd8\u9700\u8981\u4e00\u4e2a<strong>\u9a7e\u9a76\u5458<\/strong>\u3002<\/p>\n\n\n\n<p>\u8fd9\u4e2a\u9a7e\u9a76\u5458\u5c31\u662f<strong>\u914d\u7f6e\u4e2d\u5fc3\u76d1\u542c\u5668\uff08Nacos\/Apollo Listener\uff09<\/strong>\u3002<\/p>\n\n\n\n<p>\u867d\u7136\u6587\u6863\u91cc\u6ca1\u6709\u8be6\u7ec6\u5c55\u793a\u76d1\u542c\u5668\u7684\u4ee3\u7801\uff0c\u4f46\u5b8c\u6574\u7684\u94fe\u8def\u662f\u8fd9\u6837\u7684\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Nacos \u540e\u53f0\uff1a<\/strong> \u4f60\u4fee\u6539\u4e86 YAML \u914d\u7f6e <code>tomcat.threads.max = 500<\/code> \u5e76\u53d1\u5e03\u3002<\/li>\n\n\n\n<li><strong>Spring Cloud\uff1a<\/strong> \u63a5\u6536\u5230 <code>RefreshEvent<\/code>\u3002<\/li>\n\n\n\n<li><strong>oneThread \u9002\u914d\u5668\uff1a<\/strong> \u76d1\u542c\u5230\u914d\u7f6e\u53d8\u66f4\u4e8b\u4ef6\u3002<\/li>\n\n\n\n<li><strong>\u8c03\u7528 Service\uff1a<\/strong> <code>webThreadPoolService.updateThreadPool(newConfig)<\/code>\u3002<\/li>\n\n\n\n<li><strong>\u843d\u5730\uff1a<\/strong> \u6267\u884c\u4e0a\u8ff0\u7684 <code>if-else<\/code> \u5b89\u5168\u903b\u8f91\uff0cTomcat \u5e95\u5c42\u53c2\u6570\u77ac\u95f4\u53d8\u5316\u3002<\/li>\n\n\n\n<li><strong>\u7ed3\u679c\uff1a<\/strong> \u65b0\u8fdb\u6765\u7684 HTTP \u8bf7\u6c42\u7acb\u523b\u4eab\u53d7\u5230 500 \u4e2a\u7ebf\u7a0b\u7684\u5904\u7406\u80fd\u529b\uff0c\u6392\u961f\u77ac\u95f4\u6d88\u9664\u3002<\/li>\n<\/ol>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u5168\u6587\u603b\u7ed3<\/h2>\n\n\n\n<p>\u901a\u8fc7\u8fd9\u4e09\u7bc7\u7684\u6df1\u5ea6\u89e3\u6790\uff0c\u6211\u4eec\u5b8c\u6574\u62c6\u89e3\u4e86 SpringBoot Web \u5bb9\u5668\u52a8\u6001\u7ebf\u7a0b\u6c60\u7684\u5b9e\u73b0\u539f\u7406\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u539f\u7406\u7bc7\uff1a<\/strong> \u8ba4\u6e05\u4e86 Web \u5bb9\u5668\u7ebf\u7a0b\u6c60\u662f\u6d41\u91cf\u5165\u53e3\u7684\u74f6\u9888\uff0c\u7406\u89e3\u4e86 Tomcat\uff08\u6269\u5bb9\u4f18\u5148\uff09\u548c Jetty\uff08\u961f\u5217\u4f18\u5148\uff09\u7684\u5e95\u5c42\u5dee\u5f02\u3002<\/li>\n\n\n\n<li><strong>\u67b6\u6784\u7bc7\uff1a<\/strong> \u5b66\u4e60\u4e86\u5229\u7528 Spring <code>@Conditional<\/code> \u8fdb\u884c\u81ea\u52a8\u88c5\u914d\uff0c\u4ee5\u53ca\u9002\u914d\u5668\u6a21\u5f0f\u7edf\u4e00\u63a5\u53e3\u7684\u8bbe\u8ba1\u667a\u6167\u3002<\/li>\n\n\n\n<li><strong>\u5b9e\u6218\u7bc7\uff1a<\/strong> \u638c\u63e1\u4e86\u201c\u5148\u6269 Max \u540e\u6269 Core\u201d\u7684\u5b89\u5168\u8c03\u53c2\u987a\u5e8f\uff0c\u4ee5\u53ca\u5229\u7528\u53cd\u5c04\u83b7\u53d6 Jetty \u79c1\u6709\u961f\u5217\u6570\u636e\u7684\u9ed1\u79d1\u6280\u3002<\/li>\n<\/ol>\n\n\n\n<p> <\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8fd9\u662f\u4e00\u4e2a\u975e\u5e38\u786c\u6838\u4e14\u5b9e\u7528\u7684\u6280\u672f\u67b6\u6784\u6587\u6863\uff0c\u4e3b\u8981\u89e3\u51b3\u4e86 SpringBoot \u5e94\u7528\u5728\u751f\u4ea7\u73af\u5883\u4e2dWeb \u5bb9\u5668\uff08Tomcat\/Jetty\uff09\u7ebf &#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"emotion":"","emotion_color":"","title_style":"","license":"","footnotes":""},"categories":[3],"tags":[19,20],"class_list":["post-1793","post","type-post","status-publish","format-standard","hentry","category-3","tag-java","tag-20"],"_links":{"self":[{"href":"https:\/\/eve2333.top\/index.php?rest_route=\/wp\/v2\/posts\/1793","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/eve2333.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/eve2333.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/eve2333.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/eve2333.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1793"}],"version-history":[{"count":9,"href":"https:\/\/eve2333.top\/index.php?rest_route=\/wp\/v2\/posts\/1793\/revisions"}],"predecessor-version":[{"id":1807,"href":"https:\/\/eve2333.top\/index.php?rest_route=\/wp\/v2\/posts\/1793\/revisions\/1807"}],"wp:attachment":[{"href":"https:\/\/eve2333.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1793"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/eve2333.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1793"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/eve2333.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1793"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}