{"id":209,"date":"2026-07-17T20:35:14","date_gmt":"2026-07-17T12:35:14","guid":{"rendered":"https:\/\/info.ect.fyi\/blog\/?p=209"},"modified":"2026-07-17T20:39:03","modified_gmt":"2026-07-17T12:39:03","slug":"%e5%b9%b6%e5%8f%91%e7%bc%96%e7%a8%8b%e5%b8%b8%e8%a7%81%e6%96%b9%e6%b3%95%ef%bc%88%e4%ba%8c%ef%bc%89","status":"publish","type":"post","link":"https:\/\/info.ect.fyi\/blog\/?p=209","title":{"rendered":"\u5e76\u53d1\u7f16\u7a0b\u5e38\u89c1\u65b9\u6cd5\uff08\u4e8c\uff09"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">\u5f88\u591a\u65f6\u5019\uff0c\u57fa\u7840\u5e76\u4e0d\u4ee3\u8868\u597d\u7528\u3002Java\u7684\u591a\u7ebf\u7a0b\u7f16\u7a0b\u4e5f\u662f\u4e00\u6837\u3002\u7075\u6d3b\u4f7f\u7528\u57fa\u672c\u7684Thread\u786e\u5b9e\u53ef\u4ee5\u89e3\u51b3\u5927\u90e8\u5206\u95ee\u9898\uff0c\u4f46\u540c\u65f6\u4f1a\u727a\u7272\u4e00\u4e9b\u4e1c\u897f\uff0c\u5305\u62ec\u6027\u80fd\u548c\u53ef\u7ef4\u62a4\u6027\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u63a5\u4e0b\u6765\u662f\u4e00\u4e9b\u5b9e\u9645\u5f00\u53d1\u975e\u5e38\u5e38\u89c1\u7684\u529f\u80fd\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">ExecutorService<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u662f\u4e00\u4e2a\u7ebf\u7a0b\u6c60\uff0c\u53ef\u4ee5\u7ba1\u7406\u5f88\u591a\u7ebf\u7a0b\u3002\u5b83\u6709\u4e00\u4e9b\u4e0d\u540c\u7684\u58f0\u660e\u65b9\u5f0f\uff0c\u7528\u9014\u5404\u4e0d\u76f8\u540c\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u521b\u5efa\u4e00\u4e2a\u7ebf\u7a0b\u6c60\uff0c\u56fa\u5b9a5\u4e2a\u7ebf\u7a0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ExecutorService pool = Executors.newFixedThreadPool(5);\n\/\/ newFixedThreadPool\u4f7f\u7528\u7684\u963b\u585e\u961f\u5217\u6ca1\u6709\u4e0a\u9650\uff0c\u5bb9\u6613\u5bfc\u81f4OOM\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u6216\u8005\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ExecutorService pool = new ThreadPoolExecutor(\n\t5, \/\/ \u6838\u5fc3\u7ebf\u7a0b\u6570\uff0c\u95f2\u7f6e\u4f1a\u4e00\u76f4\u4fdd\u6301\u8fd9\u4e2a\u6570\u91cf\n\t10, \/\/\u6700\u5927\u7ebf\u7a0b\u6570\n\t60, \/\/ \u5b58\u6d3b\u65f6\u95f4\uff0c\u9002\u7528\u4e8e\u6838\u5fc3\u7ebf\u7a0b\u4ee5\u5916\u591a\u51fa\u7684\u7ebf\u7a0b\n\tTimeUnit.SECONDS, \/\/ \u65f6\u95f4\u5355\u4f4d\n\tnew LinkedBlockingQueue&lt;&gt;(100) \/\/ \u8fd9\u4e2a\u963b\u585e\u961f\u5217\u4f1a\u7528\u6765\u5b58\u653e\u4efb\u52a1\n);\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u53ea\u6709\u7b49\u5f85\u961f\u5217\u5df2\u6ee1\u65f6\uff0c\u624d\u4f1a\u5c1d\u8bd5\u5f00\u542f\u6838\u5fc3\u7ebf\u7a0b\u4ee5\u5916\u7684\u7ebf\u7a0b\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u6838\u5fc3\u65b9\u6cd5<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><code>submit()<\/code>\u63a5\u53d7\u4e00\u4e2aRunnable\u6216Callable\u5bf9\u8c61\uff0c\u8fd4\u56de\u4e00\u4e2a<strong>Future<\/strong>\u3002Future\u7c7b\u4f1a\u7acb\u5373\u8fd4\u56de\uff0c\u4f46\u4efb\u52a1\u6b64\u65f6\u5e76\u6ca1\u6709\u5b8c\u6210\u3002Future\u548c\u8fd9\u4e2a\u4efb\u52a1\u6709\u5bf9\u5e94\u5173\u7cfb\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>execute()<\/code>\u63a5\u53d7\u4e00\u4e2aRunnable\u5bf9\u8c61\uff0c\u5b83\u6ca1\u6709\u8fd4\u56de\u503c\u3002\u4ec5\u4ec5\u662f\u6267\u884c\u4e86\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>shutdown()<\/code>\u7528\u4e8e\u5173\u95ed\u7ebf\u7a0b\u6c60\u3002\u6b64\u540e\u7ebf\u7a0b\u6c60\u5c06\u4e0d\u4f1a\u63a5\u53d7\u65b0\u7684\u4efb\u52a1\uff0c\u4f46\u4f1a\u7b49\u5f85\u5df2\u6709\u4efb\u52a1\u6267\u884c\u5b8c\u6bd5\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>shutdownNow()<\/code>\u66f4\u52a0\u66b4\u529b\uff0c\u4f1a\u7acb\u5373\u5173\u95ed\u7ebf\u7a0b\u6c60\u5e76\u5f3a\u884c\u4e2d\u65ad\u6240\u6709\u4efb\u52a1\u3002\u5b83\u53ef\u80fd\u4e0d\u4f1a\u6210\u529f\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>awaitTermination()<\/code>\u5728\u7ebf\u7a0b\u6c60\u88ab\u5173\u95ed\u540e\u624d\u4f1a\u8fd4\u56de\uff0c\u5bf9\u4e8e<code>shutdown<\/code>\u7684\u60c5\u51b5\uff0c\u5b83\u4f1a\u7b49\u5f85\u4efb\u52a1\u6267\u884c\u5b8c\u6bd5\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u521b\u5efa\u65b9\u5f0f<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><code>Executors<\/code>\u5de5\u5177\u7c7b\u63d0\u4f9b\u4e86\u4e00\u4e9b\u9884\u8bbe\u7684<code>ExecutorService<\/code>\uff1a<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>newFixedThreadPool(5)<\/code> &#8212; \u56fa\u5b9a\u5927\u5c0f\u7684\u7ebf\u7a0b\u6c60<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>newSingleThreadExecutor()<\/code> &#8212; \u5355\u4e00\u7ebf\u7a0b\u7684Executor<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>newCachedThreadPool()<\/code> &#8212; \u7f13\u5b58\u7ebf\u7a0b\u6c60\uff0c\u5b83\u7684\u6838\u5fc3\u7ebf\u7a0b\u6570\u4e3a0<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>newScheduledThreadPool(2)<\/code> &#8212; \u5b9a\u65f6\u7ebf\u7a0b\u6c60\uff0c\u6709\u4e00\u4e9b\u6709\u7528\u7684\u5b9a\u65f6\u7279\u6027<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Future<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Future\u548c\u4e00\u4e2a\u4efb\u52a1\u6709\u5bf9\u5e94\u5173\u7cfb\u3002\u5b83\u4f1a\u88abExecutorService\u7acb\u5373\u8fd4\u56de\uff0c\u53ef\u4ee5\u901a\u8fc7\u5b83\u83b7\u53d6\u4efb\u52a1\u7684\u6267\u884c\u7ed3\u679c\u3002\u4e0d\u8fc7\uff0c\u5f53\u7136\u9700\u8981\u4efb\u52a1\u771f\u6b63\u6267\u884c\u5b8c\u6bd5\u624d\u80fd\u770b\u5230\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Future\u662f\u6709\u6cdb\u578b\u7684\uff0c\u56e0\u4e3a\u652f\u6301\u7684\u8fd4\u56de\u7c7b\u578b\u53ef\u4ee5\u6709\u5f88\u591a\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>Future.get()<\/code>\u8fd4\u56de\u4efb\u52a1\u7684\u8fd4\u56de\u503c\u3002\u5982\u679c\u4efb\u52a1\u8fd8\u6ca1\u6709\u5b8c\u6210\uff0c\u5b83\u4f1a\u963b\u585e\u5f53\u524d\u7ebf\u7a0b\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5b83\u8fd8\u63d0\u4f9b\u4e86<code>isDone()<\/code>, <code>cancel()<\/code>\u548c<code>isCancelled()<\/code>\u65b9\u6cd5\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>cancel(true)<\/code>\u4f1a\u4e2d\u65ad\u5df2\u7ecf\u5728\u8fd0\u884c\u7684\u4efb\u52a1\uff1b<code>cancel(false)<\/code>\u53ea\u4f1a\u53d6\u6d88\u8fd8\u6ca1\u5f00\u59cb\u6392\u961f\u6216\u5728\u961f\u5217\u4e2d\u7b49\u5f85\u7684\u4efb\u52a1\uff0c\u5982\u679c\u5df2\u7ecf\u5728\u8fd0\u884c\uff0c\u5b83\u4f1a\u7ee7\u7eed\u8fd0\u884c\u3002\u5b83\u8fd4\u56de\u4e00\u4e2a\u5e03\u5c14\u503c\uff0c\u4ee3\u8868\u662f\u5426\u6210\u529f\u53d6\u6d88\u4e86\u4efb\u52a1\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Lock<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u662f\u4e00\u7c7b\u7528\u4e8e\u7ba1\u7406\u8d44\u6e90\u7684\u9501\u5bf9\u8c61\u3002<code>ReentrantLock<\/code>\u662f\u6700\u5e38\u89c1\u7684Lock\uff0c\u4e4b\u6240\u4ee5\u53eb\u505a\u201cReentrant\u201d\uff0c\u662f\u56e0\u4e3a\u5b83\u5177\u6709\u53ef\u91cd\u5165\u6027\uff08\u540c\u4e00\u7ebf\u7a0b\u53ef\u4ee5\u53cd\u590d\u83b7\u5f97\u540c\u4e00\u628a\u9501\uff09\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f7f\u7528\u9501\u6700\u6838\u5fc3\u7684\u539f\u5219\u662f\uff0c\u8981\u4fdd\u8bc1\u9501\u5728\u64cd\u4f5c\u5b8c\u6210\u540e\u4e00\u5b9a\u88ab\u91ca\u653e\uff0c\u65e0\u8bba\u53d1\u751f\u4e86\u4ec0\u4e48\u3002\u8fd9\u5e38\u5e38\u9700\u8981<code>try<\/code>\u642d\u914d<code>finally<\/code>\u4ee3\u7801\u5757\u5b9e\u73b0\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>lock()<\/code>\u65b9\u6cd5\u7528\u4e8e\u52a0\u9501\uff0c\u5982\u679c\u5df2\u7ecf\u5904\u4e8e\u9501\u5b9a\u72b6\u6001\uff0c\u5b83\u4f1a\u7b49\u5f85\u76f4\u81f3\u89e3\u9501\uff0c\u7136\u540e\u518d\u9501\u5b9a\u5b83\u5e76\u8fd4\u56de\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>unlock()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u89e3\u9501\u3002\u5728\u7ebf\u7a0b\u5b8c\u6210\u81ea\u5df1\u7684\u4e1a\u52a1\u64cd\u4f5c\u540e\uff0c\u5fc5\u987b\u8c03\u7528\u5b83\u3002\u53ea\u6709\u9501\u7531\u5f53\u524d\u7ebf\u7a0b\u6301\u6709\u65f6\u624d\u53ef\u4ee5\u8c03\u7528\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>tryLock(1, TimeUnit.SECONDS)<\/code>\u7528\u4e8e\u5c1d\u8bd5\u52a0\u9501\uff0c\u5728\u6307\u5b9a\u65f6\u95f4\u5185\uff0c\u5982\u679c\u6ca1\u6709\u6210\u529f\u83b7\u53d6\u9501\uff0c\u5b83\u4f1a\u8fd4\u56defalse\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f88\u591a\u65f6\u5019\uff0c\u57fa\u7840\u5e76\u4e0d\u4ee3\u8868\u597d\u7528\u3002Java\u7684\u591a\u7ebf\u7a0b\u7f16\u7a0b\u4e5f\u662f\u4e00\u6837\u3002\u7075\u6d3b\u4f7f\u7528\u57fa\u672c\u7684Thread\u786e\u5b9e\u53ef\u4ee5\u89e3\u51b3\u5927\u90e8\u5206\u95ee\u9898\uff0c\u4f46 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12],"tags":[],"class_list":["post-209","post","type-post","status-publish","format-standard","hentry","category-12"],"_links":{"self":[{"href":"https:\/\/info.ect.fyi\/blog\/index.php?rest_route=\/wp\/v2\/posts\/209","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/info.ect.fyi\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/info.ect.fyi\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/info.ect.fyi\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/info.ect.fyi\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=209"}],"version-history":[{"count":5,"href":"https:\/\/info.ect.fyi\/blog\/index.php?rest_route=\/wp\/v2\/posts\/209\/revisions"}],"predecessor-version":[{"id":214,"href":"https:\/\/info.ect.fyi\/blog\/index.php?rest_route=\/wp\/v2\/posts\/209\/revisions\/214"}],"wp:attachment":[{"href":"https:\/\/info.ect.fyi\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=209"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/info.ect.fyi\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=209"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/info.ect.fyi\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=209"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}