{"id":203822,"date":"2025-05-22T15:12:31","date_gmt":"2025-05-22T07:12:31","guid":{"rendered":"https:\/\/server.hk\/cnblog\/203822\/"},"modified":"2025-05-22T15:12:31","modified_gmt":"2025-05-22T07:12:31","slug":"%e4%b8%bb%e7%ba%bf%e7%a8%8b%e5%92%8c%e5%ad%90%e7%ba%bf%e7%a8%8b%e4%ba%a4%e6%9b%bf%e6%89%a7%e8%a1%8c%ef%bc%8c%e4%bb%a3%e7%a0%81%e4%b8%ba%e4%bd%95%e6%97%a0%e6%b3%95%e6%ad%a3%e5%b8%b8%e8%bf%90%e8%a1%8c","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/203822\/","title":{"rendered":"\u4e3b\u7ebf\u7a0b\u548c\u5b50\u7ebf\u7a0b\u4ea4\u66ff\u6267\u884c\uff0c\u4ee3\u7801\u4e3a\u4f55\u65e0\u6cd5\u6b63\u5e38\u8fd0\u884c\u4ee5\u53ca\u5982\u4f55\u89e3\u51b3\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>\u4e3b\u7ebf\u7a0b\u548c\u5b50\u7ebf\u7a0b\u4ea4\u66ff\u6267\u884c\uff0c\u4ee3\u7801\u4e3a\u4f55\u65e0\u6cd5\u6b63\u5e38\u8fd0\u884c\u4ee5\u53ca\u5982\u4f55\u89e3\u51b3\uff1f<\/h1>\n<p>\u4f60\u5728\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\u76f8\u5173\u7684\u77e5\u8bc6\u5417\uff1f\u672c\u6587<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u4e3b\u7ebf\u7a0b\u548c\u5b50\u7ebf\u7a0b\u4ea4\u66ff\u6267\u884c\uff0c\u4ee3\u7801\u4e3a\u4f55\u65e0\u6cd5\u6b63\u5e38\u8fd0\u884c\u4ee5\u53ca\u5982\u4f55\u89e3\u51b3\uff1f\u300b<\/span>\uff0c\u4e3b\u8981\u4ecb\u7ecd\u7684\u5185\u5bb9\u5c31\u6d89\u53ca\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\uff0c\u5982\u679c\u4f60\u60f3\u63d0\u5347\u81ea\u5df1\u7684\u5f00\u53d1\u80fd\u529b\uff0c\u5c31\u4e0d\u8981\u9519\u8fc7\u8fd9\u7bc7\u6587\u7ae0\uff0c\u5927\u5bb6\u8981\u77e5\u9053\u7f16\u7a0b\u7406\u8bba\u57fa\u7840\u548c\u5b9e\u6218\u64cd\u4f5c\u90fd\u662f\u4e0d\u53ef\u6216\u7f3a\u7684\u54e6\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241126\/17326082686745810c3dd27.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>\u4e3b\u7ebf\u7a0b\u548c\u5b50\u7ebf\u7a0b\u4ea4\u66ff\u6267\u884c\u7684\u7ebf\u7a0b\u540c\u6b65<\/strong><\/p>\n<p>\u672c\u95ee\u9898\u8981\u6c42\u5b9e\u73b0\u4e3b\u7ebf\u7a0b\u548c\u5b50\u7ebf\u7a0b\u4ea4\u66ff\u6267\u884c100\u6b21\u548c10\u6b21\u7684\u5faa\u73af\u3002\u63d0\u4f9b\u7684\u4ee3\u7801\u4f7f\u7528 synchronized \u548c wait()\/notify() \u5b9e\u73b0\u7ebf\u7a0b\u540c\u6b65\uff0c\u4f46\u5b58\u5728\u4e00\u4e9b\u95ee\u9898\u5bfc\u81f4\u7a0b\u5e8f\u65e0\u6cd5\u6b63\u5e38\u8fd0\u884c\u3002<\/p>\n<p><strong>\u95ee\u9898\u539f\u56e0\uff1a<\/strong><\/p>\n<ol>\n<li>\u4e3b\u7ebf\u7a0b\u7684 wait() \u65b9\u6cd5\u653e\u5728\u4e86 finally \u5757\u4e2d\uff0c\u5bfc\u81f4 wait() \u65b9\u6cd5\u65e0\u6cd5\u5728 notify() \u65b9\u6cd5\u8c03\u7528\u540e\u91ca\u653e\u9501\u3002<\/li>\n<li>\u5b50\u7ebf\u7a0b\u7684 wait() \u65b9\u6cd5\u7684\u9501\u5bf9\u8c61\u4e0d\u662f me \u800c\u662f this\uff0c\u5bfc\u81f4\u5b50\u7ebf\u7a0b\u65e0\u6cd5\u88ab\u4e3b\u7ebf\u7a0b\u7684 notify() \u65b9\u6cd5\u5524\u9192\u3002<\/li>\n<\/ol>\n<p><strong>\u4fee\u6539\u540e\u7684\u4ee3\u7801\uff1a<\/strong><\/p>\n<pre>public class Test_02 {\n    public static void main(String[] args) {\n        MeThread me = new MeThread();\n        Thread t = new Thread(me);\n        t.start();\n\n        boolean flag = true;\n        int i = 0;  \/\/ \u4fee\u6539\u4e86\u521d\u59cb\u503c\n        synchronized (me) {\n            while (flag) {\n                System.out.println(Thread.currentThread().getName() + i);\n                i++;\n                if (i % 100 == 0) {\n                    try {\n                        \/\/ \u65b9\u4fbf\u89c2\u5bdf\u6548\u679c,\u7761\u77201s\n                        Thread.sleep(1000);\n                        System.out.println(\"main============\");\n                        \/\/ \u4ece finally \u4ee3\u7801\u5757\u4e2d\u524d\u79fb\u5230 wait() \u65b9\u6cd5\u524d\n                        me.notify();\n                        \/\/ \u9501\u5bf9\u8c61\u4e3a me\n                        me.wait();\n                    } catch (InterruptedException e) {\n                        e.printStackTrace();\n                    }\n                }\n            }\n        }\n    }\n}\n\nclass MeThread implements Runnable {\n    @Override\n    public synchronized void run() {\n        int i = 0;\n        boolean flag = true;\n        while (flag) {\n            System.out.println(Thread.currentThread().getName() + i);\n            i++;\n            if (i % 10 == 0) {\n                try {\n                    \/\/ \u65b9\u4fbf\u89c2\u5bdf\n                    Thread.sleep(1000);\n                    System.out.println(\"MeThread============\");\n                    \/\/ \u540c\u6b65\u65b9\u6cd5\u9501\u5bf9\u8c61\u4e3a this, notify() \u79fb\u52a8\u5230 wait() \u65b9\u6cd5\u524d\n                    this.notify();\n                    \/\/ \u540c\u6b65\u65b9\u6cd5\u9501\u5bf9\u8c61\u4e3a this\n                    this.wait();\n                } catch (InterruptedException e) {\n                    e.printStackTrace();\n                }\n            }\n        }\n    }\n}<\/pre>\n<p><strong>\u4fee\u6539\u8bf4\u660e\uff1a<\/strong><\/p>\n<ol>\n<li>\u5c06\u4e3b\u7ebf\u7a0b\u7684 wait() \u65b9\u6cd5\u79fb\u52a8\u5230 notify() \u65b9\u6cd5\u4e4b\u540e\u3002<\/li>\n<li>\u540c\u6b65\u65b9\u6cd5 run() \u7684\u9501\u5bf9\u8c61\u6539\u4e3a this\uff0c\u786e\u4fdd\u4e3b\u7ebf\u7a0b\u548c\u5b50\u7ebf\u7a0b\u4f7f\u7528\u76f8\u540c\u7684\u9501\u3002<\/li>\n<\/ol>\n<p>\u4eca\u5929\u5e26\u5927\u5bb6\u4e86\u89e3\u4e86\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u5e0c\u671b\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff1b\u5173\u4e8e\u6587\u7ae0\u7684\u6280\u672f\u77e5\u8bc6\u6211\u4eec\u4f1a\u4e00\u70b9\u70b9\u6df1\u5165\u4ecb\u7ecd\uff0c\u6b22\u8fce\u5927\u5bb6\u5173\u6ce8\u516c\u4f17\u53f7\uff0c\u4e00\u8d77\u5b66\u4e60\u7f16\u7a0b~<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e3b\u7ebf\u7a0b\u548c\u5b50\u7ebf\u7a0b\u4ea4\u66ff\u6267\u884c\uff0c\u4ee3\u7801\u4e3a\u4f55&#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-203822","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/203822","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=203822"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/203822\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=203822"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=203822"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=203822"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}