{"id":3886,"date":"2025-07-13T11:36:03","date_gmt":"2025-07-13T03:36:03","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=3886"},"modified":"2025-07-13T11:36:03","modified_gmt":"2025-07-13T03:36:03","slug":"wait-%e5%92%8c-notify-%e4%b8%ba%e4%bb%80%e4%b9%88%e5%bf%85%e9%a1%bb%e5%9c%a8-synchronized-%e4%bb%a3%e7%a0%81%e5%9d%97%e4%b8%ad%e4%bd%bf%e7%94%a8","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/07\/13\/wait-%e5%92%8c-notify-%e4%b8%ba%e4%bb%80%e4%b9%88%e5%bf%85%e9%a1%bb%e5%9c%a8-synchronized-%e4%bb%a3%e7%a0%81%e5%9d%97%e4%b8%ad%e4%bd%bf%e7%94%a8\/","title":{"rendered":"wait()\u00a0\u548c\u00a0notify()\u00a0\u4e3a\u4ec0\u4e48\u5fc5\u987b\u5728\u00a0synchronized\u00a0\u4ee3\u7801\u5757\u4e2d\u4f7f\u7528"},"content":{"rendered":"\n<p><code>wait()<\/code> \u548c <code>notify()<\/code> \u5fc5\u987b\u5728 <code>synchronized<\/code> \u4ee3\u7801\u5757\u4e2d\u4f7f\u7528\uff0c\u8fd9\u662f Java \u591a\u7ebf\u7a0b\u8bbe\u8ba1\u4e2d\u7684\u4e00\u4e2a<strong>\u5f3a\u5236\u89c4\u5b9a<\/strong>\uff0c\u5176\u539f\u56e0\u6d89\u53ca\u5230 Java \u5bf9\u5bf9\u8c61\u76d1\u89c6\u5668\uff08monitor\uff09\u7684\u4f7f\u7528\u673a\u5236\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">1. \u80cc\u666f\u77e5\u8bc6\uff1a\u5bf9\u8c61\u76d1\u89c6\u5668\u662f\u4ec0\u4e48\uff1f<\/h2>\n\n\n\n<p>Java \u4e2d\u7684\u6bcf\u4e00\u4e2a\u5bf9\u8c61\u90fd\u6709\u4e00\u4e2a<strong>\u76d1\u89c6\u5668\u9501\uff08monitor\uff09<\/strong>\uff0c\u4e5f\u53eb\u201c\u5185\u7f6e\u9501\u201d\u6216\u201c\u5bf9\u8c61\u9501\u201d\u3002\u8fd9\u4e2a\u9501\u53ef\u4ee5\u88ab <code>synchronized<\/code> \u83b7\u53d6\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>wait()<\/code> \u4f1a\u8ba9\u5f53\u524d\u7ebf\u7a0b<strong>\u91ca\u653e\u76d1\u89c6\u5668\u9501<\/strong>\uff0c\u5e76\u8fdb\u5165<strong>\u7b49\u5f85\u961f\u5217<\/strong>\u3002<\/li>\n\n\n\n<li><code>notify()<\/code> \u4f1a\u8ba9\u67d0\u4e2a\u7b49\u5f85\u961f\u5217\u4e2d\u7684\u7ebf\u7a0b<strong>\u91cd\u65b0\u83b7\u5f97\u9501\u7684\u7ade\u4e89\u6743<\/strong>\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\">2. \u4e3a\u4ec0\u4e48\u5fc5\u987b\u5728 <code>synchronized<\/code> \u4ee3\u7801\u5757\u4e2d\uff1f<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\u539f\u56e0\u4e00\uff1a<code>wait()<\/code> \u548c <code>notify()<\/code> \u4f9d\u8d56\u5bf9\u8c61\u7684\u76d1\u89c6\u5668<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>wait()<\/code>\u3001<code>notify()<\/code>\u3001<code>notifyAll()<\/code> \u65b9\u6cd5\u662f <code>Object<\/code> \u7c7b\u7684\u65b9\u6cd5\uff0c\u800c\u4e0d\u662f <code>Thread<\/code>\u3002<\/li>\n\n\n\n<li>\u5b83\u4eec\u5fc5\u987b\u5728<strong>\u5f53\u524d\u7ebf\u7a0b\u5df2\u7ecf\u6301\u6709\u8be5\u5bf9\u8c61\u7684\u76d1\u89c6\u5668\u9501<\/strong>\u65f6\u8c03\u7528\uff08\u4e5f\u5c31\u662f\u5728 <code>synchronized(obj)<\/code> \u4ee3\u7801\u5757\u4e2d\uff09\uff0c\u5426\u5219\u629b\u51fa <code>IllegalMonitorStateException<\/code> \u5f02\u5e38\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">\u539f\u56e0\u4e8c\uff1a\u4fdd\u8bc1\u7ebf\u7a0b\u5b89\u5168\u548c\u72b6\u6001\u4e00\u81f4\u6027<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5982\u679c\u5728\u6ca1\u6709\u540c\u6b65\u7684\u60c5\u51b5\u4e0b\u8c03\u7528 <code>wait()<\/code> \u548c <code>notify()<\/code>\uff0c\u53ef\u80fd\u4f1a\u5bfc\u81f4\uff1a<\/li>\n\n\n\n<li>\u7ebf\u7a0b\u72b6\u6001\u6df7\u4e71\uff08\u6bd4\u5982\u4e00\u4e2a\u7ebf\u7a0b\u521a\u8fdb\u5165\u7b49\u5f85\uff0c\u4f46\u8fd8\u6ca1\u52a0\u5165\u7b49\u5f85\u961f\u5217\uff0c\u53e6\u4e00\u4e2a\u7ebf\u7a0b\u5c31\u8c03\u7528\u4e86 <code>notify()<\/code>\uff0c\u4f1a\u9519\u8fc7\u901a\u77e5\uff09\u3002<\/li>\n\n\n\n<li>\u6570\u636e\u7ade\u6001\uff08\u591a\u4e2a\u7ebf\u7a0b\u8bbf\u95ee\u5171\u4eab\u6570\u636e\u800c\u6ca1\u6709\u540c\u6b65\u4fdd\u62a4\uff09\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\">3. \u5f3a\u5236\u89c4\u5b9a\u7684\u4f53\u73b0<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>Object lock = new Object();\n\nlock.wait(); \/\/ \u274c \u7f16\u8bd1\u901a\u8fc7\uff0c\u8fd0\u884c\u65f6\u629b IllegalMonitorStateException<\/code><\/pre>\n\n\n\n<p>\u5fc5\u987b\u8fd9\u6837\u5199\u624d\u5408\u6cd5\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>synchronized (lock) {\n    lock.wait();  \/\/ \u2705 \u5408\u6cd5\uff0c\u7ebf\u7a0b\u5f53\u524d\u62e5\u6709lock\u5bf9\u8c61\u7684\u76d1\u89c6\u5668\n}<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">4. \u4e3e\u4e2a\u5b8c\u6574\u4f8b\u5b50<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>class WaitNotifyExample {\n    private static final Object lock = new Object();\n\n    public static void main(String&#91;] args) {\n        Thread waitingThread = new Thread(() -&gt; {\n            synchronized (lock) {\n                try {\n                    System.out.println(\"\u7ebf\u7a0bA\u7b49\u5f85...\");\n                    lock.wait();\n                    System.out.println(\"\u7ebf\u7a0bA\u6536\u5230\u901a\u77e5\");\n                } catch (InterruptedException e) {\n                    Thread.currentThread().interrupt();\n                }\n            }\n        });\n\n        Thread notifyingThread = new Thread(() -&gt; {\n            synchronized (lock) {\n                System.out.println(\"\u7ebf\u7a0bB\u901a\u77e5...\");\n                lock.notify();\n            }\n        });\n\n        waitingThread.start();\n        try { Thread.sleep(100); } catch (InterruptedException e) {}\n        notifyingThread.start();\n    }\n}<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">5. \u603b\u7ed3\uff1a\u4e3a\u4ec0\u4e48\u8981\u5728 synchronized \u4e2d\u8c03\u7528\uff1f<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u65b9\u6cd5<\/th><th>\u662f\u5426\u5fc5\u987b\u5728 <code>synchronized<\/code> \u4e2d<\/th><th>\u539f\u56e0<\/th><\/tr><\/thead><tbody><tr><td><code>wait()<\/code><\/td><td>\u662f<\/td><td>\u91ca\u653e\u9501\u5e76\u8fdb\u5165\u7b49\u5f85\uff0c\u9700\u8981\u62e5\u6709\u5bf9\u8c61\u9501<\/td><\/tr><tr><td><code>notify()<\/code><\/td><td>\u662f<\/td><td>\u901a\u77e5\u7b49\u5f85\u7ebf\u7a0b\uff0c\u9700\u8981\u62e5\u6709\u5bf9\u8c61\u9501<\/td><\/tr><tr><td><code>notifyAll()<\/code><\/td><td>\u662f<\/td><td>\u540c\u4e0a\uff0c\u901a\u77e5\u6240\u6709\u7b49\u5f85\u8be5\u5bf9\u8c61\u9501\u7684\u7ebf\u7a0b<\/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\">6. \u5b98\u65b9\u8bf4\u660e\u4e0e\u8d44\u6599<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\ud83d\udd17\u00a0<a class=\"\" href=\"https:\/\/docs.oracle.com\/javase\/8\/docs\/api\/java\/lang\/Object.html#wait--\">Java \u5b98\u65b9 API \u6587\u6863\uff1aObject.wait()<\/a><\/li>\n\n\n\n<li>\ud83d\udd17\u00a0<a class=\"\" href=\"https:\/\/docs.oracle.com\/javase\/specs\/jls\/se8\/html\/jls-17.html#jls-17.2\">JLS (Java Language Specification) on Synchronization<\/a><\/li>\n\n\n\n<li>\ud83d\udcd8 \u300aJava \u5e76\u53d1\u7f16\u7a0b\u5b9e\u6218\u300b\u7b2c 14 \u7ae0\uff1a\u6784\u5efa\u81ea\u5b9a\u4e49\u540c\u6b65\u5de5\u5177<\/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\">\u603b\u7ed3\u53e3\u8bc0<\/h2>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p><strong>\u5bf9\u8c61\u76d1\u89c6\u5668\uff0c\u9501\u5185\u624d\u53ef\u64cd\uff1bwait \u91ca\u653e\u9501\uff0cnotify \u5524\u4eba\u8dd1\u3002<\/strong><\/p>\n\n\n\n<p>\u66f4\u591a\u8be6\u7ec6\u5185\u5bb9\u8bf7\u5173\u6ce8\u5176\u4ed6\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n<\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>wait() \u548c notify() \u5fc5\u987b\u5728 synchronized \u4ee3\u7801\u5757\u4e2d\u4f7f\u7528\uff0c\u8fd9\u662f Java \u591a\u7ebf\u7a0b\u8bbe\u8ba1 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":3887,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[86],"tags":[],"class_list":["post-3886","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java-"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3886","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=3886"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3886\/revisions"}],"predecessor-version":[{"id":3888,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3886\/revisions\/3888"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media\/3887"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=3886"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=3886"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=3886"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}