{"id":203214,"date":"2025-05-22T12:13:03","date_gmt":"2025-05-22T04:13:03","guid":{"rendered":"https:\/\/server.hk\/cnblog\/203214\/"},"modified":"2025-05-22T12:13:03","modified_gmt":"2025-05-22T04:13:03","slug":"%e5%a4%9a%e7%ba%bf%e7%a8%8b%e5%8a%a0%e5%87%8f%e4%ba%a4%e6%9b%bf%e8%bf%90%e7%ae%97%e7%bb%93%e6%9e%9c%e9%94%99%e8%af%af%ef%bc%9a%e5%a6%82%e4%bd%95%e7%a1%ae%e4%bf%9d%e7%ba%bf%e7%a8%8b%e8%ae%bf%e9%97%ae","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/203214\/","title":{"rendered":"\u591a\u7ebf\u7a0b\u52a0\u51cf\u4ea4\u66ff\u8fd0\u7b97\u7ed3\u679c\u9519\u8bef\uff1a\u5982\u4f55\u786e\u4fdd\u7ebf\u7a0b\u8bbf\u95ee\u8d44\u6e90\u7684\u987a\u5e8f\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>\u591a\u7ebf\u7a0b\u52a0\u51cf\u4ea4\u66ff\u8fd0\u7b97\u7ed3\u679c\u9519\u8bef\uff1a\u5982\u4f55\u786e\u4fdd\u7ebf\u7a0b\u8bbf\u95ee\u8d44\u6e90\u7684\u987a\u5e8f\uff1f<\/h1>\n<p>\u201c\u7eb5\u6709\u75be\u98ce\u6765\uff0c\u4eba\u751f\u4e0d\u8a00\u5f03\u201d\uff0c\u8fd9\u53e5\u8bdd\u9001\u7ed9\u6b63\u5728\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\u7684\u670b\u53cb\u4eec\uff0c\u4e5f\u5e0c\u671b\u5728\u9605\u8bfb\u672c\u6587<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u591a\u7ebf\u7a0b\u52a0\u51cf\u4ea4\u66ff\u8fd0\u7b97\u7ed3\u679c\u9519\u8bef\uff1a\u5982\u4f55\u786e\u4fdd\u7ebf\u7a0b\u8bbf\u95ee\u8d44\u6e90\u7684\u987a\u5e8f\uff1f\u300b<\/span>\u540e\uff0c\u80fd\u591f\u771f\u7684\u5e2e\u52a9\u5230\u5927\u5bb6\u3002\u6211\u4e5f\u4f1a\u5728\u540e\u7eed\u7684\u6587\u7ae0\u4e2d\uff0c\u9646\u7eed\u66f4\u65b0<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\u76f8\u5173\u7684\u6280\u672f\u6587\u7ae0\uff0c\u6709\u597d\u7684\u5efa\u8bae\u6b22\u8fce\u5927\u5bb6\u5728\u8bc4\u8bba\u7559\u8a00\uff0c\u975e\u5e38\u611f\u8c22\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241102\/173050996567257c8d59732.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>\u591a\u7ebf\u7a0b\u5b9e\u73b0\u52a0\u51cf\u4ea4\u66ff\u8fd0\u7b97\uff0c\u7ed3\u679c\u4e0e\u9884\u671f\u4e0d\u7b26<\/strong><\/p>\n<p>\u8fd9\u4e2a\u95ee\u9898\u6e90\u4e8e\u5728\u591a\u7ebf\u7a0b\u73af\u5883\u4e0b\u7684\u540c\u6b65\u95ee\u9898\u3002\u5728\u7ed9\u5b9a\u7684\u4ee3\u7801\u7247\u6bb5\u4e2d\uff0c\u5b58\u5728\u591a\u4e2a\u7ebf\u7a0b\u5e76\u53d1\u64cd\u4f5c\u5171\u4eab\u8d44\u6e90\uff08num \u53d8\u91cf\uff09\uff0c\u4f46\u672a\u91c7\u53d6\u9002\u5f53\u7684\u540c\u6b65\u63aa\u65bd\u3002<\/p>\n<p>\u5177\u4f53\u6765\u8bf4\uff0c\u5728 resource \u7c7b\u4e2d\uff0cadd \u548c sub \u65b9\u6cd5\u5747\u4f7f\u7528\u4e86\u540c\u6b65\u5757\uff0c\u5e76\u5728\u9700\u8981\u65f6\u4f7f\u7528 wait \u548c notifyall \u8fdb\u884c\u7ebf\u7a0b\u901a\u4fe1\u3002\u7136\u800c\uff0c\u8be5\u4ee3\u7801\u6709\u4e00\u4e2a\u5173\u952e\u7f3a\u9677\uff1a<strong>\u5b83\u6ca1\u6709\u63a7\u5236\u7ebf\u7a0b\u8fdb\u5165\u65b9\u6cd5\u7684\u987a\u5e8f<\/strong>\u3002<\/p>\n<p>\u8fd9\u4f1a\u5bfc\u81f4\u7ebf\u7a0b\u5728\u9519\u8bef\u7684\u65f6\u523b\u8fdb\u5165\u65b9\u6cd5\uff0c\u5e76\u5bfc\u81f4\u4e0d\u4e00\u81f4\u7684\u72b6\u6001\u3002\u4f8b\u5982\uff0c\u51cf\u6cd5\u7ebf\u7a0b\u53ef\u80fd\u4f1a\u5728\u52a0\u6cd5\u7ebf\u7a0b\u5c1a\u672a\u5b8c\u6210\u4e4b\u524d\u6267\u884c\uff0c\u5bfc\u81f4\u8d1f\u6570\u7ed3\u679c\u3002<\/p>\n<p>\u8981\u89e3\u51b3\u6b64\u95ee\u9898\uff0c\u9700\u8981\u786e\u4fdd\u7ebf\u7a0b\u4ee5\u9884\u671f\u7684\u987a\u5e8f\u8bbf\u95ee\u8d44\u6e90\u3002\u4e00\u79cd\u65b9\u6cd5\u662f\u4f7f\u7528\u6761\u4ef6\u53d8\u91cf\uff0c\u8be5\u53d8\u91cf\u53ef\u4ee5\u963b\u6b62\u7ebf\u7a0b\u8fdb\u5165\u7279\u5b9a\u7684\u65b9\u6cd5\uff0c\u76f4\u5230\u6ee1\u8db3\u7279\u5b9a\u6761\u4ef6\uff08\u4f8b\u5982\uff0c\u5f53\u524d\u6b63\u5728\u6267\u884c\u52a0\u6cd5\u64cd\u4f5c\uff09\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u53ef\u4ee5\u5f3a\u5236\u52a0\u6cd5\u548c\u51cf\u6cd5\u64cd\u4f5c\u6309\u987a\u5e8f\u8fdb\u884c\uff0c\u4ece\u800c\u907f\u514d\u4e0d\u4e00\u81f4\u7684\u72b6\u6001\u3002<\/p>\n<p>\u4fee\u6539\u540e\u7684\u4ee3\u7801\u53ef\u80fd\u4f1a\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre>class Resource {\n    private int num = 0;\n    private boolean flag = true;\n\n    private final Condition condition = new Object(); \/\/ \u5f15\u5165\u6761\u4ef6\u53d8\u91cf\n\n    public synchronized void add() throws InterruptedException {\n        while (!flag) {\n            condition.await();\n        }\n        Thread.sleep(100);\n        num++;\n        System.out.println(\"[\u52a0\u6cd5\u64cd\u4f5c-\" + Thread.currentThread().getName() + \"]num = \" + num + \" \" + flag);\n        flag = false;\n        condition.signalAll();\n    }\n\n    public synchronized void sub() throws InterruptedException {\n        while (flag) {\n            condition.await();\n        }\n        Thread.sleep(200);\n        num--;\n        System.out.println(\"[\u51cf\u6cd5\u64cd\u4f5c-\" + Thread.currentThread().getName() + \"]num = \" + num + \" \" + flag);\n        flag = true;\n        condition.signalAll();\n    }\n}<\/pre>\n<p>\u901a\u8fc7\u4f7f\u7528\u6761\u4ef6\u53d8\u91cf\uff0c\u7ebf\u7a0b\u53ef\u4ee5\u7b49\u5f85\u76f4\u5230\u6ee1\u8db3\u7279\u5b9a\u6761\u4ef6\u624d\u80fd\u7ee7\u7eed\u6267\u884c\uff0c\u4ece\u800c\u786e\u4fdd\u52a0\u6cd5\u548c\u51cf\u6cd5\u64cd\u4f5c\u6309\u9884\u671f\u987a\u5e8f\u8fdb\u884c\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u300a\u591a\u7ebf\u7a0b\u52a0\u51cf\u4ea4\u66ff\u8fd0\u7b97\u7ed3\u679c\u9519\u8bef\uff1a\u5982\u4f55\u786e\u4fdd\u7ebf\u7a0b\u8bbf\u95ee\u8d44\u6e90\u7684\u987a\u5e8f\uff1f\u300b\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u5173\u4e8e\u7684\u8d44\u6599\u8bf7\u5173\u6ce8\u516c\u4f17\u53f7\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u591a\u7ebf\u7a0b\u52a0\u51cf\u4ea4\u66ff\u8fd0\u7b97\u7ed3\u679c\u9519\u8bef\uff1a\u5982\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-203214","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/203214","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=203214"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/203214\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=203214"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=203214"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=203214"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}