{"id":993,"date":"2021-12-19T19:41:38","date_gmt":"2021-12-19T11:41:38","guid":{"rendered":"https:\/\/blog.cauchyschwarz.com\/?p=993"},"modified":"2021-12-19T19:41:41","modified_gmt":"2021-12-19T11:41:41","slug":"java%e5%a4%9a%e7%ba%bf%e7%a8%8b","status":"publish","type":"post","link":"https:\/\/blog.cauchyschwarz.com\/?p=993","title":{"rendered":"Java\u591a\u7ebf\u7a0b"},"content":{"rendered":"\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_82_2 ez-toc-wrap-right counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<label for=\"ez-toc-cssicon-toggle-item-69e0abe804bfa\" class=\"ez-toc-cssicon-toggle-label\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/label><input type=\"checkbox\"  id=\"ez-toc-cssicon-toggle-item-69e0abe804bfa\" checked aria-label=\"Toggle\" \/><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/blog.cauchyschwarz.com\/?p=993\/#Java%E4%B8%AD%E5%88%9B%E5%BB%BA%E7%BA%BF%E7%A8%8B%E7%9A%84%E4%B8%A4%E7%A7%8D%E6%96%B9%E5%BC%8F\" >Java\u4e2d\u521b\u5efa\u7ebf\u7a0b\u7684\u4e24\u79cd\u65b9\u5f0f<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/blog.cauchyschwarz.com\/?p=993\/#%E7%BA%BF%E7%A8%8B%E4%BC%98%E5%85%88%E7%BA%A7\" >\u7ebf\u7a0b\u4f18\u5148\u7ea7<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/blog.cauchyschwarz.com\/?p=993\/#%E7%BA%BF%E7%A8%8Byield\" >\u7ebf\u7a0byield()<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/blog.cauchyschwarz.com\/?p=993\/#Daemon_Threads\" >Daemon Threads<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/blog.cauchyschwarz.com\/?p=993\/#%E7%AD%89%E5%BE%85%E7%BA%BF%E7%A8%8B%E7%BB%93%E6%9D%9F\" >\u7b49\u5f85\u7ebf\u7a0b\u7ed3\u675f<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/blog.cauchyschwarz.com\/?p=993\/#%E7%BA%BF%E7%A8%8B%E4%B8%AD%E6%96%ADinterrupt\" >\u7ebf\u7a0b\u4e2d\u65ad(interrupt)<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/blog.cauchyschwarz.com\/?p=993\/#%E9%81%BF%E5%85%8D%E5%A4%9A%E4%B8%AA%E7%BA%BF%E7%A8%8B%E5%90%8C%E6%97%B6%E6%89%A7%E8%A1%8C%E4%B8%80%E4%B8%AA%E6%96%B9%E6%B3%95\" >\u907f\u514d\u591a\u4e2a\u7ebf\u7a0b\u540c\u65f6\u6267\u884c\u4e00\u4e2a\u65b9\u6cd5<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/blog.cauchyschwarz.com\/?p=993\/#%E9%9A%90%E5%BC%8F%E9%94%81Intrinsic_locks%E4%B8%8Esynchronized%E5%85%B3%E9%94%AE%E5%AD%97\" >\u9690\u5f0f\u9501(Intrinsic locks)\u4e0esynchronized\u5173\u952e\u5b57<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/blog.cauchyschwarz.com\/?p=993\/#%E6%AD%BB%E9%94%81\" >\u6b7b\u9501<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/blog.cauchyschwarz.com\/?p=993\/#waitnotifynotifyAll\" >wait\/notify\/notifyAll<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-11\" href=\"https:\/\/blog.cauchyschwarz.com\/?p=993\/#%E9%A5%A5%E9%A5%BF%E4%B8%8E%E5%85%AC%E5%B9%B3%E6%80%A7\" >\u9965\u997f\u4e0e\u516c\u5e73\u6027<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-12\" href=\"https:\/\/blog.cauchyschwarz.com\/?p=993\/#%E6%B4%BB%E9%94%81\" >\u6d3b\u9501<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-13\" href=\"https:\/\/blog.cauchyschwarz.com\/?p=993\/#Java%E7%BA%BF%E7%A8%8B%E7%9A%84%E7%8A%B6%E6%80%81\" >Java\u7ebf\u7a0b\u7684\u72b6\u6001<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-14\" href=\"https:\/\/blog.cauchyschwarz.com\/?p=993\/#happens-before_relation\" >happens-before relation<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-15\" href=\"https:\/\/blog.cauchyschwarz.com\/?p=993\/#Java_Memory_Model\" >Java Memory Model<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-16\" href=\"https:\/\/blog.cauchyschwarz.com\/?p=993\/#%E4%BD%BF%E7%94%A8javautilTimer%E8%B0%83%E5%BA%A6%E5%AE%89%E6%8E%92%E4%BB%BB%E5%8A%A1\" >\u4f7f\u7528java.util.Timer\u8c03\u5ea6(\u5b89\u6392)\u4efb\u52a1<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-17\" href=\"https:\/\/blog.cauchyschwarz.com\/?p=993\/#ThreadLocal\" >ThreadLocal<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-18\" href=\"https:\/\/blog.cauchyschwarz.com\/?p=993\/#JSR166_Concurrency_Utilities\" >JSR166:Concurrency Utilities<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-19\" href=\"https:\/\/blog.cauchyschwarz.com\/?p=993\/#Executor%E3%80%81Trhead_Pool%E5%92%8CForkJoin_Pool\" >Executor\u3001Trhead Pool\u548cForkJoin Pool<\/a><ul class='ez-toc-list-level-2' ><li class='ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-20\" href=\"https:\/\/blog.cauchyschwarz.com\/?p=993\/#Executor_Framework\" >Executor Framework<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-21\" href=\"https:\/\/blog.cauchyschwarz.com\/?p=993\/#Executor_interface\" >Executor interface<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-22\" href=\"https:\/\/blog.cauchyschwarz.com\/?p=993\/#ExecutorService_interface\" >ExecutorService interface<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-23\" href=\"https:\/\/blog.cauchyschwarz.com\/?p=993\/#Executors%E7%B1%BB\" >Executors\u7c7b<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-24\" href=\"https:\/\/blog.cauchyschwarz.com\/?p=993\/#Thread_Pool\" >Thread Pool<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-25\" href=\"https:\/\/blog.cauchyschwarz.com\/?p=993\/#%E5%85%B3%E4%BA%8E%E7%BA%BF%E7%A8%8B%E6%B1%A0%E7%9A%84%E4%B8%80%E4%BA%9B%E6%A6%82%E5%BF%B5\" >\u5173\u4e8e\u7ebf\u7a0b\u6c60\u7684\u4e00\u4e9b\u6982\u5ff5<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-26\" href=\"https:\/\/blog.cauchyschwarz.com\/?p=993\/#Java_Thread_Pool%E7%9A%84%E5%AE%9E%E7%8E%B0\" >Java Thread Pool\u7684\u5b9e\u73b0<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-27\" href=\"https:\/\/blog.cauchyschwarz.com\/?p=993\/#Scheduled_Thread_Pools\" >Scheduled Thread Pools<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-28\" href=\"https:\/\/blog.cauchyschwarz.com\/?p=993\/#ScheduledThreadPoolExecutor%E7%B1%BB\" >ScheduledThreadPoolExecutor\u7c7b<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-29\" href=\"https:\/\/blog.cauchyschwarz.com\/?p=993\/#Forkjoin_framework\" >Fork\/join framework<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-30\" href=\"https:\/\/blog.cauchyschwarz.com\/?p=993\/#%E5%87%A0%E7%A7%8D%E5%90%8C%E6%AD%A5%E6%9C%BA%E5%88%B6\" >\u51e0\u79cd\u540c\u6b65\u673a\u5236<\/a><\/li><\/ul><\/nav><\/div>\n<h1 class=\"wp-block-heading\" id=\"java%E4%B8%AD%E5%88%9B%E5%BB%BA%E7%BA%BF%E7%A8%8B%E7%9A%84%E4%B8%A4%E7%A7%8D%E6%96%B9%E5%BC%8F\"><span class=\"ez-toc-section\" id=\"Java%E4%B8%AD%E5%88%9B%E5%BB%BA%E7%BA%BF%E7%A8%8B%E7%9A%84%E4%B8%A4%E7%A7%8D%E6%96%B9%E5%BC%8F\"><\/span>Java\u4e2d\u521b\u5efa\u7ebf\u7a0b\u7684\u4e24\u79cd\u65b9\u5f0f<span class=\"ez-toc-section-end\"><\/span><\/h1>\n\n\n\n<ul class=\"wp-block-list\"><li>\u7ee7\u627fThread<\/li><li>\u5b9e\u73b0Runnable\u63a5\u53e3<\/li><\/ul>\n\n\n\n<p>Thread\u7c7b\u7684\u51e0\u4e2a\u65b9\u6cd5\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Thread#start()<\/li><li>Thread#currentThread()<\/li><li>Thread#sleep()<\/li><li>Thread#setName()<\/li><\/ul>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"%E7%BA%BF%E7%A8%8B%E4%BC%98%E5%85%88%E7%BA%A7\"><span class=\"ez-toc-section\" id=\"%E7%BA%BF%E7%A8%8B%E4%BC%98%E5%85%88%E7%BA%A7\"><\/span>\u7ebf\u7a0b\u4f18\u5148\u7ea7<span class=\"ez-toc-section-end\"><\/span><\/h1>\n\n\n\n<p>Thread#setPriority()<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Thread.MIN_PRIORITY<\/li><li>Thread.NORM_PRIORITY<\/li><li>Thread.MAX_PRIORITY<\/li><\/ul>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"%E7%BA%BF%E7%A8%8Byield\"><span class=\"ez-toc-section\" id=\"%E7%BA%BF%E7%A8%8Byield\"><\/span>\u7ebf\u7a0byield()<span class=\"ez-toc-section-end\"><\/span><\/h1>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"daemon-threads\"><span class=\"ez-toc-section\" id=\"Daemon_Threads\"><\/span>Daemon Threads<span class=\"ez-toc-section-end\"><\/span><\/h1>\n\n\n\n<ul class=\"wp-block-list\"><li>JVM\u5728\u7ed3\u675f\u4e3b\u7ebf\u7a0b\u4e4b\u524d\u4f1a\u7b49\u5f85\u975e\u5b88\u62a4\u7ebf\u7a0b\uff0c\u4f46\u662f\u4e0d\u4f1a\u7b49\u5f85\u5b88\u62a4\u7ebf\u7a0b<\/li><li>\u7531\u5b88\u62a4\u7ebf\u7a0b\u521b\u5efa\u7684\u7ebf\u7a0b\u4ecd\u7136\u662f\u5b88\u62a4\u7ebf\u7a0b\uff0c\u7531\u975e\u5b88\u62a4\u7ebf\u7a0b\u521b\u5efa\u7684\u7ebf\u7a0b\u4ecd\u7136\u662f\u975e\u5b88\u62a4\u7ebf\u7a0b\u3002\u8c03\u7528main\u5165\u53e3\u65b9\u6cd5\u7684\u7ebf\u7a0b\u662f\u975e\u5b88\u62a4\u7ebf\u7a0b<\/li><li>\u53ef\u4ee5\u901a\u8fc7api<code>Thread#setDaemon(true\/false)<\/code>\u8bbe\u7f6e\u7ebf\u7a0b\u662f\u5426\u662f\u5b88\u62a4\u7ebf\u7a0b<\/li><li><code>System.exit(0)<\/code>\u4f1a\u76f4\u63a5\u7ed3\u675fJVM\u800c\u4e0d\u8bba\u662f\u5426\u8fd8\u6709\u5b88\u62a4\/\u975e\u5b88\u62a4\u7ebf\u7a0b\u5728\u8fd0\u884c<\/li><\/ul>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"%E7%AD%89%E5%BE%85%E7%BA%BF%E7%A8%8B%E7%BB%93%E6%9D%9F\"><span class=\"ez-toc-section\" id=\"%E7%AD%89%E5%BE%85%E7%BA%BF%E7%A8%8B%E7%BB%93%E6%9D%9F\"><\/span>\u7b49\u5f85\u7ebf\u7a0b\u7ed3\u675f<span class=\"ez-toc-section-end\"><\/span><\/h1>\n\n\n\n<ul class=\"wp-block-list\"><li>Thread#join()<\/li><li>Thread#join(long millis)<\/li><li>Thread#join(long millis, int nanos)<\/li><\/ul>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"%E7%BA%BF%E7%A8%8B%E4%B8%AD%E6%96%ADinterrupt\"><span class=\"ez-toc-section\" id=\"%E7%BA%BF%E7%A8%8B%E4%B8%AD%E6%96%ADinterrupt\"><\/span>\u7ebf\u7a0b\u4e2d\u65ad(interrupt)<span class=\"ez-toc-section-end\"><\/span><\/h1>\n\n\n\n<p>Java\u7ebf\u7a0b\u4e2d\u65ad\u7684\u4e24\u79cd\u60c5\u51b5\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>\u7ebf\u7a0b\u901a\u8fc7<code>Object.wait(..),Thread.join(..),Thread.sleep(..)<\/code>\u65b9\u6cd5\u5904\u4e8e\u963b\u585e\u72b6\u6001\u65f6\uff0c\u5982\u679c\u6709\u53e6\u5916\u7684\u7ebf\u7a0b\u8c03\u7528\u4e86\u6b64\u7ebf\u7a0b\u7684interrupt\u65b9\u6cd5\uff0c\u90a3\u4e48\u6b64\u7ebf\u7a0b\u5c31\u4f1a\u6536\u5230\u4e00\u4e2a<em>InterruptedException<\/em>\uff0c\u77e5\u9053\u81ea\u5df1\u4e0d\u8981\u518d\u5446\u5446\u5730\u963b\u585e\uff0c\u5f62\u52bf\u6709\u53d8\u5316\u4e86<\/li><li>\u53e6\u4e00\u79cd\u662f\u7ebf\u7a0b\u6b63\u5728\u6267\u884c\u81ea\u5df1\u7684\u4ee3\u7801\uff0c\u53e6\u5916\u6709\u7ebf\u7a0b\u8c03\u7528\u4e86\u8fd9\u4e2a\u7ebf\u7a0b\u7684interrupt\u65b9\u6cd5\uff0c\u8fd9\u4e2a\u65f6\u5019\u5982\u679c\u6b64\u7ebf\u7a0b\u4ee3\u7801\u5728\u4e2d\u65ad\u4e8b\u4ef6\u53d1\u751f\u540e\u6709\u8c03\u7528<code>isInterrupted()<\/code>\u65b9\u6cd5\u68c0\u67e5\u81ea\u5df1\u7684\u4e2d\u65ad\u72b6\u6001\uff0c\u90a3\u4e48\u8fd9\u4e2a\u7ebf\u7a0b\u5c31\u53ef\u4ee5\u77e5\u9053\u6709\u4e2d\u65ad\u4e8b\u4ef6\u53d1\u751f\uff0c\u5426\u5219\u8fd9\u4e2a\u4e2d\u65ad\u72b6\u6001\u5c31\u4e00\u76f4\u4fdd\u5b58\u5728\u90a3\u91cc\u7b49\u5f85\u8fd9\u4e2a\u7ebf\u7a0b\u7684\u53d1\u73b0<\/li><\/ul>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"%E9%81%BF%E5%85%8D%E5%A4%9A%E4%B8%AA%E7%BA%BF%E7%A8%8B%E5%90%8C%E6%97%B6%E6%89%A7%E8%A1%8C%E4%B8%80%E4%B8%AA%E6%96%B9%E6%B3%95\"><span class=\"ez-toc-section\" id=\"%E9%81%BF%E5%85%8D%E5%A4%9A%E4%B8%AA%E7%BA%BF%E7%A8%8B%E5%90%8C%E6%97%B6%E6%89%A7%E8%A1%8C%E4%B8%80%E4%B8%AA%E6%96%B9%E6%B3%95\"><\/span>\u907f\u514d\u591a\u4e2a\u7ebf\u7a0b\u540c\u65f6\u6267\u884c\u4e00\u4e2a\u65b9\u6cd5<span class=\"ez-toc-section-end\"><\/span><\/h1>\n\n\n\n<p><strong>synchronized<\/strong>\u5173\u952e\u5b57<\/p>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"%E9%9A%90%E5%BC%8F%E9%94%81intrinsic-locks%E4%B8%8Esynchronized%E5%85%B3%E9%94%AE%E5%AD%97\"><span class=\"ez-toc-section\" id=\"%E9%9A%90%E5%BC%8F%E9%94%81Intrinsic_locks%E4%B8%8Esynchronized%E5%85%B3%E9%94%AE%E5%AD%97\"><\/span>\u9690\u5f0f\u9501(Intrinsic locks)\u4e0esynchronized\u5173\u952e\u5b57<span class=\"ez-toc-section-end\"><\/span><\/h1>\n\n\n\n<p><strong>synchronized<\/strong>\u5173\u952e\u5b57\u7684\u5b9e\u73b0\u539f\u7406\u5176\u5b9e\u662f\u6bcf\u4e2a<strong>synchronized<\/strong>\u5173\u952e\u5b57\u5173\u8054\u4e86\u4e00\u4e2a\u5bf9\u8c61\uff0c\u800c\u8fd9\u4e9b\u5bf9\u8c61\u90fd\u6709\u4e00\u4e2a\u9690\u5f0f\u7684\u9501\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Intrinsic lock\u662f\u548c\u5bf9\u8c61\u5173\u8054\u5728\u4e00\u8d77\u7684\u800c\u4e0d\u662f\u65b9\u6cd5<br>\u5982\u679c\u51e0\u4e2asynchronized\u65b9\u6cd5\u5173\u8054\u7684\u662f\u540c\u4e00\u4e2a\u5bf9\u8c61\uff0c\u90a3\u4e48\u8fd9\u4e9b\u65b9\u6cd5\u5c31\u6709\u53ef\u80fd\u56e0\u4e3a\u4e00\u4e2a\u65b9\u6cd5\u5728\u88ab\u8c03\u7528\u800c\u963b\u585e\u3002<\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">public class MultipleSyncMethodsDemo {\n\n    public static void main (String[] args) throws InterruptedException {\n        MultipleSyncMethodsDemo demo = new MultipleSyncMethodsDemo();\n        Thread thread1 = new Thread(() -&gt; {\n            System.out.println(\"thread1 before call \"+ LocalDateTime.now());\n            demo.syncMethod1(\"from thread1\");\n            System.out.println(\"thread1 after call \"+LocalDateTime.now());\n        });\n        Thread thread2 = new Thread(() -&gt; {\n            System.out.println(\"thread2 before call \"+LocalDateTime.now());\n            demo.syncMethod2(\"from thread2\");\n            System.out.println(\"thread2 after call \"+LocalDateTime.now());\n        });\n\n        thread1.start();\n        thread2.start();\n    }\n\n    private synchronized void syncMethod1 (String msg) {\n        System.out.println(\"in the syncMethod1 \"+msg+\" \"+LocalDateTime.now());\n        try {\n            TimeUnit.SECONDS.sleep(5);\n        } catch (InterruptedException e) {\n            e.printStackTrace();\n        }\n    }\n\n    private synchronized void syncMethod2 (String msg) {\n        System.out.println(\"in the syncMethod2 \"+msg+\" \"+LocalDateTime.now());\n        try {\n            TimeUnit.SECONDS.sleep(5);\n        } catch (InterruptedException e) {\n            e.printStackTrace();\n        }\n    }\n}\n<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\"><li>Intrinsic lock\u662f\u53ef\u91cd\u5165\u9501<strong>Intrinsic lock are acquired on a per-thread basis rather than per-method call basis. Once a thread has acquired the lock it can internally call other methods without reacquiring the lock. The Lock will only be release when the thread is done with the entry method invocation.<\/strong><\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">public class ReentrantDemo {\n\n    public static void main (String[] args) throws InterruptedException {\n        ReentrantDemo demo = new ReentrantDemo();\n        Thread thread1 = new Thread(() -&gt; {\n            System.out.println(\"thread1 before call \"+ LocalDateTime.now());\n            demo.syncMethod1(\"from thread1\");\n            System.out.println(\"thread1 after call \"+LocalDateTime.now());\n        });\n        Thread thread2 = new Thread(() -&gt; {\n            System.out.println(\"thread2 before call \"+LocalDateTime.now());\n            demo.syncMethod2(\"from thread2\");\n            System.out.println(\"thread2 after call \"+LocalDateTime.now());\n        });\n\n        thread1.start();\n        thread2.start();\n    }\n\n    private synchronized void syncMethod1 (String msg) {\n        System.out.println(\"in the syncMethod1 \"+msg+\" \"+LocalDateTime.now());\n        syncMethod2(\"from method syncMethod1, reentered call\");\n    }\n\n    private synchronized void syncMethod2 (String msg) {\n        System.out.println(\"in the syncMethod2 \"+msg+\" \"+LocalDateTime.now());\n        try {\n            TimeUnit.SECONDS.sleep(3);\n        } catch (InterruptedException e) {\n            e.printStackTrace();\n        }\n    }\n}\n<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\"><li>\u5bf9\u4e8estatic\u65b9\u6cd5\u7684synchronized\u5173\u952e\u5b57\uff0c\u5173\u8054\u7684\u662f\u4ee3\u8868\u8fd9\u4e2a\u7c7b\u7684\u5bf9\u8c61\u800c\u4e0d\u662f\u4e00\u4e2a\u7c7b\u5b9e\u4f8b\u3002<\/li><li><strong>synchronized<\/strong>\u5173\u952e\u5b57\u4e5f\u53ef\u4ee5\u7528\u4e8e\u4ee3\u7801\u5757\uff0c\u53ea\u662f\u6b64\u65f6\u9700\u8981\u663e\u793a\u5730\u6307\u660e\u4e00\u4e2a\u975e\u7a7a\u7684\u7528\u4e8e\u540c\u6b65\u7684\u5bf9\u8c61\u3002\u8fd9\u79cd\u505a\u6cd5\u6709\u4e24\u4e2a\u597d\u5904\uff1a<ul><li>\u66f4\u7cbe\u7ec6\u5730\u63a7\u5236\u9700\u8981\u540c\u6b65\u7684\u4ee3\u7801\u5757\uff1b<\/li><li>\u53ef\u4ee5\u81ea\u5df1\u9009\u62e9\u4f7f\u7528\u54ea\u4e2a\u5bf9\u8c61\u7684intrinsic lock\u8fdb\u884c\u540c\u6b65<\/li><\/ul><\/li><li>Generally speaking we should always use objects locks which our code maintains instead of relying on JVM managed objects.<\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">public class SyncBlockStringLock {\n    private Map&lt;String, Object&gt; locks = new HashMap&lt;&gt;();\n\n    private static final File rootFolder = new File(\"d:\\\\test\");\n\n    static {\n        if (!rootFolder.exists()) {\n            rootFolder.mkdir();\n        }\n    }\n\n    public static void main (String[] args) {\n        SyncBlockStringLock obj = new SyncBlockStringLock();\n        Thread thread1 = new Thread(() -&gt; {\n            for (int i = 0; i &lt; 5; i++) {\n                String path = rootFolder.getAbsolutePath() + File.separatorChar + i;\n                obj.writeData(path, \" thread1 data \" + i);\n                obj.readData(path);\n            }\n        });\n\n        Thread thread2 = new Thread(() -&gt; {\n            for (int i = 0; i &lt; 5; i++) {\n                String path = rootFolder.getAbsolutePath() + File.separatorChar + i;\n                obj.writeData(path, \" thread2 data \" + i);\n                obj.readData(path);\n            }\n        });\n\n        thread1.start();\n        thread2.start();\n    }\n\n    private void writeData (String path, String data) {\n        synchronized (getLock(path)) {\n            try {\n                Files.write(Paths.get(path), data.getBytes());\n            } catch (IOException e) {\n                e.printStackTrace();\n            }\n        }\n    }\n\n    private void readData (String path) {\n        synchronized (getLock(path)) {\n            String s = null;\n            try {\n                s = new String(Files.readAllBytes(Paths.get(path)));\n            } catch (IOException e) {\n                e.printStackTrace();\n            }\n            System.out.println(s);\n        }\n    }\n\n    private Object getLock (String path) {\n        if (!locks.containsKey(path)) {\n            locks.put(path, new Object());\n        }\n\n        return locks.get(path);\n    }\n}\n<\/code><\/pre>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"%E6%AD%BB%E9%94%81\"><span class=\"ez-toc-section\" id=\"%E6%AD%BB%E9%94%81\"><\/span>\u6b7b\u9501<span class=\"ez-toc-section-end\"><\/span><\/h1>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"waitnotifynotifyall\"><span class=\"ez-toc-section\" id=\"waitnotifynotifyAll\"><\/span>wait\/notify\/notifyAll<span class=\"ez-toc-section-end\"><\/span><\/h1>\n\n\n\n<p>\u5bf9\u4e8eIntrinsic lock\uff0cwait\/notify\/notifyAll\u8fd9\u5199api\u662f\u4e0e\u4e4b\u76f8\u5173\u7684\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>\u8c03\u7528wait\/notify\/notifyAll\u65f6\u5fc5\u987b\u5df2\u7ecf\u83b7\u5f97\u4e86\u5bf9\u8c61\u7684Intrinsic lock\uff0c\u5426\u5219\u4f1a\u62a5\u9519\u3002\u6240\u4ee5\u8fd9\u4e9b\u64cd\u4f5c\u901a\u5e38\u90fd\u5728synchronized\u65b9\u6cd5\/\u4ee3\u7801\u5757\u4e2d\u88ab\u8c03\u7528<\/li><li>\u8c03\u7528wait\u540e\u7ebf\u7a0b\u4f1a\u81ea\u52a8\u91ca\u653e\u76f8\u5e94\u7684intrinsic lock<\/li><li>notify\u53ea\u5524\u9192\u4e00\u4e2a\u963b\u585e\u7684\u7ebf\u7a0b\uff0cnotifyAll\u662f\u5524\u9192\u6240\u6709\u963b\u585e\u7684\u7ebf\u7a0b<\/li><\/ul>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"%E9%A5%A5%E9%A5%BF%E4%B8%8E%E5%85%AC%E5%B9%B3%E6%80%A7\"><span class=\"ez-toc-section\" id=\"%E9%A5%A5%E9%A5%BF%E4%B8%8E%E5%85%AC%E5%B9%B3%E6%80%A7\"><\/span>\u9965\u997f\u4e0e\u516c\u5e73\u6027<span class=\"ez-toc-section-end\"><\/span><\/h1>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"%E6%B4%BB%E9%94%81\"><span class=\"ez-toc-section\" id=\"%E6%B4%BB%E9%94%81\"><\/span>\u6d3b\u9501<span class=\"ez-toc-section-end\"><\/span><\/h1>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>A livelock is a recursive situation where two or more threads would keep repeating a particular code logic. The intended logic is typically giving opportunity to the other threads to proceed in favor of &#8216;this&#8217; thread.<\/p><\/blockquote>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>A real-world example of livelock occurs when two people meet in a narrow corridor, and each tries to be polite by moving aside to let the other pass, but they end up swaying from side to side without making any progress because they both repeatedly move the same way at the same time.<\/p><\/blockquote>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"java%E7%BA%BF%E7%A8%8B%E7%9A%84%E7%8A%B6%E6%80%81\"><span class=\"ez-toc-section\" id=\"Java%E7%BA%BF%E7%A8%8B%E7%9A%84%E7%8A%B6%E6%80%81\"><\/span>Java\u7ebf\u7a0b\u7684\u72b6\u6001<span class=\"ez-toc-section-end\"><\/span><\/h1>\n\n\n\n<p><code>public Thread.State getState()<\/code><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><em>NEW<\/em><br>Thread has not yet started<\/li><li><em>RUNNABLE<\/em><br>Thread is currently running without blocking\/waiting in its run method<\/li><li><em>BLOCKED<\/em><br>Thread is blocked from entering a synchronized block\/method, waiting for the monitor lock to be released by the other thread<\/li><li><em>WAITING<\/em><br>Thread is waiting due to one of these calls\uff1a<code>Object.wait(),Thread.join(),LockSupport.park()<\/code><\/li><li><em>TIMED_WAITING<\/em><br>Thread is waiting due to one of these timeout based method calls\uff1a<code>Thread.sleep(long millis), Object.wait(long millis), Thread.join(long millis), LockSupport.parkNanos(Object blocker, long nanos), LockSupport.parkUntil(Object blocker, long nanos)<\/code><\/li><li><em>TERMINATED<\/em><br>A thread has exited from its run() method<\/li><\/ul>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"happens-before-relation\"><span class=\"ez-toc-section\" id=\"happens-before_relation\"><\/span>happens-before relation<span class=\"ez-toc-section-end\"><\/span><\/h1>\n\n\n\n<p>\u8981\u60f3\u591a\u7ebf\u7a0b\u7684Java\u80fd\u591f\u6b63\u5e38\u5de5\u4f5c\uff0c\u786e\u5b9a<em>happens-before<\/em>\u5173\u7cfb\u662f\u975e\u5e38\u91cd\u8981\u7684\uff1a<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p><strong>Happens-before relationship is a guarantee that action performed by one thread is visible to another action in different thread.<\/strong><br>Happens-before defines a partial ordering on all actions within the program. To guarantee that the thread executing action Y can see the results of action X (whether or not X and Y occur in different threads), there must be a happens-before relationship between X and Y. In the absence of a happens-before ordering between two operations, the JVM is free to reorder them as it wants (JIT compiler optimization).<\/p><\/blockquote>\n\n\n\n<p>\u8fd9\u4e2ahappens-before\u4e0d\u4ec5\u610f\u5473\u7740\u65f6\u95f4\u4e0a\u7684\u5148\u540e\uff0c\u66f4\u662f\u610f\u5473\u7740\u540e\u4e00\u4e2a\u884c\u4e3a\u662f\u5728\u524d\u4e00\u4e2a\u884c\u4e3a\u7684\u7ed3\u679c\u57fa\u7840\u4e0a(\u6bd4\u5982\u5185\u5b58\u4fee\u6539)\u8fdb\u884c\u7684\u3002<\/p>\n\n\n\n<p><strong>\u5efa\u7acbhappens-before\u5173\u7cfb\u7684\u51e0\u79cd\u65b9\u6cd5<\/strong>\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li><strong>Single thread rule<\/strong>\uff1aEach action in a single thread happens-before every action in that thread that comes later in the program order.<br><img loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"655\" class=\"wp-image-994\" style=\"width: 750px;\" src=\"https:\/\/blog.cauchyschwarz.com\/wp-content\/uploads\/2021\/12\/java_multithread_single-thread-rule.png\" alt=\"\" srcset=\"https:\/\/blog.cauchyschwarz.com\/wp-content\/uploads\/2021\/12\/java_multithread_single-thread-rule.png 333w, https:\/\/blog.cauchyschwarz.com\/wp-content\/uploads\/2021\/12\/java_multithread_single-thread-rule-300x262.png 300w\" sizes=\"auto, (max-width: 750px) 100vw, 750px\" \/><\/li><li><strong>Monitor lock rule<\/strong>\uff1aAn unlock on a monitor lock (exiting synchronized method\/block) happens-before every subsequent acquiring on the same monitor lock.<br><img loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"427\" class=\"wp-image-995\" style=\"width: 750px;\" src=\"https:\/\/blog.cauchyschwarz.com\/wp-content\/uploads\/2021\/12\/java_multithread_monitor-lock-rule.png\" alt=\"\" srcset=\"https:\/\/blog.cauchyschwarz.com\/wp-content\/uploads\/2021\/12\/java_multithread_monitor-lock-rule.png 624w, https:\/\/blog.cauchyschwarz.com\/wp-content\/uploads\/2021\/12\/java_multithread_monitor-lock-rule-300x171.png 300w\" sizes=\"auto, (max-width: 750px) 100vw, 750px\" \/><\/li><li><strong>Volatile variable rule<\/strong>\uff1aA write to a volatile field happens-before every subsequent read of that same field. Writes and reads of volatile fields have similar memory consistency effects as entering and exiting monitors (synchronized block around reads and writes), but without actually aquiring monitors\/locks.<br><img loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"380\" class=\"wp-image-996\" style=\"width: 750px;\" src=\"https:\/\/blog.cauchyschwarz.com\/wp-content\/uploads\/2021\/12\/java_multithread_volatile-variable-rule.png\" alt=\"\" srcset=\"https:\/\/blog.cauchyschwarz.com\/wp-content\/uploads\/2021\/12\/java_multithread_volatile-variable-rule.png 731w, https:\/\/blog.cauchyschwarz.com\/wp-content\/uploads\/2021\/12\/java_multithread_volatile-variable-rule-300x152.png 300w, https:\/\/blog.cauchyschwarz.com\/wp-content\/uploads\/2021\/12\/java_multithread_volatile-variable-rule-670x339.png 670w\" sizes=\"auto, (max-width: 750px) 100vw, 750px\" \/><\/li><li><strong>Thread start rule<\/strong>\uff1aA call to Thread.start() on a thread happens-before every action in the started thread. Say thread A spawns a new thread B by calling threadA.start(). All actions performed in thread B&#8217;s run method will see thread A&#8217;s calling threadA.start() method and before that (only in thread A) happened before them.<br><img loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"401\" class=\"wp-image-997\" style=\"width: 750px;\" src=\"https:\/\/blog.cauchyschwarz.com\/wp-content\/uploads\/2021\/12\/java_multithread_thread-start-rule.png\" alt=\"\" srcset=\"https:\/\/blog.cauchyschwarz.com\/wp-content\/uploads\/2021\/12\/java_multithread_thread-start-rule.png 608w, https:\/\/blog.cauchyschwarz.com\/wp-content\/uploads\/2021\/12\/java_multithread_thread-start-rule-300x160.png 300w\" sizes=\"auto, (max-width: 750px) 100vw, 750px\" \/><\/li><li><strong>Thread join rule<\/strong>\uff1aAll actions in a thread happen-before any other thread successfully returns from a join on that thread. Say thread A spawns a new thread B by calling threadA.start() then calls threadA.join(). Thread A will wait at join() call until thread B&#8217;s run method finishes. After join method returns, all subsequent actions in thread A will see all actions performed in thread B&#8217;s run method happened before them.<br><img loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"376\" class=\"wp-image-998\" style=\"width: 750px;\" src=\"https:\/\/blog.cauchyschwarz.com\/wp-content\/uploads\/2021\/12\/java_multithread_thread-join-rule.png\" alt=\"\" srcset=\"https:\/\/blog.cauchyschwarz.com\/wp-content\/uploads\/2021\/12\/java_multithread_thread-join-rule.png 619w, https:\/\/blog.cauchyschwarz.com\/wp-content\/uploads\/2021\/12\/java_multithread_thread-join-rule-300x150.png 300w\" sizes=\"auto, (max-width: 750px) 100vw, 750px\" \/><\/li><li><strong>Transitivity<\/strong>\uff1aIf A happens-before B, and B happens-before C, then A happens-before C.<\/li><\/ol>\n\n\n\n<p>\u8bb0\u4f4f\u8fd9\u51e0\u6761\u89c4\u5219\u7684\u65b9\u6cd5\uff1a\u9996\u5148\u5148\u6765\u540e\u5230\u548c\u4f20\u9012\u6027\u662f\u4e24\u4e2a\u5f88\u81ea\u7136\u7684\u89c4\u5219\uff0c\u5176\u6b21\u5bf9\u4e8e\u7ebf\u7a0b\u6765\u8bf4\u7ebf\u7a0b\u5f00\u59cb\u7684\u64cd\u4f5c\u80af\u5b9a\u5728\u7ebf\u7a0b\u7684run()\u65b9\u6cd5\u4e4b\u524d\uff0c\u7ebf\u7a0b\u7684run()\u65b9\u6cd5\u80af\u5b9a\u5728\u53e6\u5916\u7684\u7ebf\u7a0bjoin()\u8fd9\u4e2a\u7ebf\u7a0b\u4e4b\u524d\uff0c\u6700\u540eJava\u4e3a\u4e86\u8ba9\u5f00\u53d1\u8005\u80fd\u591f\u65b9\u4fbf\u5b9e\u73b0\u591a\u7ebf\u7a0b\u7a0b\u5e8f\u7684\u540c\u6b65\uff0c\u5f15\u5165\u4e86\u9501\u548cvolatile\u3002\u4e8e\u662f\u8fd96\u4e2a\u89c4\u5219\u5c31\u8fd9\u6837\u8bb0\u4f4f\u4e86\u3002<\/p>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"java-memory-model\"><span class=\"ez-toc-section\" id=\"Java_Memory_Model\"><\/span>Java Memory Model<span class=\"ez-toc-section-end\"><\/span><\/h1>\n\n\n\n<p>\u7531\u4e8e\u771f\u5b9e\u786c\u4ef6\u7684\u5185\u5b58\u7ed3\u6784\u3001\u5904\u7406\u5668\u4f18\u5316\u548cJIT\u7f16\u8bd1\u5668\u7684\u4f18\u5316\u5b9e\u5728\u592a\u590d\u6742\uff0cJava Memory Model(JSR-133)\u88ab\u8bbe\u8ba1\u51fa\u6765\u9690\u85cf\u8fd9\u4e9b\u771f\u5b9e\u60c5\u51b5\u7684\u7ec6\u8282\uff0cJava\u5f00\u53d1\u8005\u4eec\u53ea\u9700\u8981\u5728\u8fd9\u4e00\u5c42\u62bd\u8c61\u4e0a\u8fdb\u884c\u601d\u8003\u5373\u53ef\u3002<\/p>\n\n\n\n<p>\u9700\u8981\u4e00\u4e2a<em>Java Memory Model<\/em>\u7684\u7406\u7531\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Variable Visibility Problem<br>\u5728\u591a\u6838\/\u591a\u5904\u7406\u5668\u7684\u786c\u4ef6\u73af\u5883\u4e0b\uff0c\u5185\u5b58\u5f80\u5f80\u662f\u5177\u6709\u591a\u5c42\u6b21\u7f13\u5b58\u7ed3\u6784\u7684\u3002\u4e3a\u4e86\u6027\u80fd\u4e0a\u7684\u4f18\u5316\uff0c\u5bf9\u4e8e\u4e00\u4e2a\u5171\u4eab\u53d8\u91cf\uff0c\u4e00\u4e2a\u6838\/\u5904\u7406\u5668\u5f80\u5f80\u8bfb\u5230\/\u5199\u5230\u7684\u662f\u76f8\u5e94\u7684\u7f13\u5b58\u3002\u5982\u679c\u663e\u793a\u6dfb\u52a0\u6307\u4ee4\u66f4\u65b0\/\u540c\u6b65\u8fd9\u4e2a\u5171\u4eab\u53d8\u91cf\uff0c\u90a3\u4e48\u4e00\u4e2a\u6838\/\u5904\u7406\u5668\u5bf9\u8fd9\u4e2a\u53d8\u91cf\u503c\u505a\u7684\u4fee\u6539\u5176\u4ed6\u6838\/\u5904\u7406\u5668\u53ef\u80fd\u5c31\u4e0d\u80fd\u53ca\u65f6\u7684\u770b\u5230\u3002<\/li><li>Code reordering Problem<br>Java\u5141\u8bb8Javac\u7f16\u8bd1\u5668\u6216\u8005\u5373\u65f6\u7f16\u8bd1\u7f16\u8bd1\u5bf9\u4ee3\u7801\u505a\u4f18\u5316\uff0c\u8fd9\u4e9b\u4f18\u5316\u53ef\u80fd\u4f1a\u91cd\u65b0\u6392\u5217\u5f00\u53d1\u8005\u7684\u4ee3\u7801\u6267\u884c\u987a\u5e8f\uff1b\u800c\u5185\u5b58\u5c42\u6b21\u7ed3\u6784\u7684\u5b58\u5728\u4e5f\u53ef\u80fd\u4f7f\u5f97\u4ee3\u7801\u770b\u8d77\u6765\u50cf\u91cd\u65b0\u6392\u5217\u8fc7\u4e00\u6837\u3002\u5bf9\u4e8e\u5355\u7ebf\u7a0b\u7a0b\u5e8f\u6765\u8bb2\u8fd9\u79cd\u91cd\u65b0\u6392\u5217\u4ee3\u7801\u7684\u4f18\u5316\u6ca1\u6709\u95ee\u9898\uff0c\u4f46\u662f\u5bf9\u4e8e\u591a\u7ebf\u7a0b\u7a0b\u5e8f\u5374\u53ef\u80fd\u4f7f\u7a0b\u5e8f\u4ea7\u751f\u5f02\u5e38\u884c\u4e3a\u3002<\/li><li>Sequential Consistency<\/li><\/ul>\n\n\n\n<p>\u5728<em>Java Memory Model<\/em>\u4e4b\u4e0a\u6b63\u786e\u5b9e\u73b0\u591a\u7ebf\u7a0b\u540c\u6b65\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>\u548c\u540c\u6b65\u3001\u5171\u4eab\u53d8\u91cf\u6709\u5173\u7684\u64cd\u4f5c\u90fd\u5177\u6709<em>happens-before<\/em>\u5173\u7cfb\u3002<\/li><li>\u6ca1\u6709<strong>\u6570\u636e\u7ade\u4e89(Data race)<\/strong>\u3002\u6570\u636e\u7ade\u4e89\u5e94\u8be5\u7528Intrinsic Locks\u907f\u514d\u3002<br><strong>\u6570\u636e\u7ade\u4e89(Data race)<\/strong>\uff1a\u4e00\u4e2a\u7a0b\u5e8f\u4e2d\u5b58\u5728\u4e24\u5904\u6ca1\u6709<em>happens-before<\/em>\u5173\u7cfb\u7684\u51b2\u7a81\u8bbf\u95ee\u3002When a program contains two conflicting accesses that are not ordered by a happens-before relationship, it is said to contain a data race.<strong>\u51b2\u7a81\u8bbf\u95ee(conflicting access)<\/strong>\uff1a\u4e24\u4e2a\u64cd\u4f5c\u8bbf\u95ee\u540c\u4e00\u4e2a\u5171\u4eab\u53d8\u91cf\uff0c\u4e14\u5176\u4e2d\u81f3\u5c11\u6709\u4e00\u4e2a\u662f\u5199\u64cd\u4f5c\u3002Two actions using the same shared field or array variable are said to be conflicting if at least one of the accesses is a write.<\/li><\/ol>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"850\" height=\"447\" src=\"https:\/\/blog.cauchyschwarz.com\/wp-content\/uploads\/2021\/12\/java_multithread_jmm.png\" alt=\"\" class=\"wp-image-999\" srcset=\"https:\/\/blog.cauchyschwarz.com\/wp-content\/uploads\/2021\/12\/java_multithread_jmm.png 850w, https:\/\/blog.cauchyschwarz.com\/wp-content\/uploads\/2021\/12\/java_multithread_jmm-300x158.png 300w, https:\/\/blog.cauchyschwarz.com\/wp-content\/uploads\/2021\/12\/java_multithread_jmm-768x404.png 768w, https:\/\/blog.cauchyschwarz.com\/wp-content\/uploads\/2021\/12\/java_multithread_jmm-670x352.png 670w\" sizes=\"auto, (max-width: 850px) 100vw, 850px\" \/><\/figure>\n\n\n\n<ul class=\"wp-block-list\"><li>\u7528synchronized\u540c\u6b65\u7684\u4ee3\u7801\u5757<br>Intrinsic Locks\u505a\u4e86\u4e24\u4ef6\u4e8b\u60c5\uff1a<ol><li>\u4fdd\u8bc1\u4e86\u5bf9\u4e8e\u5171\u4eab\u53d8\u91cf\/\u4ee3\u7801\u5757\u7684\u4e92\u65a5\u8bbf\u95ee\uff1b<\/li><li>\u83b7\u5f97\u548c\u91ca\u653eIntrinsic Locks\u7684\u64cd\u4f5c\u9501\u5b9a\u548c\u5237\u65b0\u4e86\u4e0e\u5404\u7ebf\u7a0b\u76f8\u5bf9\u5e94\u7684\u672c\u5730\u5904\u7406\u5668\u7f13\u5b58\uff0c\u89e3\u51b3\u4e86\u53ef\u89c1\u6027\u95ee\u9898(visibility problems)\u3002<\/li><\/ol><\/li><li>volatile\u5173\u952e\u5b57<br>\u7528volatile\u5173\u952e\u5b57\u4fee\u9970\u5171\u4eab\u53d8\u91cf\u65f6\u8d77\u5230\u4e86\u548csynchronized\u7c7b\u4f3c\u7684\u4f5c\u7528\uff0c\u4f46\u662f\uff1a<ol><li><strong>volatile\u4e0d\u80fd\u4fdd\u8bc1\u4e00\u4e2a\u7ec4\u5408\u578b\u64cd\u4f5c\u6216\u8005\u591a\u4e2a\u64cd\u4f5c\u7684\u539f\u5b50\u6027\uff1b<\/strong><br>\u8b6c\u5982\uff0c\u4e0b\u9762\u7684\u4ee3\u7801\u662f\u6709\u95ee\u9898\u7684\uff1a<\/li><\/ol><\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">package com.logicbig.example;\n\npublic class CompositeActionWithVolatile {\n    static volatile int c;\n\n    public static void main (String[] args) throws InterruptedException {\n\n        for (int t = 0; t &lt; 10; t++) {\n            c = 0;\n\n            Thread thread1 = new Thread(() -&gt; {\n                for (int i = 0; i &lt; 1000; i++) {\n                    c++;\n                }\n            });\n\n            Thread thread2 = new Thread(() -&gt; {\n                for (int i = 0; i &lt; 1000; i++) {\n                    c++;\n                }\n            });\n\n            thread1.start();\n            thread2.start();\n\n            thread1.join();\n            thread2.join();\n\n            System.out.println(\"counter value = \" + c);\n        }\n\n    }\n}\n<\/code><\/pre>\n\n\n\n<p>\u50cf\u8fd9\u79cd<code>c++<\/code>\u7684\u64cd\u4f5c\u4e0d\u662f\u539f\u5b50\u64cd\u4f5c\uff0c\u662f\u5206\u8bfb\u53d6\u503c\uff0c\u52a0\u4e00\uff0c\u5199\u56de\u503c\u4e09\u4e2a\u6b65\u9aa4\u6267\u884c\u7684\uff0cvolatile\u5173\u952e\u5b57\u4e0d\u80fd\u4fdd\u8bc1\u53d8\u91cfc\u7684\u6b63\u786e\u540c\u6b65(\u4f30\u8ba1volatile\u9700\u8981\u5728\u5b8c\u6210\u52a0\u4e00\u518d\u5199\u4f1a\u540e\u624d\u4f1a\u89e6\u53d1\u540c\u6b65)\u3002<br><img loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"376\" class=\"wp-image-1000\" style=\"width: 750px;\" src=\"https:\/\/blog.cauchyschwarz.com\/wp-content\/uploads\/2021\/12\/java_multithread_composite-actions.png\" alt=\"\" srcset=\"https:\/\/blog.cauchyschwarz.com\/wp-content\/uploads\/2021\/12\/java_multithread_composite-actions.png 512w, https:\/\/blog.cauchyschwarz.com\/wp-content\/uploads\/2021\/12\/java_multithread_composite-actions-300x151.png 300w\" sizes=\"auto, (max-width: 750px) 100vw, 750px\" \/><br>\u5bf9\u4e8e\u8fd9\u79cd\u60c5\u51b5\u5c31\u5e94\u8be5\u4f7f\u7528synchronized\u5173\u952e\u5b57\u6216\u8005\u4f7f\u7528java.util.concurrent.atomic package\u91cc\u7684\u64cd\u4f5c\u5177\u6709\u539f\u5b50\u6027\u7684\u7c7b\u3002<br><img loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"298\" class=\"wp-image-1001\" style=\"width: 750px;\" src=\"https:\/\/blog.cauchyschwarz.com\/wp-content\/uploads\/2021\/12\/java_multithread_java-atomic-pkg.png\" alt=\"\" srcset=\"https:\/\/blog.cauchyschwarz.com\/wp-content\/uploads\/2021\/12\/java_multithread_java-atomic-pkg.png 1101w, https:\/\/blog.cauchyschwarz.com\/wp-content\/uploads\/2021\/12\/java_multithread_java-atomic-pkg-300x119.png 300w, https:\/\/blog.cauchyschwarz.com\/wp-content\/uploads\/2021\/12\/java_multithread_java-atomic-pkg-1024x406.png 1024w, https:\/\/blog.cauchyschwarz.com\/wp-content\/uploads\/2021\/12\/java_multithread_java-atomic-pkg-768x305.png 768w, https:\/\/blog.cauchyschwarz.com\/wp-content\/uploads\/2021\/12\/java_multithread_java-atomic-pkg-670x266.png 670w\" sizes=\"auto, (max-width: 750px) 100vw, 750px\" \/><\/p>\n\n\n\n<ol class=\"wp-block-list\" start=\"2\"><li><strong>volatile\u5173\u952e\u5b57\u4e0d\u89e3\u51b3\u4ee3\u7801\u53ef\u80fd\u88ab\u91cd\u65b0\u6392\u5217\u7684\u95ee\u9898\uff1b<\/strong><br><img loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"345\" class=\"wp-image-1002\" style=\"width: 750px;\" src=\"https:\/\/blog.cauchyschwarz.com\/wp-content\/uploads\/2021\/12\/java_multithread_reordering-example.png\" alt=\"\" srcset=\"https:\/\/blog.cauchyschwarz.com\/wp-content\/uploads\/2021\/12\/java_multithread_reordering-example.png 776w, https:\/\/blog.cauchyschwarz.com\/wp-content\/uploads\/2021\/12\/java_multithread_reordering-example-300x138.png 300w, https:\/\/blog.cauchyschwarz.com\/wp-content\/uploads\/2021\/12\/java_multithread_reordering-example-768x353.png 768w, https:\/\/blog.cauchyschwarz.com\/wp-content\/uploads\/2021\/12\/java_multithread_reordering-example-670x308.png 670w\" sizes=\"auto, (max-width: 750px) 100vw, 750px\" \/><\/li><\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">public class ReorderExample {\n    private int a = 1;\n    private boolean flg = true;\n\n    public void method1 () {\n        flg = false;\n        a = 2;\n    }\n\n    public void method2 () {\n        if (flg) {\n            System.out.println(\"a = \" + a);\n        }\n    }\n\n    public static void main (String[] args) {\n\n        for (int i = 0; i &lt; 100; i++) {\n\n            ReorderExample reorderExample = new ReorderExample();\n\n            Thread thread1 = new Thread(() -&gt; {\n                reorderExample.method1();\n            });\n\n            Thread thread2 = new Thread(() -&gt; {\n                reorderExample.method2();\n            });\n\n            thread1.start();\n            thread2.start();\n        }\n    }\n}\n<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\"> \u50cf\u8fd9\u79cd\u95ee\u9898\u7528volatile\u662f\u89e3\u51b3\u4e0d\u4e86\u7684\uff0c\u56e0\u4e3avolatile\u53ea\u662f\u89e3\u51b3\u7684\u53ef\u89c1\u6027\u95ee\u9898(visibility problem)\uff0c\u8ba9\u4e00\u4e2a\u7ebf\u7a0b\u5bf9\u53d8\u91cf\u505a\u7684\u6539\u52a8\u5bf9\u53e6\u4e00\u4e2a\u7ebf\u7a0b\u53ef\u89c1\u3002\u8fd9\u79cd\u60c5\u51b5\u7684\u6b63\u786e\u5904\u7406\u65b9\u5f0f\u662f\u52a0\u9501\uff1a  \n<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">public class ReorderExampleWithSynchronized {\n    private int a = 1;\n    private boolean flg = true;\n\n    public synchronized void method1 () {\n        flg = false;\n        a = 2;\n    }\n\n    public synchronized void method2 () {\n        if (flg) {\n            System.out.println(\"a = \" + a);\n        }\n    }\n\n    public static void main (String[] args) {\n\n        for (int i = 0; i &lt; 100; i++) {\n\n            ReorderExampleWithSynchronized reorderExample =\n                                             new ReorderExampleWithSynchronized();\n\n            Thread thread1 = new Thread(() -&gt; {\n                reorderExample.method1();\n            });\n\n            Thread thread2 = new Thread(() -&gt; {\n                reorderExample.method2();\n            });\n\n            thread1.start();\n            thread2.start();\n        }\n    }\n}\n<\/code><\/pre>\n\n\n\n<p>\u7528volatile\u65f6\u53ef\u4ee5\u8ba9\u4ee3\u7801\u66f4\u7b80\u6d01\uff0c\u76f8\u5f53\u4e8e\u8ba9JVM\u533a\u7ba1\u7406\u540c\u6b65\u800c\u4e0d\u662f\u6211\u4eec\u81ea\u5df1\uff1b\u5982\u679c\u4e00\u6bb5\u4ee3\u7801\u6216\u8005\u67d0\u4e2a\u65b9\u6cd5\u91cc\u9891\u7e41\u8bbf\u95ee\u67d0\u4e2avolatile\u53d8\u91cf\u662f\u53ef\u80fd\u5bfc\u81f4\u7a0b\u5e8f\u6027\u80fd\u4e0b\u964d\u7684\uff0c\u5012\u4e0d\u5982\u4f7f\u7528synchronized\u4fee\u9970\u8fd9\u4e2a\u4ee3\u7801\u5feb\u6216\u8005\u65b9\u6cd5\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Final\u57df<br>\u7531\u4e8efinal\u57df\u901a\u5e38\u5728\u88ab\u521d\u59cb\u5316\u540e\u5c31\u4e0d\u4f1a\u6539\u53d8\u503c\uff0c\u56e0\u6b64final\u57df\u6ca1\u6709\u53ef\u89c1\u6027\u548c\u91cd\u6392\u4ee3\u7801\u7684\u95ee\u9898\u3002<br>\u4e00\u4e2a\u53d8\u91cf\u4e0d\u80fd\u540c\u65f6\u7528final\u548cvolatile\u4e24\u4e2a\u5173\u952e\u5b57\u4fee\u9970\u3002<\/li><\/ul>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"%E4%BD%BF%E7%94%A8javautiltimer%E8%B0%83%E5%BA%A6%E5%AE%89%E6%8E%92%E4%BB%BB%E5%8A%A1\"><span class=\"ez-toc-section\" id=\"%E4%BD%BF%E7%94%A8javautilTimer%E8%B0%83%E5%BA%A6%E5%AE%89%E6%8E%92%E4%BB%BB%E5%8A%A1\"><\/span>\u4f7f\u7528java.util.Timer\u8c03\u5ea6(\u5b89\u6392)\u4efb\u52a1<span class=\"ez-toc-section-end\"><\/span><\/h1>\n\n\n\n<p>\u53ef\u4ee5\u4f7f\u7528\u7ebf\u7a0b\u5b89\u5168\u7684java.util.Timer\u7c7b\u6765\u5b89\u6392\u4efb\u52a1\u5728\u6307\u5b9a\u5ef6\u8fdf\u65f6\u95f4\u540e\u6267\u884c\uff0c\u6216\u8005\u5b9a\u65f6\u91cd\u590d\u3002<br><img loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"447\" class=\"wp-image-1003\" style=\"width: 750px;\" src=\"https:\/\/blog.cauchyschwarz.com\/wp-content\/uploads\/2021\/12\/java_multithread_timer-scheduling.png\" alt=\"\" srcset=\"https:\/\/blog.cauchyschwarz.com\/wp-content\/uploads\/2021\/12\/java_multithread_timer-scheduling.png 881w, https:\/\/blog.cauchyschwarz.com\/wp-content\/uploads\/2021\/12\/java_multithread_timer-scheduling-300x179.png 300w, https:\/\/blog.cauchyschwarz.com\/wp-content\/uploads\/2021\/12\/java_multithread_timer-scheduling-768x458.png 768w, https:\/\/blog.cauchyschwarz.com\/wp-content\/uploads\/2021\/12\/java_multithread_timer-scheduling-670x399.png 670w\" sizes=\"auto, (max-width: 750px) 100vw, 750px\" \/><\/p>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"threadlocal\"><span class=\"ez-toc-section\" id=\"ThreadLocal\"><\/span>ThreadLocal<span class=\"ez-toc-section-end\"><\/span><\/h1>\n\n\n\n<p>\u5bf9\u4e8e<a href=\"https:\/\/docs.oracle.com\/javase\/8\/docs\/api\/java\/lang\/ThreadLocal.html\">ThreadLocal<\/a>\u7c7b\u7684\u5bf9\u8c61\uff0c\u6bcf\u4e2a\u7ebf\u7a0b\u5bf9ThreadLocal\u5bf9\u8c61\u7684\u5f15\u7528\u90fd\u4f1a\u53d8\u6210\u5bf9\u8be5ThreadLocal\u5bf9\u8c61\u7684\u4e00\u4e2a\u4e0e\u8be5\u7ebf\u7a0b\u76f8\u5173\u8054\u7684\u62f7\u8d1d\u7684\u5e94\u7528(\u56e0\u6b64\uff0cThreadLocal\u5bf9\u8c61\u4e00\u822c\u662fprivate static, \u7528\u6765\u4fdd\u5b58\u548c\u7ebf\u7a0b\u76f8\u5173\u7684\u72b6\u6001)\u3002<\/p>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"jsr166concurrency-utilities\"><span class=\"ez-toc-section\" id=\"JSR166_Concurrency_Utilities\"><\/span>JSR166:Concurrency Utilities<span class=\"ez-toc-section-end\"><\/span><\/h1>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"907\" height=\"416\" src=\"https:\/\/blog.cauchyschwarz.com\/wp-content\/uploads\/2021\/12\/java_multithread_jsr-166.png\" alt=\"\" class=\"wp-image-1004\" srcset=\"https:\/\/blog.cauchyschwarz.com\/wp-content\/uploads\/2021\/12\/java_multithread_jsr-166.png 907w, https:\/\/blog.cauchyschwarz.com\/wp-content\/uploads\/2021\/12\/java_multithread_jsr-166-300x138.png 300w, https:\/\/blog.cauchyschwarz.com\/wp-content\/uploads\/2021\/12\/java_multithread_jsr-166-768x352.png 768w, https:\/\/blog.cauchyschwarz.com\/wp-content\/uploads\/2021\/12\/java_multithread_jsr-166-670x307.png 670w\" sizes=\"auto, (max-width: 907px) 100vw, 907px\" \/><\/figure>\n\n\n\n<ul class=\"wp-block-list\"><li>java.util.concurrent<\/li><li>java.util.concurrent.atomic<\/li><li>java.util.concurrent.locks<\/li><\/ul>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"executortrhead-pool%E5%92%8Cforkjoin-pool\"><span class=\"ez-toc-section\" id=\"Executor%E3%80%81Trhead_Pool%E5%92%8CForkJoin_Pool\"><\/span>Executor\u3001Trhead Pool\u548cForkJoin Pool<span class=\"ez-toc-section-end\"><\/span><\/h1>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"executor-framework\"><span class=\"ez-toc-section\" id=\"Executor_Framework\"><\/span>Executor Framework<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>java.util.concurrent\u5305\u91cc\u548cexecutor\u76f8\u5173\u7684\u63a5\u53e3\uff1a<br><img loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"357\" class=\"wp-image-1005\" style=\"width: 750px;\" src=\"https:\/\/blog.cauchyschwarz.com\/wp-content\/uploads\/2021\/12\/java_multithread_executor-framework-interfaces.png\" alt=\"\" srcset=\"https:\/\/blog.cauchyschwarz.com\/wp-content\/uploads\/2021\/12\/java_multithread_executor-framework-interfaces.png 852w, https:\/\/blog.cauchyschwarz.com\/wp-content\/uploads\/2021\/12\/java_multithread_executor-framework-interfaces-300x143.png 300w, https:\/\/blog.cauchyschwarz.com\/wp-content\/uploads\/2021\/12\/java_multithread_executor-framework-interfaces-768x366.png 768w, https:\/\/blog.cauchyschwarz.com\/wp-content\/uploads\/2021\/12\/java_multithread_executor-framework-interfaces-670x319.png 670w\" sizes=\"auto, (max-width: 750px) 100vw, 750px\" \/><br>java.util.concurrent\u5305\u91cc\u548cexecutor\u76f8\u5173\u7684\u7c7b\uff1a<br><img loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"582\" class=\"wp-image-1006\" style=\"width: 750px;\" src=\"https:\/\/blog.cauchyschwarz.com\/wp-content\/uploads\/2021\/12\/java_multithread_executor-framework-classes.png\" alt=\"\" srcset=\"https:\/\/blog.cauchyschwarz.com\/wp-content\/uploads\/2021\/12\/java_multithread_executor-framework-classes.png 912w, https:\/\/blog.cauchyschwarz.com\/wp-content\/uploads\/2021\/12\/java_multithread_executor-framework-classes-300x233.png 300w, https:\/\/blog.cauchyschwarz.com\/wp-content\/uploads\/2021\/12\/java_multithread_executor-framework-classes-768x596.png 768w, https:\/\/blog.cauchyschwarz.com\/wp-content\/uploads\/2021\/12\/java_multithread_executor-framework-classes-670x520.png 670w\" sizes=\"auto, (max-width: 750px) 100vw, 750px\" \/><\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"executor-interface\"><span class=\"ez-toc-section\" id=\"Executor_interface\"><\/span>Executor interface<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">package java.util.concurrent;\n    public interface Executor {\n    void execute(Runnable command);\n    }\n<\/code><\/pre>\n\n\n\n<p><code>Executor#executor(command)<\/code>\u5b9e\u9645\u4e0a\u7b49\u6548\u4e8e<code>(new Thread(command)).start();<\/code><\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"executorservice-interface\"><span class=\"ez-toc-section\" id=\"ExecutorService_interface\"><\/span>ExecutorService interface<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>\u4f5c\u4e3aExecutor\u7684\u5b50\u7c7b\uff0cExecutorService\u7c7b\u6dfb\u52a0\u4e86\u4e00\u4e9b\u7528\u4e8e\u7ba1\u7406Executor\u672c\u8eab\u548c\u8981\u6267\u884c\u7684\u4efb\u52a1\u7684\u751f\u547d\u5468\u671f\u7684\u63a5\u53e3\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">package java.util.concurrent;\n\nimport java.util.List;\nimport java.util.Collection;\n\npublic interface ExecutorService extends Executor {\n\n    void shutdown();\n\n    List&lt;Runnable&gt; shutdownNow();\n\n    boolean isShutdown();\n\n    boolean isTerminated();\n\n    boolean awaitTermination(long timeout, TimeUnit unit)\n        throws InterruptedException;\n\n    &lt;T&gt; Future&lt;T&gt; submit(Callable&lt;T&gt; task);\n\n    &lt;T&gt; Future&lt;T&gt; submit(Runnable task, T result);\n\n    Future&lt;?&gt; submit(Runnable task);\n\n    &lt;T&gt; List&lt;Future&lt;T&gt;&gt; invokeAll(Collection&lt;? extends Callable&lt;T&gt;&gt; tasks)\n        throws InterruptedException;\n\n    &lt;T&gt; List&lt;Future&lt;T&gt;&gt; invokeAll(Collection&lt;? extends Callable&lt;T&gt;&gt; tasks,\n                                  long timeout, TimeUnit unit)\n        throws InterruptedException;\n\n    &lt;T&gt; T invokeAny(Collection&lt;? extends Callable&lt;T&gt;&gt; tasks)\n        throws InterruptedException, ExecutionException;\n\n    &lt;T&gt; T invokeAny(Collection&lt;? extends Callable&lt;T&gt;&gt; tasks,\n                    long timeout, TimeUnit unit)\n        throws InterruptedException, ExecutionException, TimeoutException;\n}\n<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\"><li>ExecutorService\u4e4bsubmit\u65b9\u6cd5<br>\u7c7b\u4f3c\u4e8eExecutor#execute\u65b9\u6cd5\uff0cExecutorService\u63d0\u4f9b\u4e86\u66f4\u7075\u6d3b\u7684submit\u65b9\u6cd5<\/li><li>ExecutorService\u4e4bCallable\u63a5\u53e3<br>submit\u65b9\u6cd5\u4e0d\u4ec5\u63a5\u53d7Runnable\u7c7b\u578b\u53c2\u6570\uff0c\u4e5f\u63a5\u53d7Callable\u7c7b\u578b\u53c2\u6570\uff1a<\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">package java.util.concurrent;\n\npublic interface Callable&lt;V&gt; {\n    V call() throws Exception;\n}\n<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\"><li>ExecutorService\u4e4bFuture\u65b9\u6cd5<br>submit\u65b9\u6cd5\u8fd4\u56de\u7684\u662f<em>Future<\/em>\u7c7b\u578b\u7684\u503c\uff0c\u7528\u4e8e\u83b7\u53d6<em>Callable<\/em>\u7684\u8fd4\u56de\u503c\u548c\u7ba1\u7406Callable\/Runable\u4efb\u52a1\u7684\u72b6\u6001<\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">package java.util.concurrent;\n\npublic interface Future&lt;V&gt; {\n\n    boolean cancel(boolean mayInterruptIfRunning);\n\n    boolean isCancelled();\n\n    boolean isDone();\n\n    V get() throws InterruptedException, ExecutionException;\n\n    V get(long timeout, TimeUnit unit)\n        throws InterruptedException, ExecutionException, TimeoutException;\n}\n<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\"><li>ExecutorService\u4e4b\u6279\u91cf\u6267\u884c\u4efb\u52a1\u7684\u63a5\u53e3<br>\u5173\u4e8eBulk Submission\u7684\u65b9\u6cd5\u5c31\u662fExecutorService\u5b9a\u4e49\u4e2d\u548cCollection\u6709\u5173\u7684\u90a3\u4e9b\u63a5\u53e3<\/li><li>ExecutorService\u4e4b\u751f\u547d\u5468\u671f\u7ba1\u7406\u7684\u63a5\u53e3<br>\u770b\u65b9\u6cd5\u540d\u5b57\u5c31\u77e5\u9053\u54ea\u4e9b\u63a5\u53e3\u662f\u7528\u6765\u5e72\u8fd9\u4e2a\u7684\u4e86<\/li><\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"executors%E7%B1%BB\"><span class=\"ez-toc-section\" id=\"Executors%E7%B1%BB\"><\/span>Executors\u7c7b<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>\u57fa\u4e8e\u5de5\u5382\u6a21\u5f0f\uff0cExecutors\u7c7b\u63d0\u4f9b\u4e86\u7528\u4e8e\u521b\u5efaExecutorService\u7c7b\u5b9e\u4f8b\u7684\u9759\u6001\u65b9\u6cd5(\u4e00\u822c\u4e0d\u7528new\u521b\u5efaExecutorService\u7c7b\u5b9e\u4f8b)\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">import java.util.concurrent.ExecutorService;\nimport java.util.concurrent.Executors;\nimport java.util.concurrent.Future;\n\npublic class SingleThreadExecutorExample {\n\n    public static void main (String[] args) {\n        ExecutorService executorService = Executors.newSingleThreadExecutor();\n        Future&lt;?&gt; future = executorService.submit(new Runnable() {\n            @Override\n            public void run () {\n                System.out.println(\"task running\");\n            }\n        });\n\n        executorService.shutdown();\n    }\n}\n<\/code><\/pre>\n\n\n\n<p>Executors\u7c7b\u63d0\u4f9b\u7684\u521b\u5efaExecutorService\u7c7b\u5b9e\u4f8b\u7684\u65b9\u6cd5\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Executors method<\/th><th>Description<\/th><\/tr><\/thead><tbody><tr><td>newCachedThreadPool(&#8230;)<\/td><td>Creates a cached thread pool&#8217;s ExecutorService that can create new threads as needed or reused cached thread.<\/td><\/tr><tr><td>newFixedThreadPool(&#8230;)<\/td><td>Creates a thread pool that reuses a fixed number of threads operating off a shared unbounded queue.<\/td><\/tr><tr><td>newScheduledThreadPool(&#8230;)<\/td><td>Creates a thread pool that can schedule commands to run after a given delay, or to execute periodically.<\/td><\/tr><tr><td>newSingleThreadExecutor(&#8230;)<\/td><td>Creates an Executor that uses a single worker thread operating off an unbounded queue. Above, We saw an example already.<\/td><\/tr><tr><td>newSingleThreadScheduledExecutor(..)<\/td><td>Creates a single-threaded executor that can schedule commands to run after a given delay, or to execute periodically.<\/td><\/tr><tr><td>newWorkStealingPool(..)<\/td><td>Creates a work-stealing thread pool using all available processors as its target parallelism level. May use multiple queues to reduce contention.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"thread-pool\"><span class=\"ez-toc-section\" id=\"Thread_Pool\"><\/span>Thread Pool<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li>\u7ebf\u7a0b\u6c60\u901a\u8fc7\u7528\u63d0\u524d\u5b9e\u4f8b\u5316\u7684\u7ebf\u7a0b\u6765\u6267\u884c\u5e94\u7528\u7a0b\u5e8f\u7ea7\u522b\u7684\u4efb\u52a1\uff0c\u51cf\u5c11\u4e86\u521b\u5efa\u7ebf\u7a0b\u65f6\u7684\u989d\u5916\u5f00\u9500\u3002<\/li><li>\u53e6\u4e00\u65b9\u9762\uff0c\u7ebf\u7a0b\u6c60\u9650\u5236\u4e86\u540c\u65f6\u6267\u884c\u4efb\u52a1\u7684\u7ebf\u7a0b\u6570(\u8d85\u8fc7\u7ebf\u7a0b\u6c60\u7ebf\u7a0b\u6570\u7684\u4efb\u52a1\u88ab\u653e\u5165\u961f\u5217\u4e2d\u7b49\u5f85\u6267\u884c\uff0c\u800c\u4e0d\u662f\u6bcf\u6765\u4e00\u4e2a\u4efb\u52a1\u5c31\u7acb\u9a6c\u521b\u5efa\u4e00\u4e2a\u65b0\u7ebf\u7a0b\u6267\u884c)\u4ece\u800c\u907f\u514d\u4e86\u7cfb\u7edf\u8d44\u6e90\u88ab\u7528\u5c3d\u7684\u60c5\u51b5\u51fa\u73b0\u3002<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"%E5%85%B3%E4%BA%8E%E7%BA%BF%E7%A8%8B%E6%B1%A0%E7%9A%84%E4%B8%80%E4%BA%9B%E6%A6%82%E5%BF%B5\"><span class=\"ez-toc-section\" id=\"%E5%85%B3%E4%BA%8E%E7%BA%BF%E7%A8%8B%E6%B1%A0%E7%9A%84%E4%B8%80%E4%BA%9B%E6%A6%82%E5%BF%B5\"><\/span>\u5173\u4e8e\u7ebf\u7a0b\u6c60\u7684\u4e00\u4e9b\u6982\u5ff5<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li>Number of threads(N)<br>\u7ebf\u7a0b\u6c60\u4e2d\u6b63\u5728\u6267\u884c\u4efb\u52a1\u7684\u7ebf\u7a0b\u6570\u4e0eidle\u7ebf\u7a0b\u6570\u603b\u548c<br><code>getPoolSize()<\/code>\u7684\u8fd4\u56de\u503c<\/li><li>Core pool size(C)<br>\u7ebf\u7a0b\u6c60\u5728\u628a\u65b0\u4efb\u52a1\u653e\u5165\u961f\u5217\u4e2d\u7b49\u5f85\u6267\u884c\u4e4b\u524d\u6700\u591a\u53ef\u4ee5\u540c\u65f6\u5b58\u5728\u7684\u7ebf\u7a0b\u6570\u3002<br>\u5728\u7ebf\u7a0b\u6c60\u91cc\u540c\u65f6\u5b58\u5728\u7684\u7ebf\u7a0b\u6570\u8fbe\u5230C\u4e4b\u524d\uff0c\u5bf9\u4e8e\u65b0\u6765\u7684\u4efb\u52a1\uff0c\u7ebf\u7a0b\u6c60\u4f1a\u65b0\u5efa\u7ebf\u7a0b\u6765\u6267\u884c\u4efb\u52a1\uff0c\u800c\u4e0d\u662f\u8ba9idle threads\u6765\u6267\u884c\u8fd9\u4e9b\u4efb\u52a1\u3002\u53ea\u6709\u5728\u7ebf\u7a0b\u6c60\u7684\u7ebf\u7a0b\u6570\u8fbe\u5230C\u65f6\uff0c\u624d\u4f1a\u91cd\u7528idle threads\u3002<br><code>getCorePoolSize()<\/code>\u7684\u8fd4\u56de\u503c<\/li><li>Maximum pool size(M)<br>\u5bf9\u4e8e\u6709\u9650\u961f\u5217(bounded queues)\uff0c\u5728\u7b49\u5f85\u4efb\u52a1\u6570\u586b\u6ee1\u4e86\u961f\u5217\u65f6\uff0c\u7ebf\u7a0b\u6c60\u53ef\u4ee5\u518d\u8fdb\u4e00\u6b65\u6269\u5145\u53ef\u4ee5\u540c\u65f6\u5b58\u5728\u7684\u7ebf\u7a0b\u6570\u8fbe\u5230M\u3002\u5728\u7ebf\u7a0b\u6c60\u5df2\u7ecf\u5c06\u540c\u65f6\u5b58\u5728\u7684\u7ebf\u7a0b\u6570\u63d0\u5347\u5230\u4e86M\u4e4b\u540e\uff0c\u7b49\u5f85\u961f\u5217\u4ecd\u7136\u662f\u6ee1\u7684\u8bdd\uff0c\u90a3\u4e48\u5bf9\u4e8e\u65b0\u52a0\u8fdb\u6765\u7684\u4efb\u52a1\u7ebf\u7a0b\u6c60\u53ea\u6709\u62d2\u7edd(reject)\u4e86\u3002<\/li><li>keepAliveTime<br>idle threads\u5728\u7a7a\u95f2\u8d85\u8fc7\u6307\u5b9a\u65f6\u95f4\u540e\u4f1a\u88ab\u7ed3\u675f\u3002\u4e5f\u53ef\u4ee5\u7528<code>allowCoreThreadTimeOut(boolean)<\/code>\u9009\u62e9\u8ba9\u7a7a\u95f2\u7ebf\u7a0b\u4e00\u76f4\u5b58\u5728\u3002<\/li><li>workQueue<br>\u5fc5\u987b\u662f\u4e00\u4e2a<em>BlockingQueue<\/em>\u3002\u6709\u754c\u961f\u5217,ArrayBlockingQueue\uff1b\u65e0\u754c\u961f\u5217\uff0cLinkedBlockingQueue\u3002\u6216\u8005\u53ef\u4ee5\u7528hand-offs queue, SynchronousQueue\uff0c\u7528SynchronousQueue\u65f6\u4f1a\u5c1d\u8bd5\u7acb\u5373\u6267\u884c\u52a0\u5165\u7684\u4efb\u52a1\uff0c\u5982\u679c\u7ebf\u7a0b\u6c60\u7ebf\u7a0b\u6570\u8fd8\u6ca1\u6709\u8fbe\u5230M\uff0c\u5c31\u589e\u52a0\u7ebf\u7a0b\u6c60\u7ebf\u7a0b\u6570\uff0c\u5982\u679c\u7ebf\u7a0b\u6c60\u7ebf\u7a0b\u6570\u8fbe\u5230\u4e86M\uff0c\u5c31\u62d2\u7edd\u65b0\u4efb\u52a1\u3002\u5bf9\u4e8e\u8fd9\u79cdSynchronousQueue\uff0cCorePoolSize\u5c31\u663e\u5f97\u6ca1\u6709\u610f\u4e49\u4e86\u3002<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"java-thread-pool%E7%9A%84%E5%AE%9E%E7%8E%B0\"><span class=\"ez-toc-section\" id=\"Java_Thread_Pool%E7%9A%84%E5%AE%9E%E7%8E%B0\"><\/span>Java Thread Pool\u7684\u5b9e\u73b0<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"404\" height=\"346\" src=\"https:\/\/blog.cauchyschwarz.com\/wp-content\/uploads\/2021\/12\/java_multithread_thread-pool-impl.png\" alt=\"\" class=\"wp-image-1007\" srcset=\"https:\/\/blog.cauchyschwarz.com\/wp-content\/uploads\/2021\/12\/java_multithread_thread-pool-impl.png 404w, https:\/\/blog.cauchyschwarz.com\/wp-content\/uploads\/2021\/12\/java_multithread_thread-pool-impl-300x257.png 300w\" sizes=\"auto, (max-width: 404px) 100vw, 404px\" \/><\/figure><\/div>\n\n\n\n<p><strong>ThreadPoolExecutor<\/strong>\u7c7b\u7684\u6784\u9020\u51fd\u6570\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue&lt;Runnable&gt; workQueue) \n\nThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue&lt;Runnable&gt; workQueue, RejectedExecutionHandler handler) \n\nThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue&lt;Runnable&gt; workQueue, ThreadFactory threadFactory) \n\nThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue&lt;Runnable&gt; workQueue, ThreadFactory threadFactory, RejectedExecutionHandler handler)\n<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\"><li>handler<br>\u5728\u7ebf\u7a0b\u6c60\u62d2\u7edd\u52a0\u5165\u65b0\u7684\u4efb\u52a1\u65f6\uff0c\u7ebf\u7a0b\u6c60\u4f1a\u8c03\u7528handler\u8fdb\u884c\u76f8\u5e94\u5904\u7406\uff1a<\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">package java.util.concurrent;\n\npublic interface RejectedExecutionHandler {\n   void rejectedExecution(Runnable r, ThreadPoolExecutor executor);\n}\n<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>ThreadPoolExecutor.AbortPolicy<\/strong>\uff1a<\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">    public static class AbortPolicy implements RejectedExecutionHandler {\n\n      public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {\n        throw new RejectedExecutionException(\"Task \" + r.toString() +\n                                             \" rejected from \" +\n                                             e.toString());\n      }\n    }\n<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>ThreadPoolExecutor.CallerRunsPolicy<\/strong>\uff1a<\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">public static class CallerRunsPolicy implements RejectedExecutionHandler {\n\n    public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {\n        if (!e.isShutdown()) {\n            r.run();\n        }\n    }\n}\n<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>ThreadPoolExecutor.DiscardPolicy<\/strong>\uff1a<\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">    public static class DiscardPolicy implements RejectedExecutionHandler {\n\n      public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {\n\n      }\n    }\n<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>ThreadPoolExecutor.DiscardOldestPolicy<\/strong>\uff1a<\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">    public static class DiscardOldestPolicy implements RejectedExecutionHandler {\n\n       public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {\n         if (!e.isShutdown()) {\n            e.getQueue().poll();\n            e.execute(r);\n       }\n      }\n    }\n<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\"><li>threadFactory<\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">package java.util.concurrent;\n\npublic interface ThreadFactory {\n  Thread newThread(Runnable var1);\n}\n<\/code><\/pre>\n\n\n\n<p>ThreadPoolExecutor\u91cc\u7684\u5176\u4ed6\u6709\u7528\u65b9\u6cd5\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><em>int prestartAllCoreThreads()<\/em><br>\u63d0\u524d\u521b\u5efa\u7ebf\u7a0b\u800c\u4e0d\u662f\u9ed8\u8ba4\u7684\u5728\u9700\u8981\u65f6\u521b\u5efa\u7ebf\u7a0b\u3002\u8fd4\u56de\u5b9e\u9645\u521b\u5efa\u7684\u7ebf\u7a0b\u6570\u3002<br>*boolean prestartCoreThread()*\u53ea\u4f1a\u521b\u5efa\u4e00\u4e2a\u7ebf\u7a0b\u3002\u5982\u679c\u6240\u6709\u7684\u7ebf\u7a0b\u90fd\u5df2\u521b\u5efa\u5b8c\u6bd5\uff0c\u90a3\u4e48\u8fd4\u56defalse\u3002<\/li><\/ul>\n\n\n\n<p>\u624b\u52a8\u4f7f\u7528ThreadPoolExecutor\u7c7b\u7684\u6784\u9020\u51fd\u6570\u7684\u4f8b\u5b50\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">public class ThreadPoolExecutorExample {\n\n    public static void main (String[] args) {\n        createAndRunPoolForQueue(new ArrayBlockingQueue&lt;Runnable&gt;(3), \"Bounded\");\n        createAndRunPoolForQueue(new LinkedBlockingDeque&lt;&gt;(), \"Unbounded\");\n        createAndRunPoolForQueue(new SynchronousQueue&lt;Runnable&gt;(), \"Direct hand-off\");\n    }\n\n    private static void createAndRunPoolForQueue (BlockingQueue&lt;Runnable&gt; queue,\n                                                                      String msg) {\n        System.out.println(\"---- \" + msg + \" queue instance = \" +\n                                                  queue.getClass()+ \" -------------\");\n\n        ThreadPoolExecutor e = new ThreadPoolExecutor(2, 5, Long.MAX_VALUE,\n                                 TimeUnit.NANOSECONDS, queue);\n\n        for (int i = 0; i &lt; 10; i++) {\n            try {\n                e.execute(new Task());\n            } catch (RejectedExecutionException ex) {\n                System.out.println(\"Task rejected = \" + (i + 1));\n            }\n            printStatus(i + 1, e);\n        }\n\n        e.shutdownNow();\n\n        System.out.println(\"--------------------\\n\");\n    }\n\n    private static void printStatus (int taskSubmitted, ThreadPoolExecutor e) {\n        StringBuilder s = new StringBuilder();\n        s.append(\"poolSize = \")\n         .append(e.getPoolSize())\n         .append(\", corePoolSize = \")\n         .append(e.getCorePoolSize())\n         .append(\", queueSize = \")\n         .append(e.getQueue()\n                  .size())\n         .append(\", queueRemainingCapacity = \")\n         .append(e.getQueue()\n                  .remainingCapacity())\n         .append(\", maximumPoolSize = \")\n         .append(e.getMaximumPoolSize())\n         .append(\", totalTasksSubmitted = \")\n         .append(taskSubmitted);\n\n        System.out.println(s.toString());\n    }\n\n    private static class Task implements Runnable {\n\n        @Override\n        public void run () {\n            while (true) {\n                try {\n                    Thread.sleep(1000000);\n                } catch (InterruptedException e) {\n                    break;\n                }\n            }\n        }\n    }\n}\n<\/code><\/pre>\n\n\n\n<p>\u4e0d\u8fc7\u6211\u4eec\u4e00\u822c\u4e0d\u76f4\u63a5\u8c03\u7528\u8fd9\u4e9bThreadPoolExecutor\u7684\u6784\u9020\u51fd\u6570\uff0c\u800c\u662f\u8c03\u7528Executors\u7684\u5de5\u5382\u65b9\u6cd5\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Executors method<\/th><th>ThreadPoolExecutor\u7684\u7b49\u4ef7\u8c03\u7528<\/th><\/tr><\/thead><tbody><tr><td><em>newCachedThreadPool()<\/em><\/td><td><code>new ThreadPoolExecutor(0, Integer.MAX_VALUE, 60L, TimeUnit.SECONDS, new SynchronousQueue&lt;Runnable&gt;());<\/code><\/td><\/tr><tr><td><em>newFixedThreadPool(int nThreads)<\/em><\/td><td><code>new ThreadPoolExecutor(nThreads, nThreads, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue&lt;Runnable&gt;());<\/code><\/td><\/tr><tr><td><em>newSingleThreadExecutor()<\/em><\/td><td><code>new ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue&lt;Runnable&gt;());<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"scheduled-thread-pools\"><span class=\"ez-toc-section\" id=\"Scheduled_Thread_Pools\"><\/span>Scheduled Thread Pools<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"226\" height=\"172\" src=\"https:\/\/blog.cauchyschwarz.com\/wp-content\/uploads\/2021\/12\/java_multithread_ses-hier.png\" alt=\"\" class=\"wp-image-1008\"\/><\/figure><\/div>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"628\" src=\"https:\/\/blog.cauchyschwarz.com\/wp-content\/uploads\/2021\/12\/java_multithread_ScheduledExecutorService-1024x628.png\" alt=\"\" class=\"wp-image-1009\" srcset=\"https:\/\/blog.cauchyschwarz.com\/wp-content\/uploads\/2021\/12\/java_multithread_ScheduledExecutorService-1024x628.png 1024w, https:\/\/blog.cauchyschwarz.com\/wp-content\/uploads\/2021\/12\/java_multithread_ScheduledExecutorService-300x184.png 300w, https:\/\/blog.cauchyschwarz.com\/wp-content\/uploads\/2021\/12\/java_multithread_ScheduledExecutorService-768x471.png 768w, https:\/\/blog.cauchyschwarz.com\/wp-content\/uploads\/2021\/12\/java_multithread_ScheduledExecutorService-670x411.png 670w, https:\/\/blog.cauchyschwarz.com\/wp-content\/uploads\/2021\/12\/java_multithread_ScheduledExecutorService.png 1379w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>\u6ce8\u610f<em>scheduleWithFixedDelay(Runnable command, long initialDelay, long delay, TimeUnit unit);\u7684delay\u662f\u8ddf\u5728\u6bcf\u4e2aTask\u540e\u9762\u7684Delay\u3002<br>ScheduledExecutorService\u63a5\u53e3\u91cc\u7684\u65b9\u6cd5\u8fd4\u56de\u7684\u90fd\u662fScheduledFuture<\/em>\u63a5\u53e3\u7c7b\u578b\uff0c\u8be5\u63a5\u53e3\u5b9e\u73b0\u4e86<em>Future<\/em>\uff0c<em>Delay<\/em>\u548c<em>Comparable<\/em>\u4e09\u4e2a\u63a5\u53e3\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"scheduledthreadpoolexecutor%E7%B1%BB\"><span class=\"ez-toc-section\" id=\"ScheduledThreadPoolExecutor%E7%B1%BB\"><\/span>ScheduledThreadPoolExecutor\u7c7b<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"259\" height=\"256\" src=\"https:\/\/blog.cauchyschwarz.com\/wp-content\/uploads\/2021\/12\/java_multithread_s-pool-impl.png\" alt=\"\" class=\"wp-image-1010\"\/><\/figure><\/div>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">ScheduledThreadPoolExecutor(int corePoolSize) \n\nScheduledThreadPoolExecutor(int corePoolSize, RejectedExecutionHandler handler) \n\nScheduledThreadPoolExecutor(int corePoolSize, ThreadFactory threadFactory) \n\nScheduledThreadPoolExecutor(int corePoolSize, ThreadFactory threadFactory, RejectedExecutionHandler handler) \n<\/code><\/pre>\n\n\n\n<p>\u4e0eThreadPoolExecutor\u7684\u6784\u9020\u51fd\u6570\u76f8\u6bd4\uff0cScheduledThreadPoolExecutor\u5c11\u4e86\u5f88\u591a\u6784\u9020\u65f6\u53c2\u6570\uff0c\u6bd4\u5982\u6ca1\u6709\u8981\u6c42\u6307\u5b9a\u961f\u5217\uff0c\u56e0\u4e3a\u56fa\u5b9a\u4f7f\u7528\u4e86unbounded queue\u3002<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>While this class inherits from ThreadPoolExecutor, a few of the inherited tuning methods are not useful for it. In particular, because it acts as a fixed-sized pool using corePoolSize threads and an unbounded queue, adjustments to maximumPoolSize have no useful effect. Additionally, it is almost never a good idea to set corePoolSize to zero or use allowCoreThreadTimeOut because this may leave the pool without threads to handle tasks once they become eligible to run.<\/p><\/blockquote>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"forkjoin-framework\"><span class=\"ez-toc-section\" id=\"Forkjoin_framework\"><\/span>Fork\/join framework<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p><img loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"511\" class=\"wp-image-1011\" style=\"width: 750px;\" src=\"https:\/\/blog.cauchyschwarz.com\/wp-content\/uploads\/2021\/12\/java_multithread_fork-join-api.png\" alt=\"\" srcset=\"https:\/\/blog.cauchyschwarz.com\/wp-content\/uploads\/2021\/12\/java_multithread_fork-join-api.png 712w, https:\/\/blog.cauchyschwarz.com\/wp-content\/uploads\/2021\/12\/java_multithread_fork-join-api-300x204.png 300w, https:\/\/blog.cauchyschwarz.com\/wp-content\/uploads\/2021\/12\/java_multithread_fork-join-api-670x456.png 670w\" sizes=\"auto, (max-width: 750px) 100vw, 750px\" \/><br>\u56e0\u4e3aForkJoinPool\u4e0d\u662f\u7ee7\u627f\u81eaThreadPoolExecutor\uff0c\u6240\u4ee5\u5bf9\u4e8eForkJoinPool\u800c\u8a00\u6ca1\u6709\u524d\u9762\u8bb2\u7684\u90a3\u4e9bcore pool size, queuing, maximum pool size\u7b49\u6982\u5ff5\u3002<\/p>\n\n\n\n<p>\u83b7\u53d6ForkJoinPool\u5b9e\u4f8b\u7684\u65b9\u6cd5\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>\u9759\u6001\u65b9\u6cd5<br><code>public static ForkJoinPool commonPool()<\/code><\/li><li>\u6784\u9020\u51fd\u6570<\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">ForkJoinPool()  \nForkJoinPool(int parallelism)  \nForkJoinPool(int parallelism, ForkJoinWorkerThreadFactory factory, UncaughtExceptionHandler handler, boolean asyncMode)\n<\/code><\/pre>\n\n\n\n<p>\u63d0\u4ea4\u4efb\u52a1\u7684\u65b9\u6cd5\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>invoke(ForkJoinTask)<\/code>\uff0c\u540c\u6b65<\/li><li><code>execute(ForkJoinTask)<\/code>\uff0c\u5f02\u6b65<\/li><li><code>submit(ForkJoinTask)<\/code>\uff0c\u5f02\u6b65<\/li><\/ul>\n\n\n\n<p>\u5173\u4e8eForkJoinTask\u7c7b\uff1a<br>\u4e00\u4e2a\u4ee3\u8868\u53ef\u4ee5\u5206\u800c\u6cbb\u4e4b\u7684\u4efb\u52a1\u7684\u62bd\u8c61\u7c7b\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>ForkJoinTask#fork()\u65b9\u6cd5\uff1a<br>\u5c06\u6b64\u4efb\u52a1\u63d0\u4ea4\u5230ForkJoinPool\u8fdb\u884c\u5f02\u6b65\u8ba1\u7b97\u3002<\/li><li>ForkJoinTask#join()\u65b9\u6cd5\uff1a \u963b\u585e\u7b49\u5f85\u8ba1\u7b97\u7ed3\u679c\u3002<\/li><\/ul>\n\n\n\n<p>ForkJoinTask\u7684\u4e09\u4e2a\u5b50\u7c7b\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><em>RecursiveAction<\/em><br><code>protected abstract void compute()<\/code><\/li><li><em>RecursiveTask<\/em><br><code>protected abstract V compute()<\/code><\/li><li>*CountedCompleter`(since Java 8)<\/li><\/ul>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"%E5%87%A0%E7%A7%8D%E5%90%8C%E6%AD%A5%E6%9C%BA%E5%88%B6\"><span class=\"ez-toc-section\" id=\"%E5%87%A0%E7%A7%8D%E5%90%8C%E6%AD%A5%E6%9C%BA%E5%88%B6\"><\/span>\u51e0\u79cd\u540c\u6b65\u673a\u5236<span class=\"ez-toc-section-end\"><\/span><\/h1>\n\n\n\n<ul class=\"wp-block-list\"><li>Semaphore<br>Semaphore#acquire()\u548cSemaphore#release()<\/li><li>CountDownLatch<\/li><li>CyclicBarrier<br>\u4e0eCountDownLatch\u6709\u4e9b\u7c7b\u4f3c\uff0c\u53ea\u662f\u5728CyclicBarrier\u7684\u503c\u53d8\u4e3a0\u4e4b\u540e\uff0c\u53c8\u4f1a\u53d8\u56de\u521d\u59cb\u503c\u3002\u8fd9\u6837CyclicBarrier\u5c31\u53ef\u4ee5\u7528\u6765\u5bf9\u51e0\u4e2a\u7ebf\u7a0b\u8fdb\u884c\u9694\u4e00\u6bb5\u65f6\u95f4\u7684\u540c\u6b65\u5de5\u4f5c\u3002<\/li><\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Java\u4e2d\u521b\u5efa\u7ebf\u7a0b\u7684\u4e24\u79cd\u65b9\u5f0f \u7ee7\u627fThread \u5b9e\u73b0Runnable\u63a5\u53e3 Thread\u7c7b\u7684\u51e0\u4e2a\u65b9\u6cd5\uff1a Thread#start() Thread#currentThread() Thread#slee&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10],"tags":[87],"class_list":["post-993","post","type-post","status-publish","format-standard","hentry","category-10","tag-java"],"_links":{"self":[{"href":"https:\/\/blog.cauchyschwarz.com\/index.php?rest_route=\/wp\/v2\/posts\/993","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.cauchyschwarz.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.cauchyschwarz.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.cauchyschwarz.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.cauchyschwarz.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=993"}],"version-history":[{"count":1,"href":"https:\/\/blog.cauchyschwarz.com\/index.php?rest_route=\/wp\/v2\/posts\/993\/revisions"}],"predecessor-version":[{"id":1012,"href":"https:\/\/blog.cauchyschwarz.com\/index.php?rest_route=\/wp\/v2\/posts\/993\/revisions\/1012"}],"wp:attachment":[{"href":"https:\/\/blog.cauchyschwarz.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=993"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.cauchyschwarz.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=993"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.cauchyschwarz.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=993"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}