{"id":3141,"date":"2018-10-05T17:51:14","date_gmt":"2018-10-05T09:51:14","guid":{"rendered":"http:\/\/www.xttblog.com\/?p=3141"},"modified":"2018-10-05T17:51:14","modified_gmt":"2018-10-05T09:51:14","slug":"java-%e7%ba%bf%e7%a8%8b%e6%b1%a0-executors-%e6%95%99%e7%a8%8b","status":"publish","type":"post","link":"https:\/\/www.xttblog.com\/?p=3141","title":{"rendered":"Java \u7ebf\u7a0b\u6c60 Executors \u6559\u7a0b"},"content":{"rendered":"<p>\n\t\u8fd9\u7bc7\u6587\u7ae0\u6211\u672c\u6765\u5199\u597d\u4e86\uff0c\u4f46\u662f\u6ca1\u6709\u4fdd\u5b58\uff0c\u6211\u51fa\u53bb\u4e8610\u5206\u949f\uff0c\u7136\u540e\u56de\u6765\u4e86\uff0c\u53d1\u73b0\u6211\u7684\u7535\u8111\u88ab\u5173\u4e86\u3002\u7136\u540e\u6211\u5988\u8bf4\uff1a&ldquo;\u4f60\u4eba\u8d70\u4e86\uff0c\u7535\u8111\u4e5f\u4e0d\u5173\uff0c\u6211\u4e0d\u77e5\u600e\u4e48\u5173\uff0c\u5c31\u628a\u7535\u6e90\u62fd\u4e86&rdquo;\u3002\u54ce\uff0c\u8ba9\u6211\u8bf4\u5565\u597d\u5462\uff1f\u8ba9\u6211\u54ed\u4f1a\u5427\uff0c\u51e0\u4e2a\u5c0f\u65f6\u7684\u6770\u4f5c\uff0c\u5c31\u8fd9\u4e48\u6ca1\u4e86\u3002\n<\/p>\n<p>\n\t\u597d\u5427\uff0c\u6211\u518d\u91cd\u65b0\u5199\u4e00\u7bc7\u5427\uff01\n<\/p>\n<p>\n\t\u5728\u73b0\u4ee3\u7684\u4e92\u8054\u7f51\u5e94\u7528\u53d1\u5c55\u8fc7\u7a0b\u4e2d\uff0c\u4e09\u9ad8\u7279\u6027\uff0c\u7ecf\u5e38\u6027\u7684\u4f1a\u5728\u9762\u8bd5\u4e2d\u88ab\u95ee\u8d77\u3002\u8fd9\u662f\u4e00\u79cd\u8d8b\u52bf\uff0c\u4e0d\u7ba1\u4f60\u8981\u8fdb\u5165\u7684\u516c\u53f8\u662f\u4e0d\u662f\u5177\u6709\u8fd9\u4e9b\u7279\u6027\u3002\u4eca\u5929\u6211\u4eec\u8981\u8bf4\u7684\u662f\u6027\u80fd\u65b9\u4fbf\u591a\u7ebf\u7a0b\u7684\u4f7f\u7528\uff01\u8bf4\u5230\u591a\u7ebf\u7a0b\uff0c\u5927\u5bb6\u4e00\u5b9a\u4f1a\u60f3\u5230\u7ebf\u7a0b\u6c60\uff0c\u4e0b\u9762\u4e00\u8d77\u6765\u603b\u7ed3\u4e00\u4e0b\u591a\u7ebf\u7a0b\u7684\u597d\u5904\uff01\n<\/p>\n<h2>\n\t\u7ebf\u7a0b\u6c60\u7684\u597d\u5904<br \/>\n<\/h2>\n<ul>\n<li>\n\t\t\u91cd\u7528\u5b58\u5728\u7684\u7ebf\u7a0b\uff0c\u51cf\u5c11\u5bf9\u8c61\u521b\u5efa\u3001\u6d88\u4ea1\u7684\u5f00\u9500\uff0c\u6027\u80fd\u4e0a\u6bd4\u8f83\u597d\n\t<\/li>\n<li>\n\t\t\u53ef\u6709\u6548\u63a7\u5236\u6700\u5927\u5e76\u53d1\u7ebf\u7a0b\u6570\uff0c\u63d0\u9ad8\u7cfb\u7edf\u8d44\u6e90\u5229\u7528\u7387\uff0c\u540c\u65f6\u53ef\u4ee5\u907f\u514d\u8fc7\u591a\u8d44\u6e90\u7ade\u4e89\uff0c\u907f\u514d\u963b\u585e\n\t<\/li>\n<li>\n\t\t\u63d0\u4f9b\u5b9a\u65f6\u6267\u884c\u3001\u5b9a\u671f\u6267\u884c\u3001\u5355\u7ebf\u7a0b\u3001\u5e76\u53d1\u6570\u63a7\u5236\u7b49\u529f\u80fd\n\t<\/li>\n<\/ul>\n<p>\n\t\u5bf9\u4e8e\u7ebf\u7a0b\u6c60\uff0cjava \u4e2d\u63d0\u4f9b\u4e86\u4e00\u4e2a&nbsp;Executors \u7ebf\u7a0b\u6c60\uff0c\u6211\u4eec\u4eca\u5929\u91cd\u70b9\u8bb2\u5b83\uff01\n<\/p>\n<p>\n\t<img decoding=\"async\" alt=\"Executors \u7c7b\u5173\u7cfb\u56fe\" src=\"https:\/\/raw.githubusercontent.com\/gcy111\/xttblog\/master\/b1.png\" style=\"width: 453px;height: 280px\" \/>\n<\/p>\n<p>\n\t\u5728\u4e0a\u56fe\u4e2d\uff0c\u6211\u4eec\u6700\u5e38\u4f7f\u7528\u7684\u662f Executors\uff0c\u5b83\u4e3b\u8981\u7528\u6765\u521b\u5efa\u7ebf\u7a0b\u6c60\u4f7f\u7528\u7ebf\u7a0b\u3002\u5b83\u5305\u542b\u4e86\u4e00\u4e2a Executor \u6846\u67b6\uff0c\u5b83\u662f\u4e00\u4e2a\u6839\u636e\u4e00\u7ec4\u6267\u884c\u7b56\u7565\u7684\u8c03\u7528\u8c03\u5ea6\u6267\u884c\u548c\u63a7\u5236\u5f02\u6b65\u4efb\u52a1\u7684\u6846\u67b6\uff0c\u76ee\u7684\u662f\u63d0\u4f9b\u4e00\u79cd\u5c06\u4efb\u52a1\u63d0\u4ea4\u4e0e\u4efb\u52a1\u5982\u4f55\u8fd0\u884c\u5206\u79bb\u5f00\u7684\u673a\u5236\u3002\u5b83\u5305\u542b\u4e86\u4e09\u4e2a Executor \u63a5\u53e3\uff1a\n<\/p>\n<ul>\n<li>\n\t\tExecutor:\u8fd0\u884c\u65b0\u4efb\u52a1\u7684\u7b80\u5355\u63a5\u53e3\n\t<\/li>\n<li>\n\t\tExecutorService\uff1a\u6269\u5c55\u4e86Executor\uff0c\u6dfb\u52a0\u4e86\u7528\u6765\u7ba1\u7406\u6267\u884c\u5668\u751f\u547d\u5468\u671f\u548c\u4efb\u52a1\u751f\u547d\u5468\u671f\u7684\u65b9\u6cd5\n\t<\/li>\n<li>\n\t\tScheduleExcutorService\uff1a\u6269\u5c55\u4e86ExecutorService\uff0c\u652f\u6301Future\u548c\u5b9a\u671f\u6267\u884c\u4efb\u52a1\n\t<\/li>\n<\/ul>\n<p>\n\t\u5728\u300a\u963f\u91cc\u5df4\u5df4\u5f00\u53d1\u624b\u518c\u300b\u4e2d\u6709\u4e00\u9879\u89c4\u5219\u5199\u5230\uff1a&ldquo;\u3010\u5f3a\u5236\u3011\u7ebf\u7a0b\u6c60\u4e0d\u5141\u8bb8\u4f7f\u7528 Executors\u53bb\u521b\u5efa\uff0c\u800c\u662f\u901a\u8fc7 ThreadPoolExecutor\u7684\u65b9\u5f0f\uff0c\u8fd9\u6837\u7684\u5904\u7406\u65b9\u5f0f\u8ba9\u5199\u7684\u540c\u5b66\u66f4\u52a0\u660e\u786e\u7ebf\u7a0b\u6c60\u7684\u8fd0\u884c\u89c4\u5219\uff0c\u89c4\u907f\u8d44\u6e90\u8017\u5c3d\u7684\u98ce\u9669\u3002&#8230;&#8230;&rdquo;\n<\/p>\n<p>\n\t\u6211\u4eec\u53ef\u4ee5\u770b\u5230\uff0c\u624b\u518c\u4e0a\u8fd8\u505a\u4e86\u4e00\u4e2a\u8bf4\u660e\uff01\u5177\u4f53\u5982\u4e0b\uff1a\n<\/p>\n<p>\n\tExecutors \u8fd4\u56de\u7684\u7ebf\u7a0b\u6c60\u5bf9\u8c61\u7684\u5f0a\u7aef\u5982\u4e0b\uff1a\n<\/p>\n<ol>\n<li>\n\t\tFixedThreadPool\u548c SingleThreadPool\uff1a\u5141\u8bb8\u7684\u8bf7\u6c42\u961f\u5217\u957f\u5ea6\u4e3a Integer.MAX_VALUE\uff0c\u53ef\u80fd\u4f1a\u5806\u79ef\u5927\u91cf\u7684\u8bf7\u6c42\uff0c\u4ece\u800c\u5bfc\u81f4 OOM\u3002\n\t<\/li>\n<li>\n\t\tCachedThreadPool\u548c ScheduledThreadPool\uff1a\u5141\u8bb8\u7684\u521b\u5efa\u7ebf\u7a0b\u6570\u91cf\u4e3a Integer.MAX_VALUE\uff0c\u53ef\u80fd\u4f1a\u521b\u5efa\u5927\u91cf\u7684\u7ebf\u7a0b\uff0c\u4ece\u800c\u5bfc\u81f4 OOM\u3002\n\t<\/li>\n<\/ol>\n<p>\n\t\u4ece\u4e0a\u9762\u8fd9\u4e2a\u8bf4\u660e\u53ef\u4ee5\u770b\u51fa\uff0c\u963f\u91cc\u5df4\u5df4\u5185\u90e8\u80af\u5b9a\u6709\u4eba\u4f7f\u7528&nbsp;Executors \u53bb\u521b\u5efa\u7ebf\u7a0b\u6c60\uff0c\u7136\u800c\u7531\u4e8e\u4f7f\u7528\u4e0d\u5f53\uff0c\u4ea7\u751f\u4e86 OOM \u95ee\u9898\u3002\u6211\u731c\u8fd9\u6837\u7528\u7684\u80af\u5b9a\u4e0d\u6b62\u4e00\u4e2a\u4eba\uff0c\u800c\u4e14\u662f\u5f15\u8d77\u7684\u95ee\u9898\u6bd4\u8f83\u5927\uff0c\u5185\u90e8\u80af\u5b9a\u505a\u4e86\u7279\u4f8b\u8fdb\u884c\u5b66\u4e60\uff0c\u4e8e\u662f\u5c31\u88ab\u5199\u8fdb\u4e86\u624b\u518c\u3002\n<\/p>\n<p>\n\t\u963f\u91cc\u5df4\u5df4\u7684\u5f00\u53d1\u4eba\u5458\u80fd\u72af\u7684\u9519\uff0c\u6211\u4eec\u53ef\u80fd\u4e5f\u4f1a\u72af\uff0c\u6240\u4ee5\u5462\uff1f\u6211\u4e5f\u4e0d\u592a\u63a8\u8350\u5927\u5bb6\u76f4\u63a5\u7528&nbsp;Executors \u53bb\u76f4\u63a5\u521b\u5efa\u7ebf\u7a0b\u6c60\uff01\n<\/p>\n<h2>\n\tThreadPoolExecutor<br \/>\n<\/h2>\n<p>\n\t\u65e2\u7136\u4e0a\u9762\u63d0\u5230\u4e86\u4f7f\u7528&nbsp;ThreadPoolExecutor \u6765\u521b\u5efa\u7ebf\u7a0b\u6c60\uff0c\u90a3\u6211\u4eec\u5c31\u4e00\u8d77\u6765\u770b\u770b\u5b83\u3002\n<\/p>\n<p>\n\tThreadPoolExecutor\u4e00\u5171\u6709\u4e03\u4e2a\u53c2\u6570\uff0c\u8fd9\u4e03\u4e2a\u53c2\u6570\u914d\u5408\u8d77\u6765\uff0c\u6784\u6210\u4e86\u7ebf\u7a0b\u6c60\u5f3a\u5927\u7684\u529f\u80fd\u3002\n<\/p>\n<ul>\n<li>\n\t\tcorePoolSize\uff1a\u6838\u5fc3\u7ebf\u7a0b\u6570\u91cf\n\t<\/li>\n<li>\n\t\tmaximumPoolSize\uff1a\u7ebf\u7a0b\u6700\u5927\u7ebf\u7a0b\u6570\n\t<\/li>\n<li>\n\t\tworkQueue\uff1a\u963b\u585e\u961f\u5217\uff0c\u5b58\u50a8\u7b49\u5f85\u6267\u884c\u7684\u4efb\u52a1\uff0c\u5f88\u91cd\u8981\uff0c\u4f1a\u5bf9\u7ebf\u7a0b\u6c60\u8fd0\u884c\u8fc7\u7a0b\u4ea7\u751f\u91cd\u5927\u5f71\u54cd\n\t<\/li>\n<\/ul>\n<blockquote><p>\n\t\u5f53\u6211\u4eec\u63d0\u4ea4\u4e00\u4e2a\u65b0\u7684\u4efb\u52a1\u5230\u7ebf\u7a0b\u6c60\uff0c\u7ebf\u7a0b\u6c60\u4f1a\u6839\u636e\u5f53\u524d\u6c60\u4e2d\u6b63\u5728\u8fd0\u884c\u7684\u7ebf\u7a0b\u6570\u91cf\u6765\u51b3\u5b9a\u8be5\u4efb\u52a1\u7684\u5904\u7406\u65b9\u5f0f\u3002\u5904\u7406\u65b9\u5f0f\u6709\u4e09\u79cd\uff1a&nbsp;<br \/>\n\t1\u3001\u76f4\u63a5\u5207\u6362\uff08SynchronusQueue\uff09&nbsp;<br \/>\n\t2\u3001\u65e0\u754c\u961f\u5217\uff08LinkedBlockingQueue\uff09\u80fd\u591f\u521b\u5efa\u7684\u6700\u5927\u7ebf\u7a0b\u6570\u4e3acorePoolSize,\u8fd9\u65f6maximumPoolSize\u5c31\u4e0d\u4f1a\u8d77\u4f5c\u7528\u4e86\u3002\u5f53\u7ebf\u7a0b\u6c60\u4e2d\u6240\u6709\u7684\u6838\u5fc3\u7ebf\u7a0b\u90fd\u662f\u8fd0\u884c\u72b6\u6001\u7684\u65f6\u5019\uff0c\u65b0\u7684\u4efb\u52a1\u63d0\u4ea4\u5c31\u4f1a\u653e\u5165\u7b49\u5f85\u961f\u5217\u4e2d\u3002&nbsp;<br \/>\n\t3\u3001\u6709\u754c\u961f\u5217\uff08ArrayBlockingQueue\uff09\u6700\u5927maximumPoolSize\uff0c\u80fd\u591f\u964d\u4f4e\u8d44\u6e90\u6d88\u8017\uff0c\u4f46\u662f\u8fd9\u79cd\u65b9\u5f0f\u4f7f\u5f97\u7ebf\u7a0b\u6c60\u5bf9\u7ebf\u7a0b\u8c03\u5ea6\u53d8\u7684\u66f4\u56f0\u96be\u3002\u56e0\u4e3a\u7ebf\u7a0b\u6c60\u4e0e\u961f\u5217\u5bb9\u91cf\u90fd\u662f\u6709\u9650\u7684\u3002\u6240\u4ee5\u60f3\u8ba9\u7ebf\u7a0b\u6c60\u7684\u541e\u5410\u7387\u548c\u5904\u7406\u4efb\u52a1\u8fbe\u5230\u4e00\u4e2a\u5408\u7406\u7684\u8303\u56f4\uff0c\u53c8\u60f3\u4f7f\u6211\u4eec\u7684\u7ebf\u7a0b\u8c03\u5ea6\u76f8\u5bf9\u7b80\u5355\uff0c\u5e76\u4e14\u8fd8\u5c3d\u53ef\u80fd\u964d\u4f4e\u8d44\u6e90\u7684\u6d88\u8017\uff0c\u6211\u4eec\u5c31\u9700\u8981\u5408\u7406\u7684\u9650\u5236\u8fd9\u4e24\u4e2a\u6570\u91cf&nbsp;<br \/>\n\t\u5206\u914d\u6280\u5de7\uff1a [\u5982\u679c\u60f3\u964d\u4f4e\u8d44\u6e90\u7684\u6d88\u8017\u5305\u62ec\u964d\u4f4ecpu\u4f7f\u7528\u7387\u3001\u64cd\u4f5c\u7cfb\u7edf\u8d44\u6e90\u7684\u6d88\u8017\u3001\u4e0a\u4e0b\u6587\u5207\u6362\u7684\u5f00\u9500\u7b49\u7b49\uff0c\u53ef\u4ee5\u8bbe\u7f6e\u4e00\u4e2a\u8f83\u5927\u7684\u961f\u5217\u5bb9\u91cf\u548c\u8f83\u5c0f\u7684\u7ebf\u7a0b\u6c60\u5bb9\u91cf\uff0c\u8fd9\u6837\u4f1a\u964d\u4f4e\u7ebf\u7a0b\u6c60\u7684\u541e\u5410\u91cf\u3002\u5982\u679c\u6211\u4eec\u63d0\u4ea4\u7684\u4efb\u52a1\u7ecf\u5e38\u53d1\u751f\u963b\u585e\uff0c\u6211\u4eec\u53ef\u4ee5\u8c03\u6574maximumPoolSize\u3002\u5982\u679c\u6211\u4eec\u7684\u961f\u5217\u5bb9\u91cf\u8f83\u5c0f\uff0c\u6211\u4eec\u9700\u8981\u628a\u7ebf\u7a0b\u6c60\u5927\u5c0f\u8bbe\u7f6e\u7684\u5927\u4e00\u4e9b\uff0c\u8fd9\u6837cpu\u7684\u4f7f\u7528\u7387\u76f8\u5bf9\u6765\u8bf4\u4f1a\u9ad8\u4e00\u4e9b\u3002\u4f46\u662f\u5982\u679c\u7ebf\u7a0b\u6c60\u7684\u5bb9\u91cf\u8bbe\u7f6e\u7684\u8fc7\u5927\uff0c\u63d0\u9ad8\u4efb\u52a1\u7684\u6570\u91cf\u8fc7\u591a\u7684\u65f6\u5019\uff0c\u5e76\u53d1\u91cf\u4f1a\u589e\u52a0\uff0c\u90a3\u4e48\u7ebf\u7a0b\u4e4b\u95f4\u7684\u8c03\u5ea6\u5c31\u662f\u4e00\u4e2a\u9700\u8981\u8003\u8651\u7684\u95ee\u9898\u3002\u8fd9\u6837\u53cd\u800c\u53ef\u80fd\u4f1a\u964d\u4f4e\u5904\u7406\u4efb\u52a1\u7684\u541e\u5410\u91cf\u3002\n<\/p><\/blockquote>\n<ul>\n<li>\n\t\tkeepAliveTime\uff1a\u7ebf\u7a0b\u6ca1\u6709\u4efb\u52a1\u6267\u884c\u65f6\u6700\u591a\u4fdd\u6301\u591a\u4e45\u65f6\u95f4\u7ec8\u6b62\uff08\u5f53\u7ebf\u7a0b\u4e2d\u7684\u7ebf\u7a0b\u6570\u91cf\u5927\u4e8ecorePoolSize\u7684\u65f6\u5019\uff0c\u5982\u679c\u8fd9\u65f6\u6ca1\u6709\u65b0\u7684\u4efb\u52a1\u63d0\u4ea4\u6838\u5fc3\u7ebf\u7a0b\u5916\u7684\u7ebf\u7a0b\u4e0d\u4f1a\u7acb\u5373\u9500\u6bc1\uff0c\u800c\u662f\u7b49\u5f85\uff0c\u76f4\u5230\u8d85\u8fc7keepAliveTime\uff09\n\t<\/li>\n<li>\n\t\tunit\uff1akeepAliveTime\u7684\u65f6\u95f4\u5355\u4f4d\n\t<\/li>\n<li>\n\t\tthreadFactory\uff1a\u7ebf\u7a0b\u5de5\u5382\uff0c\u7528\u6765\u521b\u5efa\u7ebf\u7a0b\uff0c\u6709\u4e00\u4e2a\u9ed8\u8ba4\u7684\u5de5\u573a\u6765\u521b\u5efa\u7ebf\u7a0b\uff0c\u8fd9\u6837\u65b0\u521b\u5efa\u51fa\u6765\u7684\u7ebf\u7a0b\u6709\u76f8\u540c\u7684\u4f18\u5148\u7ea7\uff0c\u662f\u975e\u5b88\u62a4\u7ebf\u7a0b\u3001\u8bbe\u7f6e\u597d\u4e86\u540d\u79f0\uff09<img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"135\" src=\"https:\/\/img-blog.csdn.net\/20180505142542206?watermark\/2\/text\/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2plc29uam9rZQ==\/font\/5a6L5L2T\/fontsize\/400\/fill\/I0JBQkFCMA==\/dissolve\/70\" width=\"781\" \/>\n\t<\/li>\n<\/ul>\n<p>\n\t<img decoding=\"async\" alt=\"threadFactory\" src=\"https:\/\/raw.githubusercontent.com\/gcy111\/xttblog\/master\/b2.png\" style=\"width: 781px;height: 135px\" \/>\n<\/p>\n<blockquote><p>\n\tcorePoolSize\u3001maximumPoolSize\u3001workQueue \u4e09\u8005\u5173\u7cfb\uff1a\u5982\u679c\u8fd0\u884c\u7684\u7ebf\u7a0b\u6570\u5c0f\u4e8ecorePoolSize\u7684\u65f6\u5019\uff0c\u76f4\u63a5\u521b\u5efa\u65b0\u7ebf\u7a0b\u6765\u5904\u7406\u4efb\u52a1\u3002\u5373\u4f7f\u7ebf\u7a0b\u6c60\u4e2d\u7684\u5176\u4ed6\u7ebf\u7a0b\u662f\u7a7a\u95f2\u7684\u3002\u5982\u679c\u8fd0\u884c\u4e2d\u7684\u7ebf\u7a0b\u6570\u5927\u4e8ecorePoolSize\u4e14\u5c0f\u4e8emaximumPoolSize\u65f6\uff0c\u90a3\u4e48\u53ea\u6709\u5f53workQueue\u6ee1\u7684\u65f6\u5019\u624d\u521b\u5efa\u65b0\u7684\u7ebf\u7a0b\u53bb\u5904\u7406\u4efb\u52a1\u3002\u5982\u679ccorePoolSize\u4e0emaximumPoolSize\u662f\u76f8\u540c\u7684\uff0c\u90a3\u4e48\u521b\u5efa\u7684\u7ebf\u7a0b\u6c60\u5927\u5c0f\u662f\u56fa\u5b9a\u7684\u3002\u8fd9\u65f6\u6709\u65b0\u4efb\u52a1\u63d0\u4ea4\uff0c\u5f53workQueue\u672a\u6ee1\u65f6\uff0c\u5c31\u628a\u8bf7\u6c42\u653e\u5165workQueue\u4e2d\u3002\u7b49\u5f85\u7a7a\u7ebf\u7a0b\u4eceworkQueue\u53d6\u51fa\u4efb\u52a1\u3002\u5982\u679cworkQueue\u6b64\u65f6\u4e5f\u6ee1\u4e86\uff0c\u90a3\u4e48\u5c31\u4f7f\u7528\u53e6\u5916\u7684\u62d2\u7edd\u7b56\u7565\u53c2\u6570\u53bb\u6267\u884c\u62d2\u7edd\u7b56\u7565\u3002\n<\/p><\/blockquote>\n<p>\n\t\u521d\u59cb\u5316\u65b9\u6cd5\uff1a\u7531\u4e03\u4e2a\u53c2\u6570\u7ec4\u5408\u6210\u56db\u4e2a\u521d\u59cb\u5316\u65b9\u6cd5&nbsp;\n<\/p>\n<p>\n\t<img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"104\" src=\"https:\/\/img-blog.csdn.net\/20180505141614527?watermark\/2\/text\/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2plc29uam9rZQ==\/font\/5a6L5L2T\/fontsize\/400\/fill\/I0JBQkFCMA==\/dissolve\/70\" width=\"809\" \/><img decoding=\"async\" alt=\"ThreadPoolExecutor\" src=\"https:\/\/raw.githubusercontent.com\/gcy111\/xttblog\/master\/b3.png\" style=\"width: 809px;height: 104px\" \/>\n<\/p>\n<p>\n\t\u5176\u4ed6\u65b9\u6cd5\uff1a\n<\/p>\n<ol>\n<li>\n\t\t<img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"285\" src=\"https:\/\/img-blog.csdn.net\/201805051421294?watermark\/2\/text\/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2plc29uam9rZQ==\/font\/5a6L5L2T\/fontsize\/400\/fill\/I0JBQkFCMA==\/dissolve\/70\" width=\"684\" \/>execute() \u63d0\u4ea4\u4efb\u52a1\uff0c\u4ea4\u7ed9\u7ebf\u7a0b\u6c60\u6267\u884c\n\t<\/li>\n<li>\n\t\tsubmit() \u63d0\u4ea4\u4efb\u52a1\uff0c\u80fd\u591f\u8fd4\u56de\u6267\u884c\u7ed3\u679c execute+Future\n\t<\/li>\n<li>\n\t\tshutdown() \u5173\u95ed\u7ebf\u7a0b\u6c60\uff0c\u7b49\u5f85\u4efb\u52a1\u90fd\u6267\u884c\u5b8c\n\t<\/li>\n<li>\n\t\tshutdownNow() \u5173\u95ed\u7ebf\u7a0b\u6c60\uff0c\u4e0d\u7b49\u5f85\u4efb\u52a1\u6267\u884c\u5b8c\n\t<\/li>\n<li>\n\t\tgetTaskCount() \u7ebf\u7a0b\u6c60\u5df2\u6267\u884c\u548c\u672a\u6267\u884c\u7684\u4efb\u52a1\u603b\u6570\n\t<\/li>\n<li>\n\t\tgetCompleteTaskCount() \u5df2\u5b8c\u6210\u7684\u4efb\u52a1\u6570\u91cf\n\t<\/li>\n<li>\n\t\tgetPoolSize() \u7ebf\u7a0b\u6c60\u5f53\u524d\u7684\u7ebf\u7a0b\u6570\u91cf\n\t<\/li>\n<li>\n\t\tgetActiveCount() \u5f53\u524d\u7ebf\u7a0b\u6c60\u4e2d\u6b63\u5728\u6267\u884c\u4efb\u52a1\u7684\u7ebf\u7a0b\u6570\u91cf\n\t<\/li>\n<\/ol>\n<p>\n\t<img decoding=\"async\" alt=\"\u7ebf\u7a0b\u6c60\u7ebf\u7a0b\u751f\u547d\u5468\u671f\" src=\"https:\/\/raw.githubusercontent.com\/gcy111\/xttblog\/master\/b4.png\" style=\"width: 684px;height: 285px\" \/>\n<\/p>\n<ul>\n<li>\n\t\trunning\uff1a\u80fd\u63a5\u53d7\u65b0\u63d0\u4ea4\u7684\u4efb\u52a1\uff0c\u4e5f\u80fd\u5904\u7406\u963b\u585e\u961f\u5217\u4e2d\u7684\u4efb\u52a1\n\t<\/li>\n<li>\n\t\tshutdown\uff1a\u4e0d\u80fd\u5904\u7406\u65b0\u7684\u4efb\u52a1\uff0c\u4f46\u662f\u80fd\u7ee7\u7eed\u5904\u7406\u963b\u585e\u961f\u5217\u4e2d\u4efb\u52a1\n\t<\/li>\n<li>\n\t\tstop\uff1a\u4e0d\u80fd\u63a5\u6536\u65b0\u7684\u4efb\u52a1\uff0c\u4e5f\u4e0d\u5904\u7406\u961f\u5217\u4e2d\u7684\u4efb\u52a1\n\t<\/li>\n<li>\n\t\ttidying\uff1a\u5982\u679c\u6240\u6709\u7684\u4efb\u52a1\u90fd\u5df2\u7ecf\u7ec8\u6b62\u4e86\uff0c\u8fd9\u65f6\u6709\u6548\u7ebf\u7a0b\u6570\u4e3a0\n\t<\/li>\n<li>\n\t\tterminated\uff1a\u6700\u7ec8\u72b6\u6001\n\t<\/li>\n<\/ul>\n<p>\n\t<img decoding=\"async\" alt=\"\u7ebf\u7a0b\u6c60\u539f\u7406\" src=\"https:\/\/raw.githubusercontent.com\/gcy111\/xttblog\/master\/b5.png\" style=\"width: 904px;height: 600px\" \/>\n<\/p>\n<h2>\n\t\u7ebf\u7a0b\u6c60<br \/>\n<\/h2>\n<ul>\n<li>\n\t\tExecutors.newCachedThreadPool:\u521b\u5efa\u4e00\u4e2a\u53ef\u7f13\u5b58\u7684\u7ebf\u7a0b\u6c60\uff0c\u5982\u679c\u7ebf\u7a0b\u6c60\u7684\u957f\u5ea6\u8d85\u8fc7\u4e86\u5904\u7406\u7684\u9700\u8981\uff0c\u53ef\u4ee5\u7075\u6d3b\u56de\u6536\u7a7a\u95f2\u7ebf\u7a0b\u3002\u5982\u679c\u6ca1\u6709\u53ef\u56de\u6536\u7684\u5c31\u65b0\u5efa\u7ebf\u7a0b\n\t<\/li>\n<li>\n\t\tExecutors.newFixedThreadPool:\u5b9a\u957f\u7ebf\u7a0b\u6c60\uff0c\u53ef\u4ee5\u7ebf\u7a0b\u73b0\u6210\u7684\u6700\u5927\u5e76\u53d1\u6570\uff0c\u8d85\u51fa\u5728\u961f\u5217\u7b49\u5f85\n\t<\/li>\n<li>\n\t\tExecutors.newSingleThreadExecutor:\u5355\u7ebf\u7a0b\u5316\u7684\u7ebf\u7a0b\u6c60\uff0c\u7528\u552f\u4e00\u7684\u4e00\u4e2a\u5171\u7528\u7ebf\u7a0b\u6267\u884c\u4efb\u52a1\uff0c\u4fdd\u8bc1\u6240\u6709\u4efb\u52a1\u6309\u6307\u5b9a\u987a\u5e8f\u6267\u884c\uff08FIFO\u3001\u4f18\u5148\u7ea7&hellip;\uff09\n\t<\/li>\n<li>\n\t\tExecutors.newScheduledThreadPool:\u5b9a\u957f\u7ebf\u7a0b\u6c60\uff0c\u652f\u6301\u5b9a\u65f6\u548c\u5468\u671f\u4efb\u52a1\u6267\u884c\n\t<\/li>\n<\/ul>\n<p>\n\t\u8fd9\u662fExecutors \u521b\u5efa\u5e38\u7528\u7684 4\u79cd\u7ebf\u7a0b\u6c60\uff0c\u4f46\u662f\u6211\u5728\u4e0a\u9762\u8bf4\u4e86 \u9700\u8981\u7528ThreadPoolExecutor \u6765\u521b\u5efa\uff0c\u90a3\u4e48\u600e\u4e48\u529e\u5462\uff1f \u90a3\u6211\u4eec\u4e00\u8d77\u770b\u4e00\u4e0b\u6e90\u7801\u628a\n<\/p>\n<h2>\n\tExecutors.newCachedThreadPool&nbsp;<br \/>\n<\/h2>\n<pre class=\"brush:java;\">\r\nExecutorService newCachedThreadPool = Executors.newCachedThreadPool();<\/pre>\n<p>\n\t\u6e90\u7801\u5982\u4e0b\uff1a\n<\/p>\n<pre class=\"brush:java;\">\r\npublic static ExecutorService newCachedThreadPool() {\r\n\treturn new ThreadPoolExecutor(0, Integer.MAX_VALUE,\r\n\t\t\t\t\t\t\t\t  60L, TimeUnit.SECONDS,\r\n\t\t\t\t\t\t\t\t  new SynchronousQueue&lt;Runnable&gt;());\r\n}<\/pre>\n<h2>\n\tExecutors.newFixedThreadPool<br \/>\n<\/h2>\n<pre class=\"brush:java;\">\r\nExecutorService newFixedThreadPool = Executors.newFixedThreadPool(5);<\/pre>\n<p>\n\t\u5bf9\u5e94\u7684\u63a5\u53e3\u65b9\u6cd5\u5982\u4e0b\uff1a\n<\/p>\n<pre class=\"brush:java;\">\r\npublic static ExecutorService newFixedThreadPool(int nThreads) {\r\n\treturn new ThreadPoolExecutor(nThreads, nThreads,\r\n\t\t\t\t\t\t\t\t  0L, TimeUnit.MILLISECONDS,\r\n\t\t\t\t\t\t\t\t  new LinkedBlockingQueue&lt;Runnable&gt;());\r\n}<\/pre>\n<h2>\n\tExecutors.newSingleThreadExecutor<br \/>\n<\/h2>\n<pre class=\"brush:java;\">\r\nExecutorService newSingleThreadExecutor = Executors.newSingleThreadExecutor();<\/pre>\n<p>\n\t\u5bf9\u5e94\u7684\u63a5\u53e3\u6e90\u7801\u5982\u4e0b\uff1a\n<\/p>\n<pre class=\"brush:java;\">\r\npublic static ExecutorService newSingleThreadExecutor() {\r\n\treturn new FinalizableDelegatedExecutorService\r\n\t\t(new ThreadPoolExecutor(1, 1,\r\n\t\t\t\t\t\t\t\t0L, TimeUnit.MILLISECONDS,\r\n\t\t\t\t\t\t\t\tnew LinkedBlockingQueue&lt;Runnable&gt;()));\r\n}<\/pre>\n<h2>\n\tExecutors.newScheduledThreadPool<br \/>\n<\/h2>\n<p>\n\tScheduledExecutorService\u63d0\u4f9b\u4e86\u4e09\u79cd\u65b9\u6cd5\u53ef\u4ee5\u4f7f\u7528\uff1a&nbsp;\n<\/p>\n<p>\n\t<img decoding=\"async\" alt=\"ScheduledExecutorService\" src=\"https:\/\/raw.githubusercontent.com\/gcy111\/xttblog\/master\/b6.png\" style=\"width: 592px;height: 116px\" \/>\n<\/p>\n<ul>\n<li>\n\t\tscheduleAtFixedRate\uff1a\u4ee5\u6307\u5b9a\u7684\u901f\u7387\u6267\u884c\u4efb\u52a1&nbsp;\n\t<\/li>\n<li>\n\t\tscheduleWithFixedDelay\uff1a\u4ee5\u6307\u5b9a\u7684\u5ef6\u8fdf\u6267\u884c\u4efb\u52a1&nbsp;\n\t<\/li>\n<\/ul>\n<p>\n\t\u4e3e\u4f8b\uff1a\n<\/p>\n<pre class=\"brush:java;\">\r\nexecutorService.scheduleAtFixedRate(new Runnable() {\r\n    @Override\r\n    public void run() {\r\n        log.warn(&quot;schedule run&quot;);\r\n    }\r\n}, 1, 3, TimeUnit.SECONDS);\/\/\u5ef6\u8fdf\u4e00\u79d2\u540e\u6bcf\u96943\u79d2\u6267\u884c<\/pre>\n<p>\n\t\u5c0f\u6269\u5c55\uff1a\u5ef6\u8fdf\u6267\u884c\u4efb\u52a1\u7684\u64cd\u4f5c\uff0cjava\u4e2d\u8fd8\u6709Timer\u7c7b\u540c\u6837\u53ef\u4ee5\u5b9e\u73b0\uff1a\n<\/p>\n<pre class=\"brush:java;\">\r\nTimer timer = new Timer();\r\ntimer.schedule(new TimerTask() {\r\n    @Override\r\n    public void run() {\r\n        log.warn(&quot;timer run&quot;);\r\n    }\r\n}, new Date(), 5 * 1000);\r\n<\/pre>\n<!-- <audio autoplay=\"autoplay\" height=\"100\" width=\"100\" hidden=\"true\"><source src=\"https:\/\/c.coka.la\/orAvPR.mp3\" type=\"audio\/mp3\"\/><\/audio><img decoding=\"async\" src=\"https:\/\/images.weserv.nl\/?url=https:\/\/ws3.sinaimg.cn\/large\/8fa5dcfcgy1g4em3rzar5j20ib089ae0.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/> --><p><!-- <img decoding=\"async\" src=\"https:\/\/raw.githubusercontent.com\/xmt1139057136\/xttblog\/master\/8fa5dcfcgy1g4em3rzar5j20ib089ae0.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/>--><img decoding=\"async\" src=\"https:\/\/saas.51autoparts.cn\/api\/admin\/sys-file\/oss\/file?fileName=84db51f8561e448f9a4fda2eff0811f9.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/><\/p><p>\u6700\u540e\uff0c\u6b22\u8fce\u5173\u6ce8\u6211\u7684\u4e2a\u4eba\u5fae\u4fe1\u516c\u4f17\u53f7\uff1a\u4e1a\u4f59\u8349\uff08yyucao\uff09\uff01<b>\u53ef\u52a0\u4f5c\u8005\u5fae\u4fe1\u53f7\uff1a<font color=\"red\">xttblog2<\/font>\u3002\u5907\u6ce8\uff1a\u201c1\u201d\uff0c\u6dfb\u52a0\u535a\u4e3b\u5fae\u4fe1\u62c9\u4f60\u8fdb\u5fae\u4fe1\u7fa4\u3002\u5907\u6ce8\u9519\u8bef\u4e0d\u4f1a\u540c\u610f\u597d\u53cb\u7533\u8bf7\u3002<\/b>\u518d\u6b21\u611f\u8c22\u60a8\u7684\u5173\u6ce8\uff01\u540e\u7eed\u6709\u7cbe\u5f69\u5185\u5bb9\u4f1a\u7b2c\u4e00\u65f6\u95f4\u53d1\u7ed9\u60a8\uff01\u539f\u521b\u6587\u7ae0\u6295\u7a3f\u8bf7\u53d1\u9001\u81f3532009913@qq.com\u90ae\u7bb1\u3002\u5546\u52a1\u5408\u4f5c\u4e5f\u53ef\u6dfb\u52a0\u4f5c\u8005\u5fae\u4fe1\u8fdb\u884c\u8054\u7cfb\uff01<\/p><p>\u672c\u6587\u539f\u6587\u51fa\u5904\uff1a<a href=\"https:\/\/www.xttblog.com\">\u4e1a\u4f59\u8349<\/a>\uff1a &raquo; <a href=\"https:\/\/www.xttblog.com\/?p=3141\">Java \u7ebf\u7a0b\u6c60 Executors \u6559\u7a0b<\/a><\/p><!-- <div class=\"bd-reward-stl\"><button id=\"bdRewardBtn\"><span><\/span><\/button><\/div> -->","protected":false},"excerpt":{"rendered":"<p>\u8fd9\u7bc7\u6587\u7ae0\u6211\u672c\u6765\u5199\u597d\u4e86\uff0c\u4f46\u662f\u6ca1\u6709\u4fdd\u5b58\uff0c\u6211\u51fa\u53bb\u4e8610\u5206\u949f\uff0c\u7136\u540e\u56de\u6765\u4e86\uff0c\u53d1\u73b0\u6211\u7684\u7535\u8111\u88ab\u5173\u4e86\u3002\u7136\u540e\u6211\u5988\u8bf4\uff1a&#038;ldquo [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[1644,1705,884,531,883],"class_list":["post-3141","post","type-post","status-publish","format-standard","hentry","category-java","tag-countdownlatch","tag-executors","tag-threadpoolexecutor","tag-531","tag-883"],"_links":{"self":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/3141","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3141"}],"version-history":[{"count":1,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/3141\/revisions"}],"predecessor-version":[{"id":3154,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/3141\/revisions\/3154"}],"wp:attachment":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3141"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3141"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3141"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}