{"id":203200,"date":"2025-05-22T08:36:31","date_gmt":"2025-05-22T00:36:31","guid":{"rendered":"https:\/\/server.hk\/cnblog\/203200\/"},"modified":"2025-05-22T08:36:31","modified_gmt":"2025-05-22T00:36:31","slug":"java-%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%e4%b8%ba%e4%bd%95%e7%bb%93%e6%9e%9c%e4%b8%8e%e9%a2%84%e6%9c%9f%e4%b8%8d%e7%ac%a6%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/203200\/","title":{"rendered":"Java \u591a\u7ebf\u7a0b\u52a0\u51cf\u4ea4\u66ff\u8fd0\u7b97\u4e3a\u4f55\u7ed3\u679c\u4e0e\u9884\u671f\u4e0d\u7b26\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>Java \u591a\u7ebf\u7a0b\u52a0\u51cf\u4ea4\u66ff\u8fd0\u7b97\u4e3a\u4f55\u7ed3\u679c\u4e0e\u9884\u671f\u4e0d\u7b26\uff1f<\/h1>\n<p>\u4eb2\u7231\u7684\u7f16\u7a0b\u5b66\u4e60\u7231\u597d\u8005\uff0c\u5982\u679c\u4f60\u70b9\u5f00\u4e86\u8fd9\u7bc7\u6587\u7ae0\uff0c\u8bf4\u660e\u4f60\u5bf9\u300aJava \u591a\u7ebf\u7a0b\u52a0\u51cf\u4ea4\u66ff\u8fd0\u7b97\u4e3a\u4f55\u7ed3\u679c\u4e0e\u9884\u671f\u4e0d\u7b26\uff1f\u300b\u5f88\u611f\u5174\u8da3\u3002\u672c\u7bc7\u6587\u7ae0\u5c31\u6765\u7ed9\u5927\u5bb6\u8be6\u7ec6\u89e3\u6790\u4e00\u4e0b\uff0c\u4e3b\u8981\u4ecb\u7ecd\u4e00\u4e0b\uff0c\u5e0c\u671b\u6240\u6709\u8ba4\u771f\u8bfb\u5b8c\u7684\u7ae5\u978b\u4eec\uff0c\u90fd\u6709\u5b9e\u8d28\u6027\u7684\u63d0\u9ad8\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241101\/173043598867245b948a717.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>java \u591a\u7ebf\u7a0b\u5b9e\u73b0\u52a0\u51cf\u4ea4\u66ff\u8fd0\u7b97 \u7ed3\u679c\u4e0e\u9884\u671f\u4e0d\u7b26<\/strong><\/p>\n<p>\u5728\u7ed9\u51fa\u7684 java \u4ee3\u7801\u4e2d\uff0c\u4f5c\u8005\u4f7f\u7528\u591a\u7ebf\u7a0b\u5c1d\u8bd5\u5b9e\u73b0\u52a0\u51cf\u4ea4\u66ff\u8fd0\u7b97\uff0c\u4f46\u8fd0\u884c\u65f6\u51fa\u73b0\u7ed3\u679c\u4e0e\u9884\u671f\u4e0d\u7b26\u7684\u95ee\u9898\u3002\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre>class resource {\n    \/\/ \u5b9a\u4e49\u4e00\u4e2a\u64cd\u4f5c\u7684\u8d44\u6e90\n    private int num = 0;        \/\/ \u5b9a\u4e49\u8981\u8fdb\u884c\u52a0\u51cf\u64cd\u4f5c\u7684\u6570\u636e\n    private boolean flag = true;  \/\/ flag = true \u8868\u793a\u53ef\u4ee5\u8fdb\u884c\u52a0\u6cd5\u64cd\u4f5c,\u65e0\u6cd5\u8fdb\u884c\u51cf\u6cd5\u64cd\u4f5c\n                                 \/\/ flag = false \u8868\u793a\u53ef\u4ee5\u8fdb\u884c\u51cf\u6cd5\u64cd\u4f5c,\u65e0\u6cd5\u8fdb\u884c\u52a0\u6cd5\u64cd\u4f5c\n\n    public synchronized void add() throws interruptedexception {\n        \/\/ \u73b0\u5728\u9700\u8981\u6267\u884c\u7684\u662f\u51cf\u6cd5\u64cd\u4f5c,\u52a0\u6cd5\u64cd\u4f5c\u9700\u8981\u7b49\u5f85\n        if (!this.flag) {\n            super.wait();\n        }\n        thread.sleep(100);\n        this.num++;\n        system.out.println(\"[\u52a0\u6cd5\u64cd\u4f5c-\" + thread.currentthread().getname() + \"]num = \" + this.num+ \" \" + this.flag);\n        this.flag = false;\n        super.notifyall();\n    }\n\n    public synchronized void sub() throws interruptedexception {\n        \/\/ \u73b0\u5728\u8fdb\u884c\u7684\u662f\u51cf\u6cd5\u64cd\u4f5c,\u52a0\u6cd5\u64cd\u4f5c\u9700\u8981\u7b49\u5f85\n        if (this.flag) {\n            super.wait();\n        }\n        thread.sleep(200);\n        this.num--;\n        system.out.println(\"[\u51cf\u6cd5\u64cd\u4f5c-\" + thread.currentthread().getname() + \"]num = \" + this.num + \" \" + this.flag);\n        this.flag = true;\n        super.notifyall();\n    }\n}\n\nclass addthread implements runnable {\n    private resource resource;\n\n    public addthread(resource resource) {\n        this.resource = resource;\n    }\n\n    @override\n    public void run() {\n        for (int i = 0; i &lt; 50; i++) {\n            try {\n                this.resource.add();\n            } catch (interruptedexception e) {\n                e.printstacktrace();\n            }\n        }\n    }\n}\n\nclass subthread implements runnable {\n    private resource resource;\n\n    public subthread(resource resource) {\n        this.resource = resource;\n    }\n\n    @override\n    public void run() {\n        for (int i = 0; i &lt; 50; i++) {\n            try {\n                this.resource.sub();\n            } catch (interruptedexception e) {\n                e.printstacktrace();\n            }\n        }\n    }\n}\n\npublic class mycalculation {\n    public static void main(string[] args) {\n        resource resource = new resource();\n        addthread addthread = new addthread(resource);\n        subthread subthread = new subthread(resource);\n        new thread(addthread, \"\u52a0\u6cd5\u7ebf\u7a0ba\").start();\n        new thread(addthread, \"\u52a0\u6cd5\u7ebf\u7a0bb\").start();\n        new thread(subthread, \"\u51cf\u6cd5\u7ebf\u7a0ba\").start();\n        new thread(subthread, \"\u51cf\u6cd5\u7ebf\u7a0bb\").start();\n    }\n}<\/pre>\n<p>\u95ee\u9898\u5728\u4e8e\uff0c\u591a\u4e2a\u7ebf\u7a0b\u540c\u65f6\u8fdb\u5165 add \u6216 sub \u65b9\u6cd5\u540e\uff0c\u53ef\u80fd\u4f1a\u51fa\u73b0\u7ade\u4e89\u6761\u4ef6\u3002\u4f8b\u5982\uff0c\u5f53 sub \u7ebf\u7a0b\u8fdb\u5165\u65b9\u6cd5\u540e\uff0c\u5982\u679c flag \u4e3a true\uff0c\u7ebf\u7a0b\u5c06\u8fdb\u5165 wait() \u72b6\u6001\u3002\u6b64\u65f6\uff0cadd \u7ebf\u7a0b\u53ef\u4ee5\u6210\u529f\u8fdb\u5165\u65b9\u6cd5\uff0c\u5c06 num \u52a0 1\u3002\u7136\u540e\uff0cadd \u7ebf\u7a0b\u5c06 flag \u8bbe\u7f6e\u4e3a false\uff0c\u5e76\u5524\u9192\u6240\u6709\u7b49\u5f85\u7684\u7ebf\u7a0b\u3002\u4f46\u6b64\u65f6\uff0csub \u7ebf\u7a0b\u4ecd\u5904\u4e8e wait \u72b6\u6001\uff0c\u65e0\u6cd5\u7ee7\u7eed\u6267\u884c\u3002\u5f53 sub \u7ebf\u7a0b\u88ab\u5524\u9192\u540e\uff0c\u5b83\u5c06\u7ee7\u7eed\u6267\u884c\uff0c\u5e76\u53ef\u80fd\u5c06 num \u51cf 1\u3002\u8fd9\u6837\uff0c\u6700\u7ec8\u7ed3\u679c\u53ef\u80fd\u4e0e\u9884\u671f\u4e0d\u7b26\uff0c\u5bfc\u81f4 num \u51fa\u73b0\u8d1f\u503c\u3002<\/p>\n<p>\u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0cjava \u4e2d\u63d0\u4f9b\u4e86 wait(long) \u548c notifyall(long) \u65b9\u6cd5\uff0c\u53ef\u4ee5\u6307\u5b9a\u7b49\u5f85\u65f6\u95f4\u3002\u901a\u8fc7\u8bbe\u7f6e\u7b49\u5f85\u65f6\u95f4\uff0c\u53ef\u4ee5\u9632\u6b62\u7ebf\u7a0b\u5904\u4e8e wait \u72b6\u6001\u8fc7\u4e45\uff0c\u4ece\u800c\u907f\u514d\u51fa\u73b0\u7ade\u4e89\u6761\u4ef6\u3002<\/p>\n<p>\u4fee\u6539\u540e\u7684\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre>public synchronized void add() throws interruptedexception {\n    \/\/ \u73b0\u5728\u9700\u8981\u6267\u884c\u7684\u662f\u51cf\u6cd5\u64cd\u4f5c,\u52a0\u6cd5\u64cd\u4f5c\u9700\u8981\u7b49\u5f85\n    while (!this.flag) {\n        super.wait(10);\n    }\n    thread.sleep(100);\n    this.num++;\n    system.out.println(\"[\u52a0\u6cd5\u64cd\u4f5c-\" + thread.currentthread().getname() + \"]num = \" + this.num+ \" \" + this.flag);\n    this.flag = false;\n    super.notifyall(10);\n}<\/pre>\n<pre>public synchronized void sub() throws InterruptedException {\n    \/\/ \u73b0\u5728\u8fdb\u884c\u7684\u662f\u51cf\u6cd5\u64cd\u4f5c,\u52a0\u6cd5\u64cd\u4f5c\u9700\u8981\u7b49\u5f85\n    while (this.flag) {\n        super.wait(10);\n    }\n    Thread.sleep(200);\n    this.num--;\n    System.out.println(\"[\u51cf\u6cd5\u64cd\u4f5c-\" + Thread.currentThread().getName() + \"]num = \" + this.num + \" \" + this.flag);\n    this.flag = true;\n    super.notifyAll(10);\n}<\/pre>\n<p>\u7ec8\u4e8e\u4ecb\u7ecd\u5b8c\u5566\uff01\u5c0f\u4f19\u4f34\u4eec\uff0c\u8fd9\u7bc7\u5173\u4e8e\u300aJava \u591a\u7ebf\u7a0b\u52a0\u51cf\u4ea4\u66ff\u8fd0\u7b97\u4e3a\u4f55\u7ed3\u679c\u4e0e\u9884\u671f\u4e0d\u7b26\uff1f\u300b\u7684\u4ecb\u7ecd\u5e94\u8be5\u8ba9\u4f60\u6536\u83b7\u591a\u591a\u4e86\u5427\uff01\u6b22\u8fce\u5927\u5bb6\u6536\u85cf\u6216\u5206\u4eab\u7ed9\u66f4\u591a\u9700\u8981\u5b66\u4e60\u7684\u670b\u53cb\u5427~\u516c\u4f17\u53f7\u4e5f\u4f1a\u53d1\u5e03\u6587\u7ae0\u76f8\u5173\u77e5\u8bc6\uff0c\u5feb\u6765\u5173\u6ce8\u5427\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Java \u591a\u7ebf\u7a0b\u52a0\u51cf\u4ea4\u66ff\u8fd0\u7b97\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-203200","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/203200","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=203200"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/203200\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=203200"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=203200"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=203200"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}