{"id":203361,"date":"2025-05-22T13:14:53","date_gmt":"2025-05-22T05:14:53","guid":{"rendered":"https:\/\/server.hk\/cnblog\/203361\/"},"modified":"2025-05-22T13:14:53","modified_gmt":"2025-05-22T05:14:53","slug":"java-%e7%ba%bf%e7%a8%8b%e5%90%8c%e6%ad%a5%ef%bc%9a%e5%a6%82%e4%bd%95%e8%a7%a3%e5%86%b3%e4%b8%bb%e7%ba%bf%e7%a8%8b%e5%92%8c%e5%ad%90%e7%ba%bf%e7%a8%8b%e6%97%a0%e6%b3%95%e4%ba%a4%e6%9b%bf%e6%89%a7","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/203361\/","title":{"rendered":"Java \u7ebf\u7a0b\u540c\u6b65\uff1a\u5982\u4f55\u89e3\u51b3\u4e3b\u7ebf\u7a0b\u548c\u5b50\u7ebf\u7a0b\u65e0\u6cd5\u4ea4\u66ff\u6267\u884c\u7684\u95ee\u9898\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>Java \u7ebf\u7a0b\u540c\u6b65\uff1a\u5982\u4f55\u89e3\u51b3\u4e3b\u7ebf\u7a0b\u548c\u5b50\u7ebf\u7a0b\u65e0\u6cd5\u4ea4\u66ff\u6267\u884c\u7684\u95ee\u9898\uff1f<\/h1>\n<p>\u4eca\u65e5\u4e0d\u80af\u57cb\u5934\uff0c\u660e\u65e5\u4f55\u4ee5\u62ac\u5934\uff01\u6bcf\u65e5\u4e00\u53e5\u52aa\u529b\u81ea\u5df1\u7684\u8bdd\u54c8\u54c8~\u54c8\u55bd\uff0c\u4eca\u5929\u6211\u5c06\u7ed9\u5927\u5bb6\u5e26\u6765\u4e00\u7bc7<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aJava \u7ebf\u7a0b\u540c\u6b65\uff1a\u5982\u4f55\u89e3\u51b3\u4e3b\u7ebf\u7a0b\u548c\u5b50\u7ebf\u7a0b\u65e0\u6cd5\u4ea4\u66ff\u6267\u884c\u7684\u95ee\u9898\uff1f\u300b<\/span>\uff0c\u4e3b\u8981\u5185\u5bb9\u662f\u8bb2\u89e3<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\uff0c\u611f\u5174\u8da3\u7684\u670b\u53cb\u53ef\u4ee5\u6536\u85cf\u6216\u8005\u6709\u66f4\u597d\u7684\u5efa\u8bae\u5728\u8bc4\u8bba\u63d0\u51fa\uff0c\u6211\u90fd\u4f1a\u8ba4\u771f\u770b\u7684\uff01\u5927\u5bb6\u4e00\u8d77\u8fdb\u6b65\uff0c\u4e00\u8d77\u5b66\u4e60\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241106\/1730870142672afb7e818b2.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>\u7ebf\u7a0b\u540c\u6b65\u3001synchronized \u548c\u9501<\/strong><\/p>\n<p>\u8fd9\u7bc7\u95ee\u7b54\u6587\u7ae0\u63a2\u8ba8\u4e86 java \u4e2d\u7ebf\u7a0b\u540c\u6b65\u548c\u540c\u6b65\u673a\u5236\u7684\u5b9e\u73b0\uff0c\u91cd\u70b9\u4ecb\u7ecd\u4e86 synchronized \u5173\u952e\u5b57\u548c\u9501\u7684\u4f7f\u7528\u3002<\/p>\n<p>\u5728\u7ed9\u51fa\u7684\u4ee3\u7801\u4e2d\uff0c\u4e3b\u7ebf\u7a0b\u548c\u5b50\u7ebf\u7a0b\u4ea4\u66ff\u6267\u884c\uff0c\u6bcf\u6267\u884c 100 \u6216 10 \u6b21\u540e\u901a\u77e5\u53e6\u4e00\u4e2a\u7ebf\u7a0b\u7ee7\u7eed\u6267\u884c\u3002<\/p>\n<p><strong>\u95ee\u9898\uff1a<\/strong><\/p>\n<p>\u4ee3\u7801\u5b58\u5728\u4ee5\u4e0b\u95ee\u9898\uff1a<\/p>\n<ul>\n<li>\u4e3b\u7ebf\u7a0b\u548c\u5b50\u7ebf\u7a0b\u65e0\u6cd5\u4ea4\u66ff\u6267\u884c\uff0c\u800c\u662f\u4e00\u76f4\u7531\u4e3b\u7ebf\u7a0b\u6267\u884c\u3002<\/li>\n<li>notify() \u65b9\u6cd5\u653e\u5728\u4e86 finally \u4ee3\u7801\u5757\u4e2d\uff0c\u53ef\u80fd\u65e0\u6cd5\u6b63\u786e\u5524\u9192\u53e6\u4e00\u4e2a\u7ebf\u7a0b\u3002<\/li>\n<\/ul>\n<p><strong>\u89e3\u51b3\u65b9\u6848\uff1a<\/strong><\/p>\n<p>\u5c06\u95ee\u9898\u4ee3\u7801\u4fee\u6539\u5982\u4e0b\uff1a<\/p>\n<pre>public static void main(string[] args) {\n\n    methread me = new methread();\n    thread t = new thread(me);\n    t.start();\n\n    boolean flag = true;\n    \/\/\u4fee\u6539\u4e86\u521d\u59cb\u503c\n    int i = 0;\n\n    synchronized (me) {\n        while (flag) {\n            system.out.println(thread.currentthread().getname() + i);\n            i++;\n\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                    \/\/\u4ecefinally\u4ee3\u7801\u5757\u4e2d\u524d\u79fb\u5230wait()\u65b9\u6cd5\u524d\n                    me.notify();\n                    \/\/\u9501\u5bf9\u8c61\u4e3ame\n                    me.wait();\n                } catch (interruptedexception e) {\n                    e.printstacktrace();\n                }\n            }\n        }\n    }\n}<\/pre>\n<pre>class MeThread implements Runnable {\n    @Override\n    public synchronized void run() {\n        int i = 0;\n        boolean flag = true;\n\n        while (flag) {\n            System.out.println(Thread.currentThread().getName() + i);\n            i++;\n\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\u4e3athis,notify()\u79fb\u52a8\u5230wait()\u65b9\u6cd5\u524d\n                    this.notify();\n                    \/\/\u540c\u6b65\u65b9\u6cd5\u9501\u5bf9\u8c61\u4e3athis\n                    this.wait();\n                } catch (InterruptedException e) {\n                    e.printStackTrace();\n                }\n            }\n        }\n    }\n}<\/pre>\n<p>\u8fd9\u4e9b\u4fee\u6539\u89e3\u51b3\u4e86\u539f\u6709\u4ee3\u7801\u4e2d\u7684\u95ee\u9898\uff1a<\/p>\n<ul>\n<li>\u521d\u59cb\u5316 i \u7684\u503c\u4e3a 0\uff0c\u786e\u4fdd\u4e24\u4e2a\u7ebf\u7a0b\u4ece\u5934\u5f00\u59cb\u6267\u884c\u3002<\/li>\n<li>\u5c06 notify() \u65b9\u6cd5\u4ece finally \u4ee3\u7801\u5757\u79fb\u81f3 wait() \u65b9\u6cd5\u4e4b\u524d\uff0c\u786e\u4fdd\u5728 wait() \u4e4b\u524d\u5524\u9192\u53e6\u4e00\u4e2a\u7ebf\u7a0b\u3002<\/li>\n<li>\u5728 synchronized \u5757\u4e2d\u4f7f\u7528 me \u4f5c\u4e3a\u9501\u5bf9\u8c61\uff0c\u4ee5\u6b63\u786e\u540c\u6b65\u4e24\u4e2a\u7ebf\u7a0b\u5bf9 i \u7684\u8bbf\u95ee\u3002<\/li>\n<li>\u5728 methread \u7684 run() \u65b9\u6cd5\u4e2d\u4f7f\u7528 this \u4f5c\u4e3a\u9501\u5bf9\u8c61\uff0c\u4ee5\u6b63\u786e\u540c\u6b65\u8be5\u7ebf\u7a0b\u5bf9 i \u53d8\u91cf\u7684\u8bbf\u95ee\u3002<\/li>\n<\/ul>\n<p>\u6587\u4e2d\u5173\u4e8e\u7684\u77e5\u8bc6\u4ecb\u7ecd\uff0c\u5e0c\u671b\u5bf9\u4f60\u7684\u5b66\u4e60\u6709\u6240\u5e2e\u52a9\uff01\u82e5\u662f\u53d7\u76ca\u532a\u6d45\uff0c\u90a3\u5c31\u52a8\u52a8\u9f20\u6807\u6536\u85cf\u8fd9\u7bc7\u300aJava \u7ebf\u7a0b\u540c\u6b65\uff1a\u5982\u4f55\u89e3\u51b3\u4e3b\u7ebf\u7a0b\u548c\u5b50\u7ebf\u7a0b\u65e0\u6cd5\u4ea4\u66ff\u6267\u884c\u7684\u95ee\u9898\uff1f\u300b\u6587\u7ae0\u5427\uff0c\u4e5f\u53ef\u5173\u6ce8\u516c\u4f17\u53f7\u4e86\u89e3\u76f8\u5173\u6280\u672f\u6587\u7ae0\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Java \u7ebf\u7a0b\u540c\u6b65\uff1a\u5982\u4f55\u89e3\u51b3\u4e3b\u7ebf&#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-203361","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/203361","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=203361"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/203361\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=203361"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=203361"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=203361"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}