{"id":2189,"date":"2021-11-28T19:39:13","date_gmt":"2021-11-28T11:39:13","guid":{"rendered":"https:\/\/coderbee.net\/?p=2189"},"modified":"2021-11-28T19:48:24","modified_gmt":"2021-11-28T11:48:24","slug":"%e5%8f%88%e8%b8%a9%e5%9d%91%e4%ba%86%e3%80%82threadpoolexecutor%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/coderbee.net\/index.php\/java\/20211128\/2189","title":{"rendered":"\u53c8\u8e29\u5751\u4e86\u3002ThreadPoolExecutor\uff1f"},"content":{"rendered":"<p>\u95ee\u9898\u662f\u51fa\u73b0\u5728 24 \u53f7\u7684\u65f6\u5019\uff0c\u5f53\u65f6\u6709\u53f0 weblogic \u5b9e\u4f8b\u51fa\u73b0\u963b\u585e\uff0c\u8fd0\u7ef4 dump \u7ebf\u7a0b\u6808\u540e\u91cd\u542f\u4e86\uff0c\u6709\u4e2a\u540c\u4e8b\u8fdb\u884c\u5206\u6790\u3002<\/p>\n<p>\u8be5\u540c\u4e8b\u5206\u6790\u7ebf\u7a0b\u6808\u540e\u8ba4\u4e3a\u95ee\u9898\u51fa\u5728\u4e00\u4e2a\u88ab\u5916\u90e8\u7cfb\u7edf\u8c03\u7528\u7684\u63a5\u53e3\uff0c\u8fd9\u4e2a\u63a5\u53e3\u6536\u5230\u8bf7\u6c42\u540e\u4f1a\u4ece\u6570\u636e\u5e93\u67e5\u8be2\u6570\u636e\uff0c\u7136\u540e\u628a\u6570\u636e\u5904\u7406\u540e\u53d1\u63d0\u4ea4\u5230\u7ebf\u7a0b\u6c60\uff0c\u518d\u7531\u7ebf\u7a0b\u6c60\u5f02\u6b65\u53d1\u9001\u5230 MQ \u670d\u52a1\u5668\uff0c\u8c03\u7528\u65b9\u76d1\u542c MQ \u8fdb\u884c\u6570\u636e\u5904\u7406\uff0c\u63a5\u53e3\u4ee3\u7801\u5927\u81f4\u5982\u4e0b\uff1a<\/p>\n<p><!--more--><\/p>\n<pre><code class=\"java\">@RequestMapping(\"publish\")\npublic void publish() {\n    String msg = RandomStringUtils.randomAlphabetic(32);\n    log.info(\"before submit task :\" + msg);\n    ThreadPoolUtil.getPoolExecutor().execute(() -&gt; {\n        log.info(\"publish msq to mq done:\" + msg);\n    });\n    log.info(\"submit task done:\" + msg);\n}\n\npublic class ThreadPoolUtil {\n    private static ThreadPoolExecutor pool = new ThreadPoolExecutor(10, 30,\n            20, TimeUnit.SECONDS, new ArrayBlockingQueue&lt;&gt;(5),\n            new CustomThreadFactory(), new CustomRejectHandler());\n\n    private static AtomicInteger counter = new AtomicInteger(0);\n\n    public static ThreadPoolExecutor getPoolExecutor() {\n        return pool;\n    }\n\n    public static void shutdown() {\n        pool.shutdown();\n    }\n\n    static class CustomThreadFactory implements ThreadFactory {\n        public Thread newThread(Runnable r) {\n            Thread thread = new Thread(r);\n            thread.setName(ThreadPoolUtil.class.getName() + counter.incrementAndGet());\n            return thread;\n        }\n    }\n\n    static class CustomRejectHandler implements RejectedExecutionHandler {\n        public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {\n            try {\n                pool.getQueue().put(r);\n            } catch (InterruptedException e) {\n                e.printStackTrace();\n            }\n        }\n    }\n}\n<\/code><\/pre>\n<p>\u90a3\u540c\u4e8b\u95ee\u4e86\u53d1\u8d77\u8c03\u7528\u7684\u7cfb\u7edf\u7684\u4eba\uff0c\u5bf9\u65b9\u6bcf\u6b21\u53d6 300 \u4e2a\u4efb\u52a1\u6765\u53d1\u8d77\u8c03\u7528\uff0c\u4ed6\u89c9\u5f97\u662f\u5bf9\u65b9\u8c03\u7528\u7684\u5e76\u53d1\u592a\u5927\u5bfc\u81f4\u7684\u3002<\/p>\n<p>\u867d\u7136\u9694\u5f97\u6709\u70b9\u8fdc\uff0c\u6211\u542c\u5230\u540e\u89c9\u5f97\u6709\u7591\u60d1\uff0c\u8c03\u7528\u65b9\u6bcf\u6b21\u53d6300\u4e2a\u4efb\u52a1\u66f4\u53ef\u80fd\u662f\u4e32\u884c\u8c03\u7528\u3001\u800c\u4e0d\u662f300\u4e2a\u5e76\u53d1\u3002<\/p>\n<p>\u6211\u627e\u4ed6\u8981\u4e86\u7ebf\u7a0b\u6808\u6587\u4ef6\u8fc7\u6765\uff0c\u53d1\u73b0\u5927\u591a\u6570\u5de5\u4f5c\u7ebf\u7a0b\u90fd\u662f\u7a7a\u95f2\uff0c\u963b\u585e\u7684\u7ebf\u7a0b\u7684\u7ebf\u7a0b\u6808\u4e0e\u4e0b\u9762\u7684\u76f8\u4f3c\uff1a<\/p>\n<pre><code>\"http-nio-8001-exec-10\" #36 daemon prio=5 os_prio=0 tid=0x0000000026b8c800 nid=0x2f64 waiting on condition [0x000000002778c000]\n   java.lang.Thread.State: WAITING (parking)\n        at sun.misc.Unsafe.park(Native Method)\n        - parking to wait for  &lt;0x00000000837cd8b8&gt; (a java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject)\n        at java.util.concurrent.locks.LockSupport.park(LockSupport.java:175)\n        at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(AbstractQueuedSynchronizer.java:2039)\n        at java.util.concurrent.ArrayBlockingQueue.put(ArrayBlockingQueue.java:353)\n        at net.coderbee.cloud.controller.ThreadPoolUtil$CustomRejectHandler.rejectedExecution(ThreadPoolUtil.java:34)\n        at java.util.concurrent.ThreadPoolExecutor.reject(ThreadPoolExecutor.java:823)\n        at java.util.concurrent.ThreadPoolExecutor.execute(ThreadPoolExecutor.java:1369)\n        at net.coderbee.cloud.controller.PublishController.publish(PublishController.java:22)\n<\/code><\/pre>\n<p>\u770b\u4e86\u53c8\u770b\u4e4b\u540e\uff0c\u7ed3\u5408\u7ebf\u7a0b\u6808\u548c\u6e90\u7801\uff0c\u6211\u89c9\u5f97\u8fd9\u4e2a\u63a5\u53e3\u53ea\u662f\u4e00\u4e2a\u53d7\u5bb3\u8005\uff0c\u6839\u672c\u7684\u95ee\u9898\u4e0d\u662f\u8fd9\u4e2a\u63a5\u53e3\uff0c\u800c\u5728\u4e8e\u7ebf\u7a0b\u6c60\uff0c\u7ebf\u7a0b\u6c60\u4e3a\u4ec0\u4e48\u4e0d\u518d\u7ee7\u7eed\u5904\u7406\u63d0\u4ea4\u7684\u4efb\u52a1\u5462\u3002<\/p>\n<p>\u6211\u4e00\u5f00\u59cb\u662f\u6000\u7591\u662f\u4e0d\u662f\u8e29\u4e86 ThreadPoolExecutor \u7684 bug\uff08\u6ed1\u7a3d\u8138\uff0c\u592a\u81a8\u80c0\u4e86\uff0c\u7adf\u7136\u6000\u7591 JUC \u7684 bug\uff09\uff0c\u641c\u4e86\u4e0b\u5e76\u6ca1\u6709\u4ec0\u4e48\u542f\u53d1\u3002<\/p>\n<p>\u56e0\u4e3a\u7ebf\u7a0b\u662f\u88ab\u963b\u585e\u4e86\uff0c weblogic \u91cc\u7ebf\u7a0b\u7684\u72b6\u6001\u662f stuck\uff0c\u518d\u62ff &#8220;ThreadPoolExecutor  stuck&#8221; \u53bb\u641c\uff0c\u5728 stackoverflow \u770b\u5230\u8fd9\u4e2a <a href=\"https:\/\/stackoverflow.com\/questions\/3059222\/java-threadpoolexecutor-getting-stuck-while-using-arrayblockingqueue\">Java ThreadPoolExecutor getting stuck while using ArrayBlockingQueue<\/a> \u3002<\/p>\n<p>\u56de\u7b54\u91cc\u63d0\u5230\u7684\u8c03\u7528 shutdown \u65b9\u6cd5\u6572\u9192\u4e86\u6211\uff0c\u8fd9\u4e2a\u7ebf\u7a0b\u6c60\u679c\u7136\u5728\u4e00\u4e2a\u5b9a\u65f6\u4efb\u52a1\u8c03\u7528\u7684\u5730\u65b9\u88ab\u8c03\u7528\uff1aThreadPoolUtil.getPoolExecutor()\uff0c\u7136\u540e\u8c03\u7528\u4e86 shutdown \u65b9\u6cd5\uff08\u5c31\u5728\u4e0a\u4e00\u4e2a\u7248\u672c\u5f15\u5165\u7684\u4ee3\u7801\uff09\u3002<\/p>\n<p>\u7ebf\u7a0b\u6c60\u88ab shuadown \u540e\uff0c\u63d0\u4ea4\u4e0a\u6765\u7684\u4efb\u52a1\u88ab\u6267\u884c reject \uff0c\u7531\u4e8e RejectHandler \u662f\u5b9a\u5236\u7684\uff0c\u628a\u63d0\u4ea4\u4efb\u52a1\u7684\u7ebf\u7a0b\u963b\u585e\u4f4f\u4e86\u3002<\/p>\n<p>\u5230\u6b64\u95ee\u9898\u7b97\u662f\u641e\u6e05\u695a\u4e86\uff0c\u6709\u5982\u4e0b\u53cd\u601d\uff1a<br \/>\n1\u3001 \u53ea\u8981\u65b9\u6cd5\u3001\u5bf9\u8c61\u66b4\u9732\u51fa\u6765\u4e86\uff0c\u5c31\u53ef\u80fd\u4f1a\u88ab\u8c03\u7528\uff0c\u50cf\u8fd9\u79cd\u5168\u5c40\u7684\u7ebf\u7a0b\u6c60\u5c31\u4e0d\u5e94\u66b4\u9732\u51fa\u6765\u3002<br \/>\n2\u3001 \u5b9a\u4f4d\u95ee\u9898\uff0c\u6700\u597d\u662f\u6709\u8bc1\u636e\u7684\uff0c\u4e0d\u8981\u53ea\u662f\u201c\u5e94\u8be5\u5c31\u662f\u8fd9\u4e2a\u201d\u3002<br \/>\n3\u3001 \u78b0\u5230\u6ca1\u4ec0\u4e48\u601d\u8def\u7684\u95ee\u9898\uff0c\u591a\u68c0\u67e5\u4e0b\u6700\u8fd1\u7248\u672c\u7684\u4ee3\u7801\uff0c\u95ee\u9898\u5f88\u591a\u90fd\u662f\u5728\u6700\u8fd1\u7248\u672c\u5f15\u8fdb\u7684\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u95ee\u9898\u662f\u51fa\u73b0\u5728 24 \u53f7\u7684\u65f6\u5019\uff0c\u5f53\u65f6\u6709\u53f0 weblogic \u5b9e\u4f8b\u51fa\u73b0\u963b\u585e\uff0c\u8fd0\u7ef4 d &hellip; <a href=\"https:\/\/coderbee.net\/index.php\/java\/20211128\/2189\">\u7ee7\u7eed\u9605\u8bfb <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[18],"tags":[358],"_links":{"self":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/2189"}],"collection":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/comments?post=2189"}],"version-history":[{"count":3,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/2189\/revisions"}],"predecessor-version":[{"id":2192,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/2189\/revisions\/2192"}],"wp:attachment":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/media?parent=2189"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/categories?post=2189"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/tags?post=2189"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}