{"id":203204,"date":"2025-05-22T12:34:39","date_gmt":"2025-05-22T04:34:39","guid":{"rendered":"https:\/\/server.hk\/cnblog\/203204\/"},"modified":"2025-05-22T12:34:39","modified_gmt":"2025-05-22T04:34:39","slug":"%e4%b8%ba%e4%bb%80%e4%b9%88%e4%bd%bf%e7%94%a8%e7%b1%bb%e9%94%81%e5%9c%a8%e5%a4%9a%e7%ba%bf%e7%a8%8b%e7%bc%96%e7%a8%8b%e4%b8%ad%e4%bc%9a%e5%af%bc%e8%87%b4%e9%94%99%e8%af%af%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/203204\/","title":{"rendered":"\u4e3a\u4ec0\u4e48\u4f7f\u7528\u7c7b\u9501\u5728\u591a\u7ebf\u7a0b\u7f16\u7a0b\u4e2d\u4f1a\u5bfc\u81f4\u9519\u8bef\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>\u4e3a\u4ec0\u4e48\u4f7f\u7528\u7c7b\u9501\u5728\u591a\u7ebf\u7a0b\u7f16\u7a0b\u4e2d\u4f1a\u5bfc\u81f4\u9519\u8bef\uff1f<\/h1>\n<p><span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span>    <\/p>\n<p>\u6b22\u8fce\u5404\u4f4d\u5c0f\u4f19\u4f34\u6765\u5230\uff0c\u76f8\u805a\u4e8e\u6b64\u90fd\u662f\u7f18\u54c8\u54c8\u54c8\uff01\u4eca\u5929\u6211\u7ed9\u5927\u5bb6\u5e26\u6765<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u4e3a\u4ec0\u4e48\u4f7f\u7528\u7c7b\u9501\u5728\u591a\u7ebf\u7a0b\u7f16\u7a0b\u4e2d\u4f1a\u5bfc\u81f4\u9519\u8bef\uff1f\u300b<\/span>\uff0c\u8fd9\u7bc7\u6587\u7ae0\u4e3b\u8981\u8bb2\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\uff0c\u5982\u679c\u4f60\u5bf9<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\u76f8\u5173\u7684\u77e5\u8bc6\u975e\u5e38\u611f\u5174\u8da3\u6216\u8005\u6b63\u5728\u81ea\u5b66\uff0c\u90fd\u53ef\u4ee5\u5173\u6ce8\u6211\uff0c\u6211\u4f1a\u6301\u7eed\u66f4\u65b0\u76f8\u5173\u6587\u7ae0\uff01\u5f53\u7136\uff0c\u6709\u4ec0\u4e48\u5efa\u8bae\u4e5f\u6b22\u8fce\u5728\u8bc4\u8bba\u7559\u8a00\u63d0\u51fa\uff01\u4e00\u8d77\u5b66\u4e60\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241101\/1730451652672498c49c9ef.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>\u7ebf\u7a0b\u95f4\u901a\u4fe1\uff1a\u4f7f\u7528\u5bf9\u8c61\u9501\u4e0e\u7c7b\u9501\u7684\u5bf9\u6bd4<\/strong><\/p>\n<p>\u5728\u591a\u7ebf\u7a0b\u7f16\u7a0b\u4e2d\uff0c\u7ebf\u7a0b\u95f4\u901a\u4fe1\u662f\u4e00\u4e2a\u81f3\u5173\u91cd\u8981\u7684\u6982\u5ff5\u3002\u4e00\u4e2a\u5e38\u89c1\u7684\u673a\u5236\u662f\u4f7f\u7528\u5bf9\u8c61\u9501\uff0c\u800c\u53e6\u4e00\u4e2a\u9009\u9879\u662f\u4f7f\u7528\u7c7b\u9501\u3002<\/p>\n<p>\u5728\u63d0\u4f9b\u7684\u95ee\u9898\u793a\u4f8b\u4e2d\uff0cprinter_1\u7c7b\u4f7f\u7528\u5bf9\u8c61\u9501\u5b9e\u73b0\u4e86\u7ebf\u7a0b\u901a\u4fe1\u3002\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u7ebf\u7a0b\u5b89\u5168\u5730\u6253\u5370\u4ece 1 \u5230 100 \u7684\u6570\u5b57\u3002\u4ee3\u7801\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre>public class threadtalk {\n    public static void main(string[] args) {\n        printer_1 printer = new printer_1();\n        thread t1 = new thread(printer);\n        thread t2 = new thread(printer);\n        t1.setname(\"\u7ebf\u7a0b\u4e00\");\n        t2.setname(\"\u7ebf\u7a0b\u4e8c\");\n        t1.start();\n        t2.start();\n    }\n}\n\nclass printer_1 implements runnable {\n    private int number = 0;\n\n    @override\n    public void run() {\n        while (true) {\n            synchronized (printer_1.class) {\n                notify();\n                if (number &lt;= 100) {\n                    system.out.println(thread.currentthread().getname() + \"\u6253\u5370\u6570\u5b57\uff1a\" + number);\n                    number++;\n                    try {\n                        wait();\n                    } catch (interruptedexception e) {\n                        e.printstacktrace();\n                    }\n                } else {\n                    break;\n                }\n            }\n        }\n    }\n}<\/pre>\n<p>\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u4f7f\u7528\u5bf9\u8c61\u9501\u662f\u6b63\u786e\u7684\u505a\u6cd5\uff0c\u56e0\u4e3a\u5b83\u786e\u4fdd\u4e86\u6bcf\u4e2a\u7ebf\u7a0b\u53ea\u8bbf\u95ee\u81ea\u5df1\u7684\u6253\u5370\u673a\u5b9e\u4f8b\u3002<\/p>\n<p><strong>\u4e3a\u4ec0\u4e48\u4f7f\u7528\u7c7b\u9501\u4f1a\u62a5\u9519\uff1f<\/strong><\/p>\n<p>\u7136\u800c\uff0c\u95ee\u9898\u793a\u4f8b\u4e2d\u6307\u51fa\uff0c\u4f7f\u7528\u7c7b\u9501\u4f1a\u5bfc\u81f4\u9519\u8bef\u3002\u539f\u56e0\u5982\u4e0b\uff1a<\/p>\n<p>\u5f53\u4f7f\u7528 synchronized (printer_1.class) \u65f6\uff0c\u7c7b\u9501\u88ab\u6301\u6709\u3002\u4f46\u662f\uff0c\u5728 run() \u65b9\u6cd5\u5185\u90e8\uff0c\u7ebf\u7a0b\u5374\u5728\u4f7f\u7528 notify() \u548c wait() \u65b9\u6cd5\uff0c\u8fd9\u4e24\u4e2a\u65b9\u6cd5\u53ea\u80fd\u5728\u6301\u6709\u5bf9\u8c61\u9501\u65f6\u4f7f\u7528\u3002\u5728\u4f7f\u7528\u7c7b\u9501\u65f6\uff0c\u6301\u6709\u7684\u662f\u7c7b\u7684\u9501\uff0c\u800c\u4e0d\u662f\u5bf9\u8c61\u9501\u3002\u56e0\u6b64\uff0c\u5f53\u4e00\u4e2a\u7ebf\u7a0b\u8c03\u7528 wait() \u65f6\uff0c\u7c7b\u9501\u4f1a\u88ab\u91ca\u653e\uff0c\u800c\u5f53\u53e6\u4e00\u4e2a\u7ebf\u7a0b\u8bd5\u56fe\u8c03\u7528 notify() \u65f6\uff0c\u5c31\u4f1a\u629b\u51fa illegalmonitorstateexception \u9519\u8bef\u3002<\/p>\n<p><strong>\u89e3\u51b3\u65b9\u6848<\/strong><\/p>\n<p>\u8981\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u60a8\u5e94\u8be5\u5728\u4f7f\u7528 wait() \u548c notify() \u65b9\u6cd5\u65f6\u6301\u6709\u5bf9\u8c61\u9501\u3002\u5728\u7ed9\u51fa\u7684\u793a\u4f8b\u4e2d\uff0c\u6b63\u786e\u7684\u505a\u6cd5\u662f\uff1a<\/p>\n<pre>synchronized (this) {\n    ... your code ...\n}<\/pre>\n<p>\u6216\u8005\uff0c\u4e5f\u53ef\u4ee5\u4f7f\u7528\u9759\u6001\u65b9\u6cd5\uff1a<\/p>\n<pre>synchronized (Printer_1.class) {\n    Printer_1.class.wait();\n    Printer_1.class.notify();\n}<\/pre>\n<p>\u597d\u4e86\uff0c\u672c\u6587\u5230\u6b64\u7ed3\u675f\uff0c\u5e26\u5927\u5bb6\u4e86\u89e3\u4e86\u300a\u4e3a\u4ec0\u4e48\u4f7f\u7528\u7c7b\u9501\u5728\u591a\u7ebf\u7a0b\u7f16\u7a0b\u4e2d\u4f1a\u5bfc\u81f4\u9519\u8bef\uff1f\u300b\uff0c\u5e0c\u671b\u672c\u6587\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff01\u5173\u6ce8\u516c\u4f17\u53f7\uff0c\u7ed9\u5927\u5bb6\u5206\u4eab\u66f4\u591a\u6587\u7ae0\u77e5\u8bc6\uff01<\/p>\n<p>      \u7248\u672c\u58f0\u660e \u672c\u6587\u8f6c\u8f7d\u4e8e\uff1aphp \u5982\u6709\u4fb5\u72af\uff0c\u8bf7\u8054\u7cfb\u5220\u9664<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e3a\u4ec0\u4e48\u4f7f\u7528\u7c7b\u9501\u5728\u591a\u7ebf\u7a0b\u7f16\u7a0b\u4e2d\u4f1a\u5bfc&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-203204","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/203204","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=203204"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/203204\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=203204"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=203204"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=203204"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}