{"id":47757,"date":"2025-07-30T09:12:22","date_gmt":"2025-07-30T01:12:22","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/47757.html"},"modified":"2025-07-30T09:12:22","modified_gmt":"2025-07-30T01:12:22","slug":"%e4%b8%ba%e4%bb%80%e4%b9%88%e3%80%8a%e9%98%bf%e9%87%8cjava%e5%bc%80%e5%8f%91%e6%89%8b%e5%86%8c%e3%80%8b%e5%bc%ba%e5%88%b6%e7%a6%81%e7%94%a8executors%ef%bc%9f%e7%9c%8b%e5%ae%8c%e8%bf%99%e7%af%87","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/47757.html","title":{"rendered":"\u4e3a\u4ec0\u4e48\u300a\u963f\u91ccJava\u5f00\u53d1\u624b\u518c\u300b\u5f3a\u5236\u7981\u7528Executors\uff1f\u770b\u5b8c\u8fd9\u7bc7\u8840\u6cea\u53f2\u4f60\u5c31\u61c2\u4e86\u3002"},"content":{"rendered":"<p>\u90a3\u662f\u4e00\u4e2a\u53cc\u5341\u4e00\u7684\u51cc\u6668&#xff0c;\u4f5c\u6218\u5ba4\u91cc\u706f\u706b\u901a\u660e&#xff0c;\u5927\u5c4f\u5e55\u4e0a\u7684GMV\u6b63\u5728\u75af\u72c2\u8df3\u52a8\u3002\u7136\u800c&#xff0c;\u5c31\u5728\u96f6\u70b9\u9ad8\u5cf0\u8fc7\u53bb\u4e0d\u4e45&#xff0c;\u544a\u8b66\u7cfb\u7edf\u7a81\u7136\u50cf\u75af\u4e86\u4e00\u6837&#xff0c;\u523a\u8033\u7684\u7ea2\u8272\u8b66\u62a5\u54cd\u5f7b\u6574\u4e2a\u697c\u5c42\u2014\u2014\u8ba2\u5355\u670d\u52a1\u96ea\u5d29&#xff0c;P0\u7ea7\u6545\u969c&#xff01;<\/p>\n<p>\u6240\u6709\u4eba\u7684\u5fc3\u90fd\u6c89\u5230\u4e86\u8c37\u5e95\u3002\u7ecf\u8fc7\u534a\u5c0f\u65f6\u7684\u7d27\u6025\u6392\u67e5&#xff0c;\u6700\u7ec8\u7684\u7f6a\u9b41\u7978\u9996\u8ba9\u6240\u6709\u4eba\u5927\u8dcc\u773c\u955c&#xff1a;\u4e00\u4e2a\u65b0\u4e0a\u7ebf\u7684\u201c\u8ba2\u5355\u77ed\u4fe1\u901a\u77e5\u201d\u529f\u80fd\u3002\u8d1f\u8d23\u8fd9\u5757\u4ee3\u7801\u7684\u5b9e\u4e60\u751f\u5c0fA\u8138\u8272\u60e8\u767d&#xff0c;\u5583\u5583\u81ea\u8bed&#xff1a;\u201c\u4e0d\u53ef\u80fd\u554a&#xff0c;\u6211\u7528\u4e86\u7ebf\u7a0b\u6c60\u505a\u7684\u5f02\u6b65\u53d1\u9001&#xff0c;\u5c31\u662f\u4e00\u884cExecutors.newCachedThreadPool()&#xff0c;\u600e\u4e48\u4f1a\u5d29\u5462&#xff1f;\u201d<\/p>\n<p>\u6211\u7acb\u523b\u6293\u53d6\u4e86\u7ebf\u7a0b\u5806\u6808&#xff0c;\u770b\u7740\u90a3\u5bc6\u5bc6\u9ebb\u9ebb\u3001\u6210\u5343\u4e0a\u4e07\u4e2a\u540d\u4e3apool-x-thread-x\u7684\u7ebf\u7a0b&#xff0c;\u6211\u53f9\u4e86\u53e3\u6c14\u3002\u5c0fA&#xff0c;\u4f60\u7528\u4e00\u884c\u4ee3\u7801&#xff0c;\u51e0\u4e4e\u846c\u9001\u4e86\u6211\u4eec\u5168\u5e74\u7684\u52aa\u529b\u3002<\/p>\n<h3>1. Executors\u7684\u751c\u871c\u9677\u9631&#xff0c;\u901a\u5f80OOM\u7684\u5730\u72f1\u4e4b\u8def<\/h3>\n<p>\u5f88\u591a\u65b0\u624b&#xff0c;\u751a\u81f3\u4e00\u4e9b\u8001\u9e1f&#xff0c;\u90fd\u559c\u6b22\u7528Executors\u7684\u9759\u6001\u65b9\u6cd5\u6765\u521b\u5efa\u7ebf\u7a0b\u6c60\u3002\u56e0\u4e3a\u5b83\u7b80\u5355\u3001\u65b9\u4fbf&#xff0c;\u4e00\u884c\u641e\u5b9a\u3002\u4f46\u300a\u963f\u91cc\u5df4\u5df4Java\u5f00\u53d1\u624b\u518c\u300b\u4e2d\u660e\u786e\u89c4\u5b9a\u201c\u5f3a\u5236\u7981\u7528\u201d&#xff0c;\u8fd9\u80cc\u540e\u662f\u65e0\u6570\u8840\u6dcb\u6dcb\u7684\u6559\u8bad\u3002<\/p>\n<p>\u8ba9\u6211\u4eec\u6765\u770b\u770b&#xff0c;\u4e24\u79cd\u201c\u77ac\u95f4\u7206\u70b8\u201d\u7684\u5199\u6cd5\u3002<\/p>\n<h4>1.1 newCachedThreadPool -&gt; \u65e0\u9650\u7ebf\u7a0b&#xff0c;\u6491\u7206\u7cfb\u7edf<\/h4>\n<p>\u5c0fA\u72af\u7684\u6b63\u662f\u8fd9\u4e2a\u9519\u8bef\u3002\u4ed6\u7684\u77ed\u4fe1\u53d1\u9001\u670d\u52a1&#xff0c;\u5728\u6b63\u5e38\u60c5\u51b5\u4e0b100\u6beb\u79d2\u5c31\u80fd\u5b8c\u6210\u3002\u4f46\u5728\u53cc\u5341\u4e00\u90a3\u5929&#xff0c;\u77ed\u4fe1\u670d\u52a1\u5546\u7684\u63a5\u53e3\u51fa\u73b0\u5ef6\u8fdf&#xff0c;\u8d85\u65f6\u65f6\u95f4\u88ab\u8bbe\u7f6e\u6210\u4e8660\u79d2\u3002<\/p>\n<p><span class=\"token comment\">\/\/ \u770b\u8d77\u6765\u5c81\u6708\u9759\u597d&#xff0c;\u5b9e\u5219\u6697\u85cf\u6740\u673a<\/span><br \/>\n<span class=\"token class-name\">ThreadPoolExecutor<\/span> threadPool <span class=\"token operator\">&#061;<\/span> <span class=\"token punctuation\">(<\/span><span class=\"token class-name\">ThreadPoolExecutor<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token class-name\">Executors<\/span><span class=\"token punctuation\">.<\/span><span class=\"token function\">newCachedThreadPool<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token keyword\">for<\/span> <span class=\"token punctuation\">(<\/span><span class=\"token keyword\">int<\/span> i <span class=\"token operator\">&#061;<\/span> <span class=\"token number\">0<\/span><span class=\"token punctuation\">;<\/span> i <span class=\"token operator\">&lt;<\/span> <span class=\"token number\">10000<\/span><span class=\"token punctuation\">;<\/span> i<span class=\"token operator\">&#043;&#043;<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n    <span class=\"token comment\">\/\/ \u9ad8\u5cf0\u671f&#xff0c;\u5927\u91cf\u8bf7\u6c42\u6d8c\u5165<\/span><br \/>\n    threadPool<span class=\"token punctuation\">.<\/span><span class=\"token function\">execute<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token operator\">-&gt;<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n        <span class=\"token comment\">\/\/ \u8c03\u7528\u4e00\u4e2a\u8017\u65f660\u79d2\u7684\u5916\u90e8\u77ed\u4fe1\u63a5\u53e3&#8230;<\/span><br \/>\n        <span class=\"token keyword\">try<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n            <span class=\"token class-name\">TimeUnit<\/span><span class=\"token punctuation\">.<\/span>SECONDS<span class=\"token punctuation\">.<\/span><span class=\"token function\">sleep<\/span><span class=\"token punctuation\">(<\/span><span class=\"token number\">60<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        <span class=\"token punctuation\">}<\/span> <span class=\"token keyword\">catch<\/span> <span class=\"token punctuation\">(<\/span><span class=\"token class-name\">InterruptedException<\/span> e<span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span> <span class=\"token comment\">\/* &#8230; *\/<\/span> <span class=\"token punctuation\">}<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<p>\u707e\u96be\u540e\u679c&#xff1a;newCachedThreadPool\u7684\u6838\u5fc3\u7ebf\u7a0b\u6570\u662f0&#xff0c;\u6700\u5927\u7ebf\u7a0b\u6570\u662fInteger.MAX_VALUE&#xff08;\u7ea6\u7b49\u4e8e\u65e0\u9650&#xff09;\u3002\u5b83\u7684\u5de5\u4f5c\u961f\u5217SynchronousQueue\u4e0d\u5b58\u50a8\u4efb\u4f55\u4efb\u52a1\u3002\u8fd9\u610f\u5473\u7740\u6765\u4e00\u4e2a\u4efb\u52a1&#xff0c;\u5982\u679c\u6ca1\u6709\u7a7a\u95f2\u7ebf\u7a0b&#xff0c;\u5c31\u7acb\u523b\u521b\u5efa\u4e00\u4e2a\u65b0\u7ebf\u7a0b\u3002<\/p>\n<p>\u9ad8\u5cf0\u671f\u6bcf\u5206\u949f\u6d8c\u51656000\u4e2a\u8ba2\u5355&#xff0c;\u5c31\u9700\u8981\u521b\u5efa6000\u4e2a\u7ebf\u7a0b\u3002\u6bcf\u4e2a\u7ebf\u7a0b\u90fd\u8981\u6d88\u8017\u5927\u7ea61MB\u7684\u6808\u5185\u5b58&#xff0c;\u670d\u52a1\u5668\u77ac\u95f4\u88ab\u6570\u5343\u4e2a\u7ebf\u7a0b\u69a8\u5e72&#xff0c;\u6700\u7ec8\u629b\u51fajava.lang.OutOfMemoryError: unable to create new native thread&#xff0c;\u6574\u4e2a\u5e94\u7528\u8f70\u7136\u5012\u584c\u3002<\/p>\n<h4>1.2 newFixedThreadPool -&gt; \u65e0\u9650\u961f\u5217&#xff0c;\u6491\u7206\u5185\u5b58<\/h4>\n<p>\u6709\u4e9b\u540c\u5b66\u53ef\u80fd\u4f1a\u8bf4&#xff1a;\u201c\u90a3\u6211\u7528newFixedThreadPool\u9650\u5236\u7ebf\u7a0b\u6570\u4e0d\u5c31\u884c\u4e86&#xff1f;\u201d\u5929\u771f&#xff01;<\/p>\n<p><span class=\"token comment\">\/\/ \u7ebf\u7a0b\u6570\u662f\u56fa\u5b9a\u7684&#xff0c;\u4f46\u961f\u5217\u662f\u65e0\u5e95\u6d1e<\/span><br \/>\n<span class=\"token class-name\">ThreadPoolExecutor<\/span> threadPool <span class=\"token operator\">&#061;<\/span> <span class=\"token punctuation\">(<\/span><span class=\"token class-name\">ThreadPoolExecutor<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token class-name\">Executors<\/span><span class=\"token punctuation\">.<\/span><span class=\"token function\">newFixedThreadPool<\/span><span class=\"token punctuation\">(<\/span><span class=\"token number\">10<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token keyword\">for<\/span> <span class=\"token punctuation\">(<\/span><span class=\"token keyword\">int<\/span> i <span class=\"token operator\">&#061;<\/span> <span class=\"token number\">0<\/span><span class=\"token punctuation\">;<\/span> i <span class=\"token operator\">&lt;<\/span> <span class=\"token number\">100_000_000<\/span><span class=\"token punctuation\">;<\/span> i<span class=\"token operator\">&#043;&#043;<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n    threadPool<span class=\"token punctuation\">.<\/span><span class=\"token function\">execute<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token operator\">-&gt;<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n        <span class=\"token comment\">\/\/ \u521b\u5efa\u4e00\u4e2a\u5927\u5bf9\u8c61&#xff0c;\u6a21\u62df\u4e1a\u52a1\u6570\u636e<\/span><br \/>\n        <span class=\"token class-name\">String<\/span> payload <span class=\"token operator\">&#061;<\/span> UUID<span class=\"token punctuation\">.<\/span><span class=\"token function\">randomUUID<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">.<\/span><span class=\"token function\">toString<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        <span class=\"token keyword\">try<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n            <span class=\"token class-name\">TimeUnit<\/span><span class=\"token punctuation\">.<\/span>HOURS<span class=\"token punctuation\">.<\/span><span class=\"token function\">sleep<\/span><span class=\"token punctuation\">(<\/span><span class=\"token number\">1<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        <span class=\"token punctuation\">}<\/span> <span class=\"token keyword\">catch<\/span> <span class=\"token punctuation\">(<\/span><span class=\"token class-name\">InterruptedException<\/span> e<span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span> <span class=\"token comment\">\/* &#8230; *\/<\/span> <span class=\"token punctuation\">}<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<p>\u707e\u96be\u540e\u679c&#xff1a;newFixedThreadPool\u7684\u7ebf\u7a0b\u6570\u662f\u56fa\u5b9a\u7684&#xff0c;\u4f46\u5b83\u7684\u5de5\u4f5c\u961f\u5217LinkedBlockingQueue\u7684\u5bb9\u91cf\u662fInteger.MAX_VALUE\u3002\u5982\u679c\u4efb\u52a1\u5904\u7406\u901f\u5ea6\u8ddf\u4e0d\u4e0a\u63d0\u4ea4\u901f\u5ea6&#xff0c;\u4efb\u52a1\u5c31\u4f1a\u5728\u961f\u5217\u91cc\u65e0\u9650\u5806\u79ef&#xff0c;\u6700\u7ec8\u628a\u4f60\u7684\u5806\u5185\u5b58\u6491\u7206&#xff0c;\u629b\u51faOutOfMemoryError: Java heap space\u3002<\/p>\n<h4>1.3 \u4f60\u7684\u5730\u76d8&#xff0c;\u4f60\u5fc5\u987b\u505a\u4e3b&#xff01;<\/h4>\n<p>Executors\u7684\u4fbf\u5229\u65b9\u6cd5&#xff0c;\u672c\u8d28\u4e0a\u662f\u9690\u85cf\u4e86\u7ebf\u7a0b\u6c60\u6700\u91cd\u8981\u7684\u51e0\u4e2a\u53c2\u6570&#xff0c;\u8ba9\u4f60\u5931\u53bb\u4e86\u5bf9\u7ebf\u7a0b\u6c60\u884c\u4e3a\u7684\u63a7\u5236\u6743&#xff0c;\u8fd9\u5728\u751f\u4ea7\u73af\u5883\u4e2d\u662f\u6781\u5176\u5371\u9669\u7684\u3002\u6b63\u786e\u7684\u505a\u6cd5\u5e94\u8be5\u662f\u624b\u52a8\u521b\u5efa&#xff0c;\u505a\u5230\u5fc3\u4e2d\u6709\u6570\u3002<\/p>\n<p><span class=\"token comment\">\/\/ \u4f7f\u7528 guava \u7684 ThreadFactoryBuilder<\/span><br \/>\n<span class=\"token class-name\">ThreadFactory<\/span> threadFactory <span class=\"token operator\">&#061;<\/span> <span class=\"token keyword\">new<\/span> <span class=\"token class-name\">ThreadFactoryBuilder<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">.<\/span><span class=\"token function\">setNameFormat<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;order-sms-pool-%d&#034;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">.<\/span><span class=\"token function\">build<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token class-name\">ThreadPoolExecutor<\/span> threadPool <span class=\"token operator\">&#061;<\/span> <span class=\"token keyword\">new<\/span> <span class=\"token class-name\">ThreadPoolExecutor<\/span><span class=\"token punctuation\">(<\/span><br \/>\n    <span class=\"token number\">10<\/span><span class=\"token punctuation\">,<\/span>                             <span class=\"token comment\">\/\/ corePoolSize: \u6838\u5fc3\u7ebf\u7a0b\u6570<\/span><br \/>\n    <span class=\"token number\">50<\/span><span class=\"token punctuation\">,<\/span>                             <span class=\"token comment\">\/\/ maximumPoolSize: \u6700\u5927\u7ebf\u7a0b\u6570<\/span><br \/>\n    <span class=\"token number\">60L<\/span><span class=\"token punctuation\">,<\/span>                            <span class=\"token comment\">\/\/ keepAliveTime: \u7a7a\u95f2\u7ebf\u7a0b\u5b58\u6d3b\u65f6\u95f4<\/span><br \/>\n    <span class=\"token class-name\">TimeUnit<\/span><span class=\"token punctuation\">.<\/span>SECONDS<span class=\"token punctuation\">,<\/span><br \/>\n    <span class=\"token keyword\">new<\/span> <span class=\"token class-name\">ArrayBlockingQueue<\/span><span class=\"token generics\"><span class=\"token punctuation\">&lt;<\/span><span class=\"token punctuation\">&gt;<\/span><\/span><span class=\"token punctuation\">(<\/span><span class=\"token number\">1000<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token comment\">\/\/ workQueue: \u6709\u754c\u963b\u585e\u961f\u5217<\/span><br \/>\n    threadFactory<span class=\"token punctuation\">,<\/span><br \/>\n    <span class=\"token keyword\">new<\/span> <span class=\"token class-name\">ThreadPoolExecutor<span class=\"token punctuation\">.<\/span>AbortPolicy<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token comment\">\/\/ rejectedExecutionHandler: \u62d2\u7edd\u7b56\u7565<\/span><br \/>\n<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>\u6838\u5fc3&#xff1a;\u5fc5\u987b\u624b\u52a8\u521b\u5efaThreadPoolExecutor&#xff0c;\u660e\u786e\u6307\u5b9a\u6838\u5fc3\u7ebf\u7a0b\u6570\u3001\u6700\u5927\u7ebf\u7a0b\u6570\u3001\u6709\u754c\u961f\u5217\u548c\u62d2\u7edd\u7b56\u7565&#xff0c;\u5e76\u7ed9\u7ebf\u7a0b\u8d77\u4e00\u4e2a\u6709\u610f\u4e49\u7684\u540d\u5b57&#xff08;\u5982order-sms-pool-%d&#xff09;&#xff0c;\u8fd9\u6837\u5728\u6392\u67e5\u95ee\u9898\u65f6&#xff0c;\u4f60\u4e00\u773c\u5c31\u80fd\u770b\u51fa\u662f\u54ea\u4e2a\u4e1a\u52a1\u7684\u7ebf\u7a0b\u51fa\u4e86\u95ee\u9898\u3002<\/p>\n<h3>2. \u5047\u6027\u590d\u7528&#xff0c;\u90a3\u4e2a\u88ab\u53cd\u590d\u521b\u5efa\u7684\u201c\u4e00\u6b21\u6027\u201d\u7ebf\u7a0b\u6c60<\/h3>\n<p>\u4e8b\u6545\u4e4b\u540e&#xff0c;\u56e2\u961f\u7981\u6b62\u4e86Executors\u3002\u4f46\u4e0d\u4e45\u540e&#xff0c;\u53e6\u4e00\u4e2a\u9879\u76ee\u53c8\u62a5\u8b66\u4e86&#xff1a;\u7ebf\u7a0b\u6570\u9891\u7e41\u6296\u52a8&#xff0c;\u65f6\u4e0d\u65f6\u98d9\u5347\u5230\u51e0\u5343\u3002\u6293\u53d6\u7ebf\u7a0b\u5806\u6808\u540e\u53d1\u73b0&#xff0c;\u5185\u5b58\u91cc\u7adf\u7136\u6709\u4e0a\u5343\u4e2a\u7ebf\u7a0b\u6c60\u5b9e\u4f8b&#xff01;<\/p>\n<p>\u4e1a\u52a1\u4ee3\u7801\u770b\u8d77\u6765\u5f88\u6b63\u5e38&#xff0c;\u8c03\u7528\u4e00\u4e2aThreadPoolHelper\u6765\u83b7\u53d6\u7ebf\u7a0b\u6c60\u3002<\/p>\n<p><span class=\"token comment\">\/\/ \u4e1a\u52a1\u4ee3\u7801<\/span><br \/>\n<span class=\"token class-name\">ThreadPoolExecutor<\/span> threadPool <span class=\"token operator\">&#061;<\/span> <span class=\"token class-name\">ThreadPoolHelper<\/span><span class=\"token punctuation\">.<\/span><span class=\"token function\">getThreadPool<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\nthreadPool<span class=\"token punctuation\">.<\/span><span class=\"token function\">execute<\/span><span class=\"token punctuation\">(<\/span><span class=\"token comment\">\/* &#8230; *\/<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token comment\">\/\/ \u8f85\u52a9\u7c7b\u7684\u5b9e\u73b0<\/span><br \/>\n<span class=\"token keyword\">class<\/span> <span class=\"token class-name\">ThreadPoolHelper<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n    <span class=\"token keyword\">public<\/span> <span class=\"token keyword\">static<\/span> <span class=\"token class-name\">ThreadPoolExecutor<\/span> <span class=\"token function\">getThreadPool<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n        <span class=\"token comment\">\/\/ \u707e\u96be\u7684\u6839\u6e90&#xff1a;\u6bcf\u6b21\u8c03\u7528\u90fd\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u7ebf\u7a0b\u6c60&#xff01;<\/span><br \/>\n        <span class=\"token keyword\">return<\/span> <span class=\"token punctuation\">(<\/span><span class=\"token class-name\">ThreadPoolExecutor<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token class-name\">Executors<\/span><span class=\"token punctuation\">.<\/span><span class=\"token function\">newCachedThreadPool<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<p>\u707e\u96be\u540e\u679c&#xff1a;\u6bcf\u6b21\u8c03\u7528getThreadPool()\u90fd\u5728\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u7ebf\u7a0b\u6c60\u3002\u7ebf\u7a0b\u6c60\u7684\u610f\u4e49\u5c31\u5728\u4e8e\u590d\u7528&#xff0c;\u8fd9\u79cd\u5199\u6cd5\u6bd4\u4e0d\u7528\u7ebf\u7a0b\u6c60\u8fd8\u7cdf\u7cd5&#xff0c;\u56e0\u4e3a\u5b83\u4e0d\u4ec5\u6ca1\u6709\u590d\u7528\u7ebf\u7a0b&#xff0c;\u8fd8\u5236\u9020\u4e86\u5927\u91cf\u7684\u7ebf\u7a0b\u6c60\u5bf9\u8c61\u548c\u5783\u573e\u56de\u6536\u538b\u529b\u3002<\/p>\n<p>\u6c60\u5316\u6280\u672f\u7684\u6838\u5fc3\u662f\u590d\u7528&#xff1a;\u7ebf\u7a0b\u6c60\u3001\u8fde\u63a5\u6c60\u7b49\u6280\u672f\u7684\u7cbe\u9ad3\u5728\u4e8e\u7f13\u5b58\u548c\u590d\u7528\u6602\u8d35\u7684\u5bf9\u8c61\u3002\u6bcf\u6b21\u90fdnew\u4e00\u4e2a&#xff0c;\u5b8c\u5168\u8fdd\u80cc\u4e86\u5176\u8bbe\u8ba1\u521d\u8877\u3002<\/p>\n<p>\u53c2\u8003\u4ee3\u7801\u5982\u4e0b&#xff1a;<\/p>\n<p><span class=\"token keyword\">class<\/span> <span class=\"token class-name\">ThreadPoolHelper<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n    <span class=\"token comment\">\/\/ \u4f7f\u7528 static final \u786e\u4fdd\u7ebf\u7a0b\u6c60\u5168\u5c40\u552f\u4e00\u4e14\u4e0d\u53ef\u53d8<\/span><br \/>\n    <span class=\"token keyword\">private<\/span> <span class=\"token keyword\">static<\/span> <span class=\"token keyword\">final<\/span> <span class=\"token class-name\">ThreadPoolExecutor<\/span> THREAD_POOL <span class=\"token operator\">&#061;<\/span> <span class=\"token keyword\">new<\/span> <span class=\"token class-name\">ThreadPoolExecutor<\/span><span class=\"token punctuation\">(<\/span><br \/>\n        <span class=\"token number\">10<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token number\">50<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token number\">60L<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token class-name\">TimeUnit<\/span><span class=\"token punctuation\">.<\/span>SECONDS<span class=\"token punctuation\">,<\/span> <span class=\"token keyword\">new<\/span> <span class=\"token class-name\">ArrayBlockingQueue<\/span><span class=\"token generics\"><span class=\"token punctuation\">&lt;<\/span><span class=\"token punctuation\">&gt;<\/span><\/span><span class=\"token punctuation\">(<\/span><span class=\"token number\">1000<\/span><span class=\"token punctuation\">)<\/span><br \/>\n    <span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>    <span class=\"token keyword\">public<\/span> <span class=\"token keyword\">static<\/span> <span class=\"token class-name\">ThreadPoolExecutor<\/span> <span class=\"token function\">getThreadPool<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n        <span class=\"token keyword\">return<\/span> THREAD_POOL<span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<h3>3. \u7ebf\u7a0b\u6c60\u5185\u6218&#xff0c;IO\u4efb\u52a1\u4e0eCPU\u4efb\u52a1\u7684\u6b8a\u6b7b\u640f\u6597<\/h3>\n<p>\u6700\u540e\u4e00\u4e2a\u4e8b\u6545&#xff0c;\u66f4\u52a0\u9690\u853d\u3002\u7528\u6237\u53cd\u9988\u67d0\u4e2a\u67e5\u8be2\u63a5\u53e3&#xff08;\u7eaf\u5185\u5b58\u8ba1\u7b97&#xff09;\u8d8a\u6765\u8d8a\u6162&#xff0c;\u4ece\u51e0\u5341\u6beb\u79d2\u52a3\u5316\u5230\u597d\u51e0\u79d2\u3002CPU\u4f7f\u7528\u7387\u4e0d\u9ad8&#xff0c;\u7cfb\u7edf\u4e5f\u6ca1\u6709\u5d29\u6e83&#xff0c;\u4f46\u5c31\u662f\u6162\u5f97\u79bb\u8c31\u3002<\/p>\n<p>\u6df7\u7528\u7ebf\u7a0b\u6c60\u7684\u201c\u60b2\u60e8\u4e16\u754c\u201d&#xff1a;\u6392\u67e5\u53d1\u73b0&#xff0c;\u8fd9\u4e2a\u201c\u5012\u9709\u201d\u7684\u67e5\u8be2\u4efb\u52a1&#xff0c;\u548c\u4e00\u4e2a\u540e\u53f0\u7684\u201c\u6587\u4ef6\u6279\u5904\u7406\u201d\u4efb\u52a1&#xff0c;\u5171\u7528\u4e86\u4e00\u4e2a\u7ebf\u7a0b\u6c60\u3002<\/p>\n<p><span class=\"token comment\">\/\/ \u4e00\u4e2a\u88abIO\u5bc6\u96c6\u578b\u548cCPU\u5bc6\u96c6\u578b\u4efb\u52a1\u5171\u4eab\u7684\u7ebf\u7a0b\u6c60<\/span><br \/>\n<span class=\"token keyword\">private<\/span> <span class=\"token keyword\">static<\/span> <span class=\"token class-name\">ThreadPoolExecutor<\/span> sharedPool <span class=\"token operator\">&#061;<\/span> <span class=\"token keyword\">new<\/span> <span class=\"token class-name\">ThreadPoolExecutor<\/span><span class=\"token punctuation\">(<\/span><br \/>\n    <span class=\"token number\">2<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token number\">2<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token number\">1<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token class-name\">TimeUnit<\/span><span class=\"token punctuation\">.<\/span>HOURS<span class=\"token punctuation\">,<\/span> <span class=\"token keyword\">new<\/span> <span class=\"token class-name\">ArrayBlockingQueue<\/span><span class=\"token generics\"><span class=\"token punctuation\">&lt;<\/span><span class=\"token punctuation\">&gt;<\/span><\/span><span class=\"token punctuation\">(<\/span><span class=\"token number\">100<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">,<\/span><br \/>\n    <span class=\"token keyword\">new<\/span> <span class=\"token class-name\">ThreadPoolExecutor<span class=\"token punctuation\">.<\/span>CallerRunsPolicy<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token comment\">\/\/ \u6ce8\u610f\u8fd9\u4e2a\u62d2\u7edd\u7b56\u7565&#xff01;<\/span><br \/>\n<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token comment\">\/\/ \u4efb\u52a11&#xff1a;\u540e\u53f0\u6279\u5904\u7406\u6587\u4ef6&#xff08;\u6162&#xff0c;IO\u5bc6\u96c6\u578b&#xff09;<\/span><br \/>\n<span class=\"token keyword\">public<\/span> <span class=\"token keyword\">void<\/span> <span class=\"token function\">backgroundFileProcessing<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n    sharedPool<span class=\"token punctuation\">.<\/span><span class=\"token function\">execute<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token operator\">-&gt;<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n        <span class=\"token comment\">\/\/ &#8230; \u75af\u72c2\u5199\u6587\u4ef6&#xff0c;\u8017\u65f6\u5f88\u957f &#8230;<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<p><span class=\"token comment\">\/\/ \u4efb\u52a12&#xff1a;\u7528\u6237\u67e5\u8be2&#xff08;\u5feb&#xff0c;CPU\u5bc6\u96c6\u578b&#xff09;<\/span><br \/>\n<span class=\"token keyword\">public<\/span> <span class=\"token keyword\">int<\/span> <span class=\"token function\">userQuery<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n    <span class=\"token class-name\">Future<\/span><span class=\"token generics\"><span class=\"token punctuation\">&lt;<\/span><span class=\"token class-name\">Integer<\/span><span class=\"token punctuation\">&gt;<\/span><\/span> future <span class=\"token operator\">&#061;<\/span> sharedPool<span class=\"token punctuation\">.<\/span><span class=\"token function\">submit<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token operator\">-&gt;<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n        <span class=\"token comment\">\/\/ &#8230; \u7eaf\u5185\u5b58\u8ba1\u7b97&#xff0c;\u672c\u8be5\u5f88\u5feb &#8230;<\/span><br \/>\n        <span class=\"token keyword\">return<\/span> <span class=\"token number\">1<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token keyword\">return<\/span> future<span class=\"token punctuation\">.<\/span><span class=\"token function\">get<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<p>\u707e\u96be\u540e\u679c&#xff1a; \u8fd9\u4e2a\u7ebf\u7a0b\u6c60\u53ea\u67092\u4e2a\u7ebf\u7a0b\u3002\u540e\u53f0\u7684\u6587\u4ef6\u6279\u5904\u7406\u4efb\u52a1\u662f\u6162\u901f\u7684IO\u64cd\u4f5c&#xff0c;\u5b83\u4f1a\u957f\u65f6\u95f4\u9738\u5360\u7740\u8fd92\u4e2a\u5b9d\u8d35\u7684\u7ebf\u7a0b\u3002\u5f53\u7528\u6237\u7684\u67e5\u8be2\u8bf7\u6c42\u8fc7\u6765\u65f6&#xff0c;\u7ebf\u7a0b\u6c60\u5df2\u6ee1&#xff0c;\u961f\u5217\u4e5f\u5f88\u5feb\u88ab\u6587\u4ef6\u4efb\u52a1\u585e\u6ee1\u3002<\/p>\n<p>\u66f4\u81f4\u547d\u7684\u662fCallerRunsPolicy\u8fd9\u4e2a\u62d2\u7edd\u7b56\u7565&#xff0c;\u5b83\u7684\u610f\u601d\u662f&#xff1a;\u7ebf\u7a0b\u6c60\u5904\u7406\u4e0d\u4e86&#xff0c;\u8c01\u63d0\u4ea4\u7684\u8c01\u81ea\u5df1\u53bb\u6267\u884c\u3002\u4e8e\u662f&#xff0c;\u672c\u8be5\u5f02\u6b65\u6267\u884c\u7684\u5185\u5b58\u8ba1\u7b97\u4efb\u52a1&#xff0c;\u88ab\u786c\u751f\u751f\u5730\u4ea4\u7ed9\u4e86\u5904\u7406Web\u8bf7\u6c42\u7684Tomcat\u7ebf\u7a0b\u6765\u540c\u6b65\u6267\u884c&#xff01;\u8fd9\u4e0d\u4ec5\u8ba9\u5f53\u524d\u67e5\u8be2\u53d8\u6162&#xff0c;\u8fd8\u963b\u585e\u4e86Tomcat\u7ebf\u7a0b&#xff0c;\u4f7f\u5176\u65e0\u6cd5\u5904\u7406\u5176\u4ed6\u7528\u6237\u7684\u8bf7\u6c42&#xff0c;\u6700\u7ec8\u5bfc\u81f4\u6574\u4e2a\u5e94\u7528\u7684\u541e\u5410\u91cf\u6025\u5267\u4e0b\u964d\u3002<\/p>\n<h4>\u4e0d\u540c\u6027\u8d28\u7684\u4efb\u52a1\u5bf9\u7ebf\u7a0b\u6c60\u7684\u9700\u6c42\u622a\u7136\u4e0d\u540c<\/h4>\n<ul>\n<li>IO\u5bc6\u96c6\u578b\u4efb\u52a1&#xff1a;\u5982\u6587\u4ef6\u8bfb\u5199\u3001\u7f51\u7edc\u8bf7\u6c42\u3002\u7ebf\u7a0b\u5927\u90e8\u5206\u65f6\u95f4\u5728\u7b49\u5f85IO&#xff0c;CPU\u662f\u7a7a\u95f2\u7684\u3002\u56e0\u6b64\u9700\u8981\u8f83\u591a\u7684\u7ebf\u7a0b\u6765\u63d0\u9ad8CPU\u5229\u7528\u7387\u3002<\/li>\n<li>CPU\u5bc6\u96c6\u578b\u4efb\u52a1&#xff1a;\u5982\u590d\u6742\u8ba1\u7b97\u3002\u7ebf\u7a0b\u4e00\u76f4\u5728\u4f7f\u7528CPU\u3002\u7ebf\u7a0b\u6570\u4e0d\u5b9c\u8d85\u8fc7CPU\u6838\u5fc3\u6570&#xff0c;\u5426\u5219\u8fc7\u591a\u7684\u7ebf\u7a0b\u5207\u6362\u53cd\u800c\u4f1a\u964d\u4f4e\u6027\u80fd\u3002<\/li>\n<\/ul>\n<p>\u628a\u5b83\u4eec\u6df7\u5728\u4e00\u8d77&#xff0c;\u5c31\u50cf\u8ba9\u77ed\u8dd1\u8fd0\u52a8\u5458\u548c\u957f\u8dd1\u8fd0\u52a8\u5458\u5728\u540c\u4e00\u6761\u8d5b\u9053\u4e0a\u6bd4\u8d5b&#xff0c;\u4e92\u76f8\u5e72\u6270&#xff0c;\u8c01\u4e5f\u8dd1\u4e0d\u597d\u3002<\/p>\n<p>\u6b63\u786e\u7684\u505a\u6cd5\u662f\u4e13\u6c60\u4e13\u7528&#xff0c;\u4e92\u4e0d\u5e72\u6270\u3002<\/p>\n<p><span class=\"token comment\">\/\/ \u4e3aIO\u5bc6\u96c6\u578b\u4efb\u52a1\u521b\u5efa\u4e00\u4e2a\u72ec\u7acb\u7684\u7ebf\u7a0b\u6c60<\/span><br \/>\n<span class=\"token keyword\">private<\/span> <span class=\"token keyword\">static<\/span> <span class=\"token class-name\">ThreadPoolExecutor<\/span> ioThreadPool <span class=\"token operator\">&#061;<\/span> <span class=\"token keyword\">new<\/span> <span class=\"token class-name\">ThreadPoolExecutor<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">.<\/span><span class=\"token punctuation\">.<\/span><span class=\"token punctuation\">.<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token comment\">\/\/ \u4e3aCPU\u5bc6\u96c6\u578b\u4efb\u52a1\u521b\u5efa\u53e6\u4e00\u4e2a\u72ec\u7acb\u7684\u7ebf\u7a0b\u6c60<\/span><br \/>\n<span class=\"token keyword\">private<\/span> <span class=\"token keyword\">static<\/span> <span class=\"token class-name\">ThreadPoolExecutor<\/span> cpuThreadPool <span class=\"token operator\">&#061;<\/span> <span class=\"token keyword\">new<\/span> <span class=\"token class-name\">ThreadPoolExecutor<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">.<\/span><span class=\"token punctuation\">.<\/span><span class=\"token punctuation\">.<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token comment\">\/\/ \u5404\u81ea\u4f7f\u7528\u81ea\u5df1\u7684\u6c60\u5b50<\/span><br \/>\nioThreadPool<span class=\"token punctuation\">.<\/span><span class=\"token function\">execute<\/span><span class=\"token punctuation\">(<\/span><span class=\"token comment\">\/* \u6587\u4ef6\u5904\u7406 *\/<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\ncpuThreadPool<span class=\"token punctuation\">.<\/span><span class=\"token function\">submit<\/span><span class=\"token punctuation\">(<\/span><span class=\"token comment\">\/* \u5185\u5b58\u8ba1\u7b97 *\/<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<h3>\u7ebf\u7a0b\u6c60\u4e0d\u662f\u4e07\u91d1\u6cb9&#xff0c;\u9694\u79bb\u548c\u590d\u7528\u624d\u662f\u738b\u9053<\/h3>\n<p>\u8ba9\u6211\u4eec\u603b\u7ed3\u4e00\u4e0b\u4ece\u8fd9\u4e09\u6b21\u8840\u6dcb\u6dcb\u7684\u4e8b\u6545\u4e2d\u5b66\u5230\u7684\u6559\u8bad&#xff1a;<\/p>\n<li>\u544a\u522bExecutors&#xff0c;\u62e5\u62b1ThreadPoolExecutor&#xff1a;\u4e0d\u8981\u8d2a\u56fe\u4e00\u65f6\u7684\u65b9\u4fbf&#xff0c;\u5fc5\u987b\u624b\u52a8\u521b\u5efa\u7ebf\u7a0b\u6c60&#xff0c;\u628a\u6838\u5fc3\u53c2\u6570\u7262\u7262\u638c\u63e1\u5728\u81ea\u5df1\u624b\u91cc\u3002\u5e76\u7ed9\u4f60\u7684\u7ebf\u7a0b\u8d77\u4e2a\u597d\u540d\u5b57&#xff01;<\/li>\n<li>\u675c\u7edd\u201c\u4e00\u6b21\u6027\u201d\u7ebf\u7a0b\u6c60&#xff0c;\u575a\u6301\u590d\u7528&#xff1a;\u7ebf\u7a0b\u6c60\u5fc5\u987b\u662f\u5355\u4f8b\u7684\u3001\u88ab\u590d\u7528\u7684\u3002\u628a\u5b83\u5b58\u5728static final\u5b57\u6bb5\u91cc&#xff0c;\u6216\u8005\u4ea4\u7531Spring\u5bb9\u5668\u7ba1\u7406\u3002<\/li>\n<li>\u9694\u79bb\u4e0d\u540c\u6027\u8d28\u7684\u4efb\u52a1&#xff0c;\u907f\u514d\u201c\u5185\u6218\u201d&#xff1a;\u4e3aIO\u5bc6\u96c6\u578b\u548cCPU\u5bc6\u96c6\u578b\u4efb\u52a1\u521b\u5efa\u5404\u81ea\u72ec\u7acb\u7684\u3001\u53c2\u6570\u8c03\u4f18\u7684\u7ebf\u7a0b\u6c60\u3002\u522b\u8ba9\u4e00\u4e2a\u6162\u4efb\u52a1\u62d6\u57ae\u6574\u4e2a\u7cfb\u7edf\u3002<\/li>\n<p>\u8bb0\u4f4f&#xff0c;\u7ebf\u7a0b\u6c60\u5c31\u50cf\u4f60\u624b\u4e0b\u7684\u5175&#xff0c;\u80e1\u4e71\u6307\u6325&#xff0c;\u4ed6\u4eec\u5c31\u4f1a\u5728\u6218\u573a\u4e0a\u7ed9\u4f60\u5236\u9020\u5929\u5927\u7684\u9ebb\u70e6\u3002\u7cbe\u5fc3\u7f16\u6392&#xff0c;\u4ed6\u4eec\u624d\u662f\u4f60\u653b\u57ce\u62d4\u5be8\u7684\u5229\u5668\u3002\u5982\u679c\u89c9\u5f97\u6709\u7528&#xff0c;\u9ebb\u70e6\u70b9\u4e2a\u8d5e\u5427&#xff5e;&#xff5e;&#xff5e;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb598\u6b21\uff0c\u70b9\u8d5e25\u6b21\uff0c\u6536\u85cf7\u6b21\u3002\u4e00\u4e2a`Executors.newCachedThreadPool`\uff0c\u5728\u53cc\u5341\u4e00\u9ad8\u5cf0\u671f\u76f4\u63a5\u5e72\u5d29\u4e86\u6211\u4eec\u6574\u4e2a\u670d\u52a1\uff0cP0\u7ea7\u4e8b\u6545\uff01\u590d\u76d8\u540e\uff0c\u53c8\u56e0\u7ebf\u7a0b\u6c60\u201c\u5047\u590d\u7528\u201d\u548c\u201cIO\/CPU\u4efb\u52a1\u6df7\u7528\u201d\u5bfc\u81f4\u7cfb\u7edf\u6027\u80fd\u96ea\u5d29\u3002\u8fd9\u7bc7\u8840\u6cea\u53f2\u5c06\u5e26\u4f60\u4eb2\u5386\u4e09\u6b21\u7531\u7ebf\u7a0b\u6c60\u5f15\u53d1\u7684\u751f\u4ea7\u4e8b\u6545\uff0c\u4ece\u6e90\u7801\u5c42\u9762\u5256\u6790`Executors`\u7684\u81f4\u547d\u7f3a\u9677\uff0c\u5e76\u63ed\u793a\u7ebf\u7a0b\u6c60\u590d\u7528\u4e0e\u9694\u79bb\u7684\u9ec4\u91d1\u6cd5\u5219\u3002\u522b\u518d\u88f8\u5199\u7ebf\u7a0b\u6c60\u4e86\uff0c\u4e5f\u522b\u8ba9\u5b83\u6210\u4e3a\u4f60\u4e0b\u4e00\u4e2aP0\u7684\u5143\u51f6\uff01<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[99,4590,586,4591],"topic":[],"class_list":["post-47757","post","type-post","status-publish","format-standard","hentry","category-server","tag-java","tag-4590","tag-586","tag-4591"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>\u4e3a\u4ec0\u4e48\u300a\u963f\u91ccJava\u5f00\u53d1\u624b\u518c\u300b\u5f3a\u5236\u7981\u7528Executors\uff1f\u770b\u5b8c\u8fd9\u7bc7\u8840\u6cea\u53f2\u4f60\u5c31\u61c2\u4e86\u3002 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/47757.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"\u4e3a\u4ec0\u4e48\u300a\u963f\u91ccJava\u5f00\u53d1\u624b\u518c\u300b\u5f3a\u5236\u7981\u7528Executors\uff1f\u770b\u5b8c\u8fd9\u7bc7\u8840\u6cea\u53f2\u4f60\u5c31\u61c2\u4e86\u3002 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb598\u6b21\uff0c\u70b9\u8d5e25\u6b21\uff0c\u6536\u85cf7\u6b21\u3002\u4e00\u4e2a`Executors.newCachedThreadPool`\uff0c\u5728\u53cc\u5341\u4e00\u9ad8\u5cf0\u671f\u76f4\u63a5\u5e72\u5d29\u4e86\u6211\u4eec\u6574\u4e2a\u670d\u52a1\uff0cP0\u7ea7\u4e8b\u6545\uff01\u590d\u76d8\u540e\uff0c\u53c8\u56e0\u7ebf\u7a0b\u6c60\u201c\u5047\u590d\u7528\u201d\u548c\u201cIO\/CPU\u4efb\u52a1\u6df7\u7528\u201d\u5bfc\u81f4\u7cfb\u7edf\u6027\u80fd\u96ea\u5d29\u3002\u8fd9\u7bc7\u8840\u6cea\u53f2\u5c06\u5e26\u4f60\u4eb2\u5386\u4e09\u6b21\u7531\u7ebf\u7a0b\u6c60\u5f15\u53d1\u7684\u751f\u4ea7\u4e8b\u6545\uff0c\u4ece\u6e90\u7801\u5c42\u9762\u5256\u6790`Executors`\u7684\u81f4\u547d\u7f3a\u9677\uff0c\u5e76\u63ed\u793a\u7ebf\u7a0b\u6c60\u590d\u7528\u4e0e\u9694\u79bb\u7684\u9ec4\u91d1\u6cd5\u5219\u3002\u522b\u518d\u88f8\u5199\u7ebf\u7a0b\u6c60\u4e86\uff0c\u4e5f\u522b\u8ba9\u5b83\u6210\u4e3a\u4f60\u4e0b\u4e00\u4e2aP0\u7684\u5143\u51f6\uff01\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/47757.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2025-07-30T01:12:22+00:00\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/47757.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/47757.html\",\"name\":\"\u4e3a\u4ec0\u4e48\u300a\u963f\u91ccJava\u5f00\u53d1\u624b\u518c\u300b\u5f3a\u5236\u7981\u7528Executors\uff1f\u770b\u5b8c\u8fd9\u7bc7\u8840\u6cea\u53f2\u4f60\u5c31\u61c2\u4e86\u3002 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2025-07-30T01:12:22+00:00\",\"dateModified\":\"2025-07-30T01:12:22+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/47757.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/47757.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/47757.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"\u4e3a\u4ec0\u4e48\u300a\u963f\u91ccJava\u5f00\u53d1\u624b\u518c\u300b\u5f3a\u5236\u7981\u7528Executors\uff1f\u770b\u5b8c\u8fd9\u7bc7\u8840\u6cea\u53f2\u4f60\u5c31\u61c2\u4e86\u3002\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"\u4e3a\u4ec0\u4e48\u300a\u963f\u91ccJava\u5f00\u53d1\u624b\u518c\u300b\u5f3a\u5236\u7981\u7528Executors\uff1f\u770b\u5b8c\u8fd9\u7bc7\u8840\u6cea\u53f2\u4f60\u5c31\u61c2\u4e86\u3002 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/47757.html","og_locale":"zh_CN","og_type":"article","og_title":"\u4e3a\u4ec0\u4e48\u300a\u963f\u91ccJava\u5f00\u53d1\u624b\u518c\u300b\u5f3a\u5236\u7981\u7528Executors\uff1f\u770b\u5b8c\u8fd9\u7bc7\u8840\u6cea\u53f2\u4f60\u5c31\u61c2\u4e86\u3002 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb598\u6b21\uff0c\u70b9\u8d5e25\u6b21\uff0c\u6536\u85cf7\u6b21\u3002\u4e00\u4e2a`Executors.newCachedThreadPool`\uff0c\u5728\u53cc\u5341\u4e00\u9ad8\u5cf0\u671f\u76f4\u63a5\u5e72\u5d29\u4e86\u6211\u4eec\u6574\u4e2a\u670d\u52a1\uff0cP0\u7ea7\u4e8b\u6545\uff01\u590d\u76d8\u540e\uff0c\u53c8\u56e0\u7ebf\u7a0b\u6c60\u201c\u5047\u590d\u7528\u201d\u548c\u201cIO\/CPU\u4efb\u52a1\u6df7\u7528\u201d\u5bfc\u81f4\u7cfb\u7edf\u6027\u80fd\u96ea\u5d29\u3002\u8fd9\u7bc7\u8840\u6cea\u53f2\u5c06\u5e26\u4f60\u4eb2\u5386\u4e09\u6b21\u7531\u7ebf\u7a0b\u6c60\u5f15\u53d1\u7684\u751f\u4ea7\u4e8b\u6545\uff0c\u4ece\u6e90\u7801\u5c42\u9762\u5256\u6790`Executors`\u7684\u81f4\u547d\u7f3a\u9677\uff0c\u5e76\u63ed\u793a\u7ebf\u7a0b\u6c60\u590d\u7528\u4e0e\u9694\u79bb\u7684\u9ec4\u91d1\u6cd5\u5219\u3002\u522b\u518d\u88f8\u5199\u7ebf\u7a0b\u6c60\u4e86\uff0c\u4e5f\u522b\u8ba9\u5b83\u6210\u4e3a\u4f60\u4e0b\u4e00\u4e2aP0\u7684\u5143\u51f6\uff01","og_url":"https:\/\/www.wsisp.com\/helps\/47757.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2025-07-30T01:12:22+00:00","author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"3 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/47757.html","url":"https:\/\/www.wsisp.com\/helps\/47757.html","name":"\u4e3a\u4ec0\u4e48\u300a\u963f\u91ccJava\u5f00\u53d1\u624b\u518c\u300b\u5f3a\u5236\u7981\u7528Executors\uff1f\u770b\u5b8c\u8fd9\u7bc7\u8840\u6cea\u53f2\u4f60\u5c31\u61c2\u4e86\u3002 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2025-07-30T01:12:22+00:00","dateModified":"2025-07-30T01:12:22+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/47757.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/47757.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/47757.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"\u4e3a\u4ec0\u4e48\u300a\u963f\u91ccJava\u5f00\u53d1\u624b\u518c\u300b\u5f3a\u5236\u7981\u7528Executors\uff1f\u770b\u5b8c\u8fd9\u7bc7\u8840\u6cea\u53f2\u4f60\u5c31\u61c2\u4e86\u3002"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/47757","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=47757"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/47757\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=47757"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=47757"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=47757"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=47757"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}