{"id":1058,"date":"2022-10-22T14:06:54","date_gmt":"2022-10-22T06:06:54","guid":{"rendered":"https:\/\/qaqaq.top\/?p=1058"},"modified":"2022-11-27T12:39:50","modified_gmt":"2022-11-27T04:39:50","slug":"%e5%a4%9a%e7%ba%bf%e7%a8%8b-%e7%ba%bf%e7%a8%8b%e9%80%9a%e4%bf%a1%ef%bc%9a%e7%94%9f%e4%ba%a7%e8%80%85%e6%b6%88%e8%b4%b9%e8%80%85%e4%be%8b%e9%a2%98","status":"publish","type":"post","link":"https:\/\/qaqaq.top\/?p=1058","title":{"rendered":"\u591a\u7ebf\u7a0b-\u7ebf\u7a0b\u901a\u4fe1\uff1a\u751f\u4ea7\u8005\u6d88\u8d39\u8005\u4f8b\u9898"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code>package top.qaqaq.java.P443;\r\n\r\n\/**\r\n * \u7ebf\u7a0b\u901a\u4fe1\u7684\u5e94\u7528\uff1a\u7ecf\u5178\u4f8b\u9898\uff1a\u751f\u4ea7\u8005\/\u6d88\u8d39\u8005\u95ee\u9898\r\n *\r\n * \u751f\u4ea7\u8005(Productor)\u5c06\u4ea7\u54c1\u4ea4\u7ed9\u5e97\u5458(Clerk)\uff0c\u800c\u6d88\u8d39\u8005(Customer)\u4ece\u5e97\u5458\u5904\u53d6\u8d70\u4ea7\u54c1\uff0c\r\n * \u5e97\u5458\u4e00\u6b21\u53ea\u80fd\u6301\u6709\u56fa\u5b9a\u6570\u91cf\u7684\u4ea7\u54c1(\u6bd4\u5982:20\uff09\uff0c\u5982\u679c\u751f\u4ea7\u8005\u8bd5\u56fe\u751f\u4ea7\u66f4\u591a\u7684\u4ea7\u54c1\uff0c\u5e97\u5458\r\n * \u4f1a\u53eb\u751f\u4ea7\u8005\u505c\u4e00\u4e0b\uff0c\u5982\u679c\u5e97\u4e2d\u6709\u7a7a\u4f4d\u653e\u4ea7\u54c1\u4e86\u518d\u901a\u77e5\u751f\u4ea7\u8005\u7ee7\u7eed\u751f\u4ea7\uff1b\u5982\u679c\u5e97\u4e2d\u6ca1\u6709\u4ea7\u54c1\r\n * \u4e86\uff0c\u5e97\u5458\u4f1a\u544a\u8bc9\u6d88\u8d39\u8005\u7b49\u4e00\u4e0b\uff0c\u5982\u679c\u5e97\u4e2d\u6709\u4ea7\u54c1\u4e86\u518d\u901a\u77e5\u6d88\u8d39\u8005\u6765\u53d6\u8d70\u4ea7\u54c1\u3002\r\n *\r\n * \u5206\u6790\uff1a\r\n * 1. \u662f\u5426\u662f\u591a\u7ebf\u7a0b\u95ee\u9898\uff1f \u662f\uff0c \u751f\u4ea7\u8005\u7ebf\u7a0b\uff0c\u6d88\u8d39\u8005\u7ebf\u7a0b\r\n * 2. \u662f\u5426\u6709\u5171\u4eab\u6570\u636e\uff1f\u662f\uff0c\u5e97\u5458\uff08\u6216\u4ea7\u54c1\uff09\r\n * 3. \u5982\u4f55\u89e3\u51b3\u7ebf\u7a0b\u7684\u5b89\u5168\u95ee\u9898\uff1f\u540c\u6b65\u673a\u5236\uff0c\u6709\u4e09\u79cd\u65b9\u6cd5\r\n * 4. \u662f\u5426\u6d89\u53ca\u5230\u7ebf\u7a0b\u7684\u901a\u4fe1\uff1f\u662f\r\n *\r\n * @author RichieZhang\r\n * @create 2022-10-21 \u4e0b\u5348 8:42\r\n *\/\r\nclass Clerk{\r\n\r\n    private int productCount = 0;\r\n\r\n    \/\/\u751f\u4ea7\u4ea7\u54c1\r\n    public synchronized void produceProduct() {\r\n\r\n        if (productCount &lt; 20){\r\n            productCount++;\r\n            System.out.println(Thread.currentThread().getName() + \":\u5f00\u59cb\u751f\u4ea7\u7b2c\" + productCount + \"\u4e2a\u4ea7\u54c1\");\r\n\r\n            notify();\r\n        }else {\r\n            \/\/\u7b49\u5f85\r\n            try {\r\n                wait();\r\n            } catch (InterruptedException e) {\r\n                e.printStackTrace();\r\n            }\r\n        }\r\n    }\r\n\r\n    \/\/\u6d88\u8d39\u4ea7\u54c1\r\n    public synchronized void consumeProduct() {\r\n        if(productCount > 0){\r\n            System.out.println(Thread.currentThread().getName() + \":\u5f00\u59cb\u6d88\u8d39\u7b2c\" + productCount + \"\u4e2a\u4ea7\u54c1\");\r\n            productCount--;\r\n\r\n            notify();\r\n        }else {\r\n            \/\/\u7b49\u5f85\r\n            try {\r\n                wait();\r\n            } catch (InterruptedException e) {\r\n                e.printStackTrace();\r\n            }\r\n        }\r\n    }\r\n}\r\n\r\nclass Producer extends Thread   {\/\/\u751f\u4ea7\u8005\r\n\r\n    private Clerk clerk;\r\n\r\n    public Producer(Clerk clerk) {\r\n        this.clerk = clerk;\r\n    }\r\n\r\n    @Override\r\n    public void run() {\r\n        System.out.println(getName() + \":\u5f00\u59cb\u751f\u4ea7\u4ea7\u54c1.....\");\r\n\r\n        while (true){\r\n\r\n            try {\r\n                Thread.sleep(10);\r\n            } catch (InterruptedException e) {\r\n                e.printStackTrace();\r\n            }\r\n\r\n            clerk.produceProduct();\r\n        }\r\n    }\r\n}\r\n\r\nclass Consumer extends Thread{\/\/\u6d88\u8d39\u8005\r\n    private Clerk clerk;\r\n\r\n    public Consumer(Clerk clerk) {\r\n        this.clerk = clerk;\r\n    }\r\n\r\n    @Override\r\n    public void run() {\r\n        System.out.println(getName() + \":\u5f00\u59cb\u6d88\u8d39\u4ea7\u54c1.....\");\r\n\r\n        while (true){\r\n\r\n            try {\r\n                Thread.sleep(20);\r\n            } catch (InterruptedException e) {\r\n                e.printStackTrace();\r\n            }\r\n\r\n            clerk.consumeProduct();\r\n        }\r\n    }\r\n}\r\n\r\n\r\npublic class ProductTest {\r\n\r\n    public static void main(String&#91;] args) {\r\n\r\n        Clerk clerk = new Clerk();\r\n\r\n        Producer p1 = new Producer(clerk);\r\n\r\n        p1.setName(\"\u751f\u4ea7\u80051\");\r\n\r\n        Consumer c1 = new Consumer(clerk);\r\n        c1.setName(\"\u6d88\u8d39\u80051\");\r\n        Consumer c2 = new Consumer(clerk);\r\n        c2.setName(\"\u6d88\u8d39\u80052\");\r\n\r\n        p1.start();\r\n        c1.start();\r\n        c2.start();\r\n\r\n\r\n    }\r\n}\r\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[46],"class_list":["post-1058","post","type-post","status-publish","format-standard","hentry","category-java-code","tag-java"],"_links":{"self":[{"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/posts\/1058"}],"collection":[{"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/qaqaq.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1058"}],"version-history":[{"count":1,"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/posts\/1058\/revisions"}],"predecessor-version":[{"id":1059,"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/posts\/1058\/revisions\/1059"}],"wp:attachment":[{"href":"https:\/\/qaqaq.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1058"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/qaqaq.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1058"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/qaqaq.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1058"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}