{"id":3961,"date":"2025-07-20T11:27:43","date_gmt":"2025-07-20T03:27:43","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=3961"},"modified":"2025-07-20T11:27:43","modified_gmt":"2025-07-20T03:27:43","slug":"%e7%ba%bf%e7%a8%8b%e7%8a%b6%e6%80%81%ef%bc%8cblocked%e5%92%8cwaiting%e6%9c%89%e4%bb%80%e4%b9%88%e5%8c%ba%e5%88%ab","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/07\/20\/%e7%ba%bf%e7%a8%8b%e7%8a%b6%e6%80%81%ef%bc%8cblocked%e5%92%8cwaiting%e6%9c%89%e4%bb%80%e4%b9%88%e5%8c%ba%e5%88%ab\/","title":{"rendered":"\u7ebf\u7a0b\u72b6\u6001\uff0cBLOCKED\u548cWAITING\u6709\u4ec0\u4e48\u533a\u522b"},"content":{"rendered":"\n<p>\u7ebf\u7a0b\u72b6\u6001\u4e2d\uff0c<strong>BLOCKED<\/strong> \u548c <strong>WAITING<\/strong> \u662f\u4e24\u79cd\u4e0d\u540c\u7684\u963b\u585e\u72b6\u6001\uff0c\u5b83\u4eec\u7684\u533a\u522b\u4e3b\u8981\u4f53\u73b0\u5728\u7ebf\u7a0b\u963b\u585e\u7684\u539f\u56e0\u548c\u7b49\u5f85\u65b9\u5f0f\u4e0a\u3002\u4e0b\u9762\u5e2e\u4f60\u8be6\u7ec6\u89e3\u6790\u8fd9\u4e24\u8005\u7684\u533a\u522b\u548c\u5e94\u7528\u573a\u666f\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e00\u3001\u7ebf\u7a0b\u72b6\u6001\u7b80\u8ff0<\/h2>\n\n\n\n<p>Java \u4e2d\u7ebf\u7a0b\u7684\u72b6\u6001\u7531 <code>Thread.State<\/code> \u679a\u4e3e\u8868\u793a\uff0c\u4e3b\u8981\u72b6\u6001\u5305\u62ec\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>NEW<\/strong>\uff1a\u65b0\u5efa\uff0c\u7ebf\u7a0b\u8fd8\u672a\u542f\u52a8<\/li>\n\n\n\n<li><strong>RUNNABLE<\/strong>\uff1a\u53ef\u8fd0\u884c\uff0c\u7ebf\u7a0b\u6b63\u5728\u8fd0\u884c\u6216\u7b49\u5f85 CPU \u65f6\u95f4\u7247<\/li>\n\n\n\n<li><strong>BLOCKED<\/strong>\uff1a\u963b\u585e\uff0c\u7b49\u5f85\u83b7\u53d6\u76d1\u89c6\u5668\u9501\uff08monitor lock\uff09<\/li>\n\n\n\n<li><strong>WAITING<\/strong>\uff1a\u65e0\u9650\u671f\u7b49\u5f85\uff0c\u7b49\u5f85\u53e6\u4e00\u4e2a\u7ebf\u7a0b\u663e\u5f0f\u901a\u77e5<\/li>\n\n\n\n<li><strong>TIMED_WAITING<\/strong>\uff1a\u9650\u65f6\u7b49\u5f85\uff0c\u7b49\u5f85\u6307\u5b9a\u65f6\u95f4\u540e\u81ea\u52a8\u5524\u9192<\/li>\n\n\n\n<li><strong>TERMINATED<\/strong>\uff1a\u7ec8\u6b62\uff0c\u7ebf\u7a0b\u6267\u884c\u5b8c\u6210\u6216\u9000\u51fa<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e8c\u3001BLOCKED \u548c WAITING \u7684\u8be6\u7ec6\u533a\u522b<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u7279\u6027<\/th><th>BLOCKED<\/th><th>WAITING<\/th><\/tr><\/thead><tbody><tr><td><strong>\u7b49\u5f85\u539f\u56e0<\/strong><\/td><td>\u7b49\u5f85\u8fdb\u5165\u540c\u6b65\u5757\u6216\u65b9\u6cd5\uff0c\u4e89\u62a2\u5bf9\u8c61\u9501<\/td><td>\u7b49\u5f85\u5176\u4ed6\u7ebf\u7a0b\u663e\u5f0f\u901a\u77e5\uff08\u5982 <code>wait()<\/code>\u3001<code>join()<\/code>\uff09<\/td><\/tr><tr><td><strong>\u89e6\u53d1\u65b9\u5f0f<\/strong><\/td><td>\u7ebf\u7a0b\u5c1d\u8bd5\u83b7\u5f97\u5bf9\u8c61\u9501\u5931\u8d25\u8fdb\u5165\u963b\u585e\u961f\u5217<\/td><td>\u7ebf\u7a0b\u8c03\u7528\u65e0\u8d85\u65f6\u7b49\u5f85\u65b9\u6cd5\u8fdb\u5165\u7b49\u5f85\u72b6\u6001<\/td><\/tr><tr><td><strong>\u6062\u590d\u6761\u4ef6<\/strong><\/td><td>\u83b7\u5f97\u9501\u540e\u6062\u590d\u8fd0\u884c<\/td><td>\u88ab\u5176\u4ed6\u7ebf\u7a0b <code>notify()<\/code> \u6216 <code>notifyAll()<\/code> \u5524\u9192\uff0c\u6216\u8c03\u7528 <code>interrupt()<\/code><\/td><\/tr><tr><td><strong>\u7b49\u5f85\u65f6\u95f4<\/strong><\/td><td>\u76f4\u5230\u83b7\u5f97\u9501\uff0c\u65f6\u95f4\u4e0d\u786e\u5b9a<\/td><td>\u65e0\u9650\u7b49\u5f85\uff0c\u76f4\u5230\u6536\u5230\u901a\u77e5\u6216\u4e2d\u65ad<\/td><\/tr><tr><td><strong>\u5178\u578b\u573a\u666f<\/strong><\/td><td>\u8fdb\u5165 <code>synchronized<\/code> \u4ee3\u7801\u5757\u6216\u65b9\u6cd5\u65f6\uff0c\u9501\u88ab\u5360\u7528<\/td><td>\u8c03\u7528 <code>Object.wait()<\/code> \u6216 <code>Thread.join()<\/code> \u7b49\u7b49\u5f85\u7ebf\u7a0b\u64cd\u4f5c<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e09\u3001\u5177\u4f53\u793a\u4f8b\u5bf9\u6bd4<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">1. <strong>BLOCKED \u793a\u4f8b<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>public class BlockedDemo {\n    private final Object lock = new Object();\n\n    public void method() {\n        synchronized (lock) {\n            try {\n                Thread.sleep(5000); \/\/ \u6a21\u62df\u5360\u7528\u95015\u79d2\n            } catch (InterruptedException e) {}\n        }\n    }\n\n    public static void main(String&#91;] args) {\n        BlockedDemo demo = new BlockedDemo();\n\n        new Thread(() -&gt; demo.method(), \"\u7ebf\u7a0b-1\").start();\n\n        new Thread(() -&gt; {\n            System.out.println(Thread.currentThread().getName() + \" \u5c1d\u8bd5\u83b7\u53d6\u9501\");\n            demo.method();  \/\/ \u8fd9\u91cc\u5982\u679c\u9501\u88ab\u5360\u7528\uff0c\u7ebf\u7a0b\u4f1a\u8fdb\u5165 BLOCKED \u72b6\u6001\n        }, \"\u7ebf\u7a0b-2\").start();\n    }\n}<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>\u7ebf\u7a0b-2<\/code> \u5728\u9501\u88ab <code>\u7ebf\u7a0b-1<\/code> \u5360\u7528\u65f6\u8fdb\u5165 <strong>BLOCKED<\/strong> \u72b6\u6001\uff0c\u7b49\u5f85\u9501\u91ca\u653e\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">2. <strong>WAITING \u793a\u4f8b<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>public class WaitingDemo {\n    public static void main(String&#91;] args) throws InterruptedException {\n        Object lock = new Object();\n\n        Thread t1 = new Thread(() -&gt; {\n            synchronized (lock) {\n                try {\n                    System.out.println(\"\u7ebf\u7a0b\u7b49\u5f85\u901a\u77e5...\");\n                    lock.wait();  \/\/ \u8fdb\u5165 WAITING \u72b6\u6001\uff0c\u7b49\u5f85\u901a\u77e5\n                    System.out.println(\"\u7ebf\u7a0b\u88ab\u5524\u9192\");\n                } catch (InterruptedException e) {\n                    System.out.println(\"\u7ebf\u7a0b\u88ab\u4e2d\u65ad\");\n                }\n            }\n        });\n\n        t1.start();\n\n        Thread.sleep(2000);\n\n        synchronized (lock) {\n            lock.notify();  \/\/ \u5524\u9192\u7b49\u5f85\u7ebf\u7a0b\n        }\n    }\n}<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u7ebf\u7a0b\u8c03\u7528 <code>wait()<\/code> \u540e\u8fdb\u5165 <strong>WAITING<\/strong> \u72b6\u6001\uff0c\u7b49\u5f85\u5176\u4ed6\u7ebf\u7a0b\u8c03\u7528 <code>notify()<\/code> \u5524\u9192\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u56db\u3001\u72b6\u6001\u8f6c\u6362\u6d41\u7a0b\u793a\u610f<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>RUNNABLE \n  |\n  |\uff08\u5c1d\u8bd5\u8fdb\u5165 synchronized \u4ee3\u7801\u5757\uff0c\u9501\u88ab\u5360\u7528\uff09\n  \u2193\nBLOCKED (\u7b49\u5f85\u9501)\n  |\n  |\uff08\u83b7\u5f97\u9501\uff09\n  \u2193\nRUNNABLE\n\n---\n\nRUNNABLE\n  |\n  |\uff08\u8c03\u7528 wait() \u6216 join() \u7b49\u65e0\u8d85\u65f6\u7b49\u5f85\u65b9\u6cd5\uff09\n  \u2193\nWAITING\n  |\n  |\uff08\u5176\u4ed6\u7ebf\u7a0b\u8c03\u7528 notify()\/interrupt()\uff09\n  \u2193\nRUNNABLE<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e94\u3001\u603b\u7ed3\u5bf9\u6bd4<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u65b9\u9762<\/th><th>BLOCKED<\/th><th>WAITING<\/th><\/tr><\/thead><tbody><tr><td>\u963b\u585e\u539f\u56e0<\/td><td>\u7ade\u4e89\u5bf9\u8c61\u9501<\/td><td>\u7b49\u5f85\u663e\u5f0f\u901a\u77e5<\/td><\/tr><tr><td>\u662f\u5426\u4f9d\u8d56\u9501<\/td><td>\u662f<\/td><td>\u5426<\/td><\/tr><tr><td>\u7b49\u5f85\u65f6\u957f<\/td><td>\u4e0d\u5b9a\uff0c\u76f4\u5230\u83b7\u5f97\u9501<\/td><td>\u4e0d\u5b9a\uff0c\u76f4\u5230\u901a\u77e5\u6216\u4e2d\u65ad<\/td><\/tr><tr><td>\u8fdb\u5165\u65b9\u5f0f<\/td><td>\u5c1d\u8bd5\u8fdb\u5165 <code>synchronized<\/code> \u5757\u65f6\u5931\u8d25<\/td><td>\u8c03\u7528 <code>wait()<\/code>\u3001<code>join()<\/code> \u7b49<\/td><\/tr><tr><td>\u5524\u9192\u65b9\u5f0f<\/td><td>\u83b7\u5f97\u9501\u5373\u6062\u590d<\/td><td>\u5176\u4ed6\u7ebf\u7a0b\u8c03\u7528 <code>notify()<\/code>\/<code>interrupt()<\/code><\/td><\/tr><tr><td>\u5178\u578b\u4f7f\u7528\u573a\u666f<\/td><td>\u540c\u6b65\u9501\u7ade\u4e89<\/td><td>\u7ebf\u7a0b\u95f4\u901a\u4fe1\u548c\u534f\u8c03<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u516d\u3001\u53c2\u8003\u8d44\u6599<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/docs.oracle.com\/javase\/8\/docs\/api\/java\/lang\/Thread.State.html\">Java\u5b98\u65b9\u6587\u6863 &#8211; Thread.State<\/a><\/li>\n\n\n\n<li>\u300aJava\u5e76\u53d1\u7f16\u7a0b\u5b9e\u6218\u300bChapter 3 \u2014 \u7ebf\u7a0b\u72b6\u6001\u4e0e\u540c\u6b65<\/li>\n\n\n\n<li><a href=\"https:\/\/docs.oracle.com\/javase\/tutorial\/essential\/concurrency\/states.html\">Oracle Java Tutorials &#8211; Thread States<\/a><\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u66f4\u591a\u8be6\u7ec6\u5185\u5bb9\u8bf7\u5173\u6ce8\u5176\u4ed6\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u7ebf\u7a0b\u72b6\u6001\u4e2d\uff0cBLOCKED \u548c WAITING \u662f\u4e24\u79cd\u4e0d\u540c\u7684\u963b\u585e\u72b6\u6001\uff0c\u5b83\u4eec\u7684\u533a\u522b\u4e3b\u8981\u4f53\u73b0\u5728\u7ebf\u7a0b\u963b\u585e\u7684\u539f\u56e0\u548c\u7b49\u5f85 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":3962,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[86,68],"tags":[],"class_list":["post-3961","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java-","category-java"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3961","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=3961"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3961\/revisions"}],"predecessor-version":[{"id":3964,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3961\/revisions\/3964"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media\/3962"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=3961"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=3961"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=3961"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}