{"id":4079,"date":"2024-04-24T08:30:13","date_gmt":"2024-04-24T00:30:13","guid":{"rendered":"https:\/\/xinchewhd.com.cn\/index.php\/%e9%9d%a2%e8%af%95\/%e9%9d%a2%e8%af%95%e5%ae%98%ef%bc%9ajava%e4%b8%ad%e5%a6%82%e4%bd%95%e5%88%9b%e5%bb%ba%e7%ba%bf%e7%a8%8b%e6%b1%a0%ef%bc%9f\/"},"modified":"2024-04-24T08:30:13","modified_gmt":"2024-04-24T00:30:13","slug":"%e9%9d%a2%e8%af%95%e5%ae%98%ef%bc%9ajava%e4%b8%ad%e5%a6%82%e4%bd%95%e5%88%9b%e5%bb%ba%e7%ba%bf%e7%a8%8b%e6%b1%a0%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/xinchewhd.com.cn\/index.php\/%e9%9d%a2%e8%af%95\/%e9%9d%a2%e8%af%95%e5%ae%98%ef%bc%9ajava%e4%b8%ad%e5%a6%82%e4%bd%95%e5%88%9b%e5%bb%ba%e7%ba%bf%e7%a8%8b%e6%b1%a0%ef%bc%9f\/","title":{"rendered":"\u9762\u8bd5\u5b98\uff1aJava\u4e2d\u5982\u4f55\u521b\u5efa\u7ebf\u7a0b\u6c60\uff1f"},"content":{"rendered":"<div class='wxsyncmain'>\n<p data-v-md-line=\"1\"><span style=\"font-size: 15px;\">\u5728Java\u4e2d\u521b\u5efa\u7ebf\u7a0b\u6c60\u901a\u5e38\u662f\u901a\u8fc7java.util.concurrent\u5305\u4e2d\u7684Executors\u5de5\u5382\u7c7b\u6216\u8005\u76f4\u63a5\u4f7f\u7528ThreadPoolExecutor\u7c7b\u6765\u5b8c\u6210\u7684\u3002\u8fd9\u91cc\u6709\u51e0\u79cd\u5e38\u89c1\u7684\u65b9\u5f0f\u6765\u521b\u5efa\u7ebf\u7a0b\u6c60\uff1a<\/span><\/p>\n<h3 data-v-md-heading=\"\u4f7f\u7528-executors-\u5de5\u5382\u65b9\u6cd5\" data-v-md-line=\"3\" style=\"margin-top: 24px;margin-bottom: 24px;\"><strong><span style=\"font-size: 20px;\">1. \u4f7f\u7528Executors\u5de5\u5382\u65b9\u6cd5<\/span><\/strong><\/h3>\n<ul class=\"list-paddingleft-1\" style=\"list-style-type: disc;\">\n<li>\n<p data-v-md-line=\"5\"><span style=\"font-size: 15px;\">\u56fa\u5b9a\u6570\u91cf\u7684\u7ebf\u7a0b\u6c60\uff1a<\/span><\/p>\n<\/li>\n<\/ul>\n<section class=\"code-snippet__fix code-snippet__js\">\n<ul class=\"code-snippet__line-index code-snippet__js\">\n<li><\/li>\n<li><\/li>\n<\/ul>\n<pre class=\"code-snippet__js\" data-lang=\"nginx\"><code><span class=\"code-snippet_outer\"><span class=\"code-snippet__attribute\">int<\/span> nThreads = <span class=\"code-snippet__number\">5<\/span>;<\/span><\/code><code><span class=\"code-snippet_outer\"><span class=\"code-snippet__attribute\">ExecutorService<\/span> pool = Executors.newFixedThreadPool(nThreads);<\/span><\/code><\/pre>\n<\/section>\n<ul class=\"list-paddingleft-1\" style=\"list-style-type: disc;\">\n<li>\n<p><span style=\"font-size: 15px;\">\u5355\u7ebf\u7a0b\u7684\u7ebf\u7a0b\u6c60\uff08\u4fdd\u8bc1\u4efb\u52a1\u6309\u987a\u5e8f\u6267\u884c\uff09\uff1a<\/span><\/p>\n<\/li>\n<\/ul>\n<section class=\"code-snippet__fix code-snippet__js\">\n<ul class=\"code-snippet__line-index code-snippet__js\">\n<li><\/li>\n<\/ul>\n<pre class=\"code-snippet__js\" data-lang=\"nginx\"><code><span class=\"code-snippet_outer\"><span class=\"code-snippet__attribute\">ExecutorService<\/span> pool = Executors.newSingleThreadExecutor();<\/span><\/code><\/pre>\n<\/section>\n<ul class=\"list-paddingleft-1\" style=\"list-style-type: disc;\">\n<li>\n<p><span style=\"font-size: 15px;\">3\u3001\u53ef\u7f13\u5b58\u7684\u7ebf\u7a0b\u6c60\uff08\u81ea\u52a8\u7ebf\u7a0b\u56de\u6536\u548c\u521b\u5efa\uff09\uff1a<\/span><\/p>\n<\/li>\n<\/ul>\n<section class=\"code-snippet__fix code-snippet__js\">\n<ul class=\"code-snippet__line-index code-snippet__js\">\n<li><\/li>\n<\/ul>\n<pre class=\"code-snippet__js\" data-lang=\"nginx\"><code><span class=\"code-snippet_outer\"><span class=\"code-snippet__attribute\">ExecutorService<\/span> pool = Executors.newCachedThreadPool();<\/span><\/code><\/pre>\n<\/section>\n<ul class=\"list-paddingleft-1\" style=\"list-style-type: disc;\">\n<li>\n<p><span style=\"font-size: 15px;\">4.\u8ba1\u5212\u4efb\u52a1\u7684\u7ebf\u7a0b\u6c60\uff08\u7528\u4e8e\u5ef6\u8fdf\u6216\u5468\u671f\u6027\u6267\u884c\u4efb\u52a1\uff09\uff1a<\/span><\/p>\n<\/li>\n<\/ul>\n<section class=\"code-snippet__fix code-snippet__js\">\n<ul class=\"code-snippet__line-index code-snippet__js\">\n<li><\/li>\n<\/ul>\n<pre class=\"code-snippet__js\" data-lang=\"nginx\"><code><span class=\"code-snippet_outer\"><span class=\"code-snippet__attribute\">ScheduledExecutorService<\/span> scheduler = Executors.newScheduledThreadPool(nThreads);<\/span><\/code><\/pre>\n<\/section>\n<h3 data-v-md-heading=\"\u76f4\u63a5\u4f7f\u7528-threadpoolexecutor-\u6784\u9020\u51fd\u6570\" data-v-md-line=\"26\" style=\"margin-top: 24px;margin-bottom: 24px;\"><strong><span style=\"font-size: 20px;\">2. \u76f4\u63a5\u4f7f\u7528ThreadPoolExecutor\u6784\u9020\u51fd\u6570<\/span><\/strong><\/h3>\n<p data-v-md-line=\"28\"><span style=\"font-size: 15px;\">\u5982\u679c\u4f60\u9700\u8981\u66f4\u591a\u7684\u5b9a\u5236\uff0c\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528ThreadPoolExecutor\u7684\u6784\u9020\u5668\u6765\u521b\u5efa\u7ebf\u7a0b\u6c60\uff1a<\/span><\/p>\n<section class=\"code-snippet__fix code-snippet__js\">\n<ul class=\"code-snippet__line-index code-snippet__js\">\n<li><\/li>\n<li><\/li>\n<li><\/li>\n<li><\/li>\n<li><\/li>\n<li><\/li>\n<li><\/li>\n<li><\/li>\n<li><\/li>\n<li><\/li>\n<li><\/li>\n<li><\/li>\n<\/ul>\n<pre class=\"code-snippet__js\" data-lang=\"cs\"><code><span class=\"code-snippet_outer\"><span class=\"code-snippet__keyword\">int<\/span> corePoolSize = <span class=\"code-snippet__number\">5<\/span>; <span class=\"code-snippet__comment\">\/\/ \u6838\u5fc3\u7ebf\u7a0b\u6570<\/span><\/span><\/code><code><span class=\"code-snippet_outer\"><span class=\"code-snippet__keyword\">int<\/span> maximumPoolSize = <span class=\"code-snippet__number\">10<\/span>; <span class=\"code-snippet__comment\">\/\/ \u6700\u5927\u7ebf\u7a0b\u6570<\/span><\/span><\/code><code><span class=\"code-snippet_outer\"><span class=\"code-snippet__keyword\">long<\/span> keepAliveTime = <span class=\"code-snippet__number\">60<\/span>; <span class=\"code-snippet__comment\">\/\/ \u975e\u6838\u5fc3\u7ebf\u7a0b\u7a7a\u95f2\u7b49\u5f85\u65b0\u4efb\u52a1\u7684\u6700\u957f\u65f6\u95f4<\/span><\/span><\/code><code><span class=\"code-snippet_outer\">TimeUnit unit = TimeUnit.SECONDS; <span class=\"code-snippet__comment\">\/\/ keepAliveTime \u7684\u65f6\u95f4\u5355\u4f4d<\/span><\/span><\/code><code><span class=\"code-snippet_outer\">BlockingQueue&lt;Runnable&gt; workQueue = <span class=\"code-snippet__keyword\">new<\/span> ArrayBlockingQueue&lt;&gt;(<span class=\"code-snippet__number\">100<\/span>); <span class=\"code-snippet__comment\">\/\/ \u4efb\u52a1\u961f\u5217<\/span><\/span><\/code><code><span class=\"code-snippet_outer\"><\/span><\/code><code><span class=\"code-snippet_outer\">ThreadPoolExecutor pool = <span class=\"code-snippet__keyword\">new<\/span> ThreadPoolExecutor(<\/span><\/code><code><span class=\"code-snippet_outer\">        corePoolSize,<\/span><\/code><code><span class=\"code-snippet_outer\">        maximumPoolSize,<\/span><\/code><code><span class=\"code-snippet_outer\">        keepAliveTime,<\/span><\/code><code><span class=\"code-snippet_outer\">        unit,<\/span><\/code><code><span class=\"code-snippet_outer\">        workQueue);<\/span><\/code><\/pre>\n<\/section>\n<h3 data-v-md-heading=\"\u53c2\u6570\u89e3\u91ca\" data-v-md-line=\"45\"><strong><span style=\"font-size: 15px;\">\u53c2\u6570\u89e3\u91ca\uff1a<\/span><\/strong><\/h3>\n<ul data-v-md-line=\"47\" class=\"list-paddingleft-1\">\n<li>\n<p><span style=\"font-size: 15px;\">corePoolSize: \u6838\u5fc3\u7ebf\u7a0b\u6570 \u2014\u2014 \u7cfb\u7edf\u59cb\u7ec8\u7ef4\u62a4\u7684\u7ebf\u7a0b\u6570\u91cf\u3002<\/span><\/p>\n<\/li>\n<li>\n<p><span style=\"font-size: 15px;\">maximumPoolSize: \u6700\u5927\u7ebf\u7a0b\u6570 \u2014\u2014 \u5141\u8bb8\u5b58\u5728\u7684\u6700\u5927\u7ebf\u7a0b\u6570\u91cf\u3002<\/span><\/p>\n<\/li>\n<li>\n<p><span style=\"font-size: 15px;\">keepAliveTime: \u5f53\u7ebf\u7a0b\u6570\u91cf\u8d85\u8fc7\u6838\u5fc3\u7ebf\u7a0b\u6570\u91cf\u65f6\uff0c\u8fd9\u662f\u989d\u5916\u7ebf\u7a0b\u7a7a\u95f2\u7684\u6700\u957f\u65f6\u95f4\uff0c\u4e4b\u540e\u4f1a\u88ab\u56de\u6536\u3002<\/span><\/p>\n<\/li>\n<li>\n<p><span style=\"font-size: 15px;\">unit: keepAliveTime\u53c2\u6570\u7684\u65f6\u95f4\u5355\u4f4d\u3002<\/span><\/p>\n<\/li>\n<li>\n<p><span style=\"font-size: 15px;\">workQueue: \u5f53\u6240\u6709\u6838\u5fc3\u7ebf\u7a0b\u90fd\u5fd9\u65f6\uff0c\u7528\u6765\u4fdd\u5b58\u5f85\u5904\u7406\u4efb\u52a1\u7684\u961f\u5217\u3002<\/span><\/p>\n<\/li>\n<\/ul>\n<p data-v-md-line=\"53\"><span style=\"font-size: 15px;\">\u4e00\u65e6\u4f60\u521b\u5efa\u4e86\u4e00\u4e2a\u7ebf\u7a0b\u6c60\uff0c\u4f60\u5c31\u53ef\u4ee5\u7528\u5176execute\u65b9\u6cd5\u63d0\u4ea4\u5b9e\u73b0\u4e86Runnable\u63a5\u53e3\u7684\u4efb\u52a1\uff0c\u6216\u8005\u7528submit\u65b9\u6cd5\u63d0\u4ea4\u5b9e\u73b0\u4e86Callable\u63a5\u53e3\u7684\u4efb\u52a1\u3002\u8bb0\u5f97\uff0c\u5728\u4f7f\u7528\u5b8c\u7ebf\u7a0b\u6c60\u540e\uff0c\u5e94\u8be5\u8c03\u7528\u5b83\u7684shutdown\u6216shutdownNow\u65b9\u6cd5\u6765\u5173\u95ed\u7ebf\u7a0b\u6c60\uff0c\u4ee5\u91ca\u653e\u8d44\u6e90\u3002<\/span><\/p>\n<p data-v-md-line=\"55\"><span style=\"font-size: 15px;\">\u4f8b\u5982:<\/span><\/p>\n<section class=\"code-snippet__fix code-snippet__js\">\n<ul class=\"code-snippet__line-index code-snippet__js\">\n<li><\/li>\n<li><\/li>\n<li><\/li>\n<li><\/li>\n<li><\/li>\n<li><\/li>\n<li><\/li>\n<\/ul>\n<pre class=\"code-snippet__js\" data-lang=\"javascript\"><code><span class=\"code-snippet_outer\">pool.execute(<span class=\"code-snippet__keyword\">new<\/span> RunnableTask());<\/span><\/code><code><span class=\"code-snippet_outer\">Future&lt;<span class=\"code-snippet__built_in\">String<\/span>&gt; future = pool.submit(<span class=\"code-snippet__keyword\">new<\/span> CallableTask());<\/span><\/code><code><span class=\"code-snippet_outer\"><\/span><\/code><code><span class=\"code-snippet_outer\"><span class=\"code-snippet__comment\">\/\/ ...<\/span><\/span><\/code><code><span class=\"code-snippet_outer\"><\/span><\/code><code><span class=\"code-snippet_outer\">pool.shutdown(); <span class=\"code-snippet__comment\">\/\/ \u5e73\u7f13\u5173\u95ed\uff0c\u4e0d\u518d\u63a5\u53d7\u65b0\u4efb\u52a1\uff0c\u4f46\u662f\u4f1a\u5904\u7406\u6240\u6709\u5df2\u7ecf\u63d0\u4ea4\u7684\u4efb\u52a1<\/span><\/span><\/code><code><span class=\"code-snippet_outer\"><span class=\"code-snippet__comment\">\/\/ pool.shutdownNow(); \/\/ \u5c1d\u8bd5\u7acb\u5373\u5173\u95ed\uff0c\u5c1d\u8bd5\u505c\u6b62\u6b63\u5728\u6267\u884c\u7684\u4efb\u52a1\uff0c\u4e0d\u5904\u7406\u961f\u5217\u4e2d\u7b49\u5f85\u7684\u4efb\u52a1<\/span><\/span><\/code><\/pre>\n<\/section>\n<p><span style=\"font-size: 15px;\">\u8fd9\u6837\u5c31\u53ef\u4ee5\u521b\u5efa\u5e76\u6709\u6548\u5730\u7ba1\u7406\u4e00\u4e2aJava\u7ebf\u7a0b\u6c60\uff0c\u4f7f\u5f97\u4f60\u80fd\u591f\u63d0\u9ad8\u591a\u7ebf\u7a0b\u5e94\u7528\u7a0b\u5e8f\u7684\u6027\u80fd\u548c\u54cd<\/span><span style=\"font-size: 15px;\"><\/span><span style=\"font-size: 15px;\">\u5e94\u901f\u5ea6\u3002<\/span><span style=\"font-size: 15px;letter-spacing: 0.034em;\"><\/span><\/p>\n<p style=\"display: none;\"><mp-style-type data-value=\"3\"><\/mp-style-type><\/p>\n<blockquote>\n<p>\u672c\u7bc7\u6587\u7ae0\u6765\u6e90\u4e8e\u5fae\u4fe1\u516c\u4f17\u53f7:                 \u4e92\u8054\u7f51\u9762\u8bd5\u5c0f\u5e2e\u624b              <\/p>\n<\/blockquote>\n<\/div>\n","raw":"","protected":false},"excerpt":{"rendered":"<p>\u5728Java\u4e2d\u521b\u5efa\u7ebf\u7a0b\u6c60\u901a\u5e38\u662f\u901a\u8fc7java.util.concurrent\u5305\u4e2d\u7684Executors\u5de5\u5382\u7c7b\u6216\u8005\u76f4\u63a5\u4f7f\u7528ThreadPo &#8230;<\/p>\n","protected":false},"author":1,"featured_media":4076,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"emotion":"","emotion_color":"","title_style":"","license":"","footnotes":""},"categories":[30],"tags":[],"class_list":["post-4079","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-30"],"post_thumbnail_image":"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2024\/05\/20240524145010907-150x132.jpeg","content_first_image":null,"post_medium_image_300":"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2024\/05\/20240524145010907-300x127.jpeg","post_thumbnail_image_624":"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2024\/05\/20240524145010907.jpeg","post_frist_image":null,"post_medium_image":"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2024\/05\/20240524145010907-300x127.jpeg","post_large_image":"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2024\/05\/20240524145010907.jpeg","post_full_image":"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2024\/05\/20240524145010907.jpeg","post_all_images":[],"videoAdId":"","listAd":"0","listAdId":"","listAdEvery":6,"total_comments":0,"category_name":"\u9762\u8bd5","post_date":"2024-04-24","like_count":"0","praiseWord":"\u9f13\u52b1","copyright_state":"","excitationAd":"0","rewardedVideoAdId":"","detailAdId":"","detailAd":"0","enterpriseMinapp":"0","audios":[],"postImageUrl":"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2023\/05\/20230519082947553.jpg","avatarurls":[],"related_posts":null,"pageviews":265,"next_post_id":4559,"next_post_title":"\u9762\u8bd5\u5b98\uff1a\u4f60\u4e86\u89e3Redis\u7684\u8fc7\u671f\u5220\u9664\u7b56\u7565\u5417\uff1fRedis\u6709\u54ea\u4e9b\u5185\u5b58\u6dd8\u6c70\u7b56\u7565\uff1f","previous_post_id":4083,"previous_post_title":"\u963f\u91cc\u9762\u8bd5\u9898\uff1aJava\u4e2d\u5982\u4f55\u63d0\u9ad8\u6587\u4ef6\u7684\u8bfb\u5199\u901f\u5ea6\uff1f","_links":{"self":[{"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/posts\/4079","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/comments?post=4079"}],"version-history":[{"count":0,"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/posts\/4079\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/media\/4076"}],"wp:attachment":[{"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/media?parent=4079"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/categories?post=4079"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/tags?post=4079"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}