{"id":1574,"date":"2020-11-16T11:32:10","date_gmt":"2020-11-16T03:32:10","guid":{"rendered":"https:\/\/www.yusian.com\/blog\/?p=1574"},"modified":"2020-11-17T17:40:12","modified_gmt":"2020-11-17T09:40:12","slug":"%e7%ba%bf%e7%a8%8b%e9%97%b4%e9%80%9a%e8%ae%af%e4%b9%8b%e3%80%90%e7%94%9f%e4%ba%a7%e8%80%85%e6%b6%88%e8%b4%b9%e8%80%85%e3%80%91%e6%a8%a1%e5%9e%8b","status":"publish","type":"post","link":"https:\/\/www.yusian.com\/blog\/java\/2020\/11\/16\/1132101574.html","title":{"rendered":"\u7ebf\u7a0b\u95f4\u901a\u8baf\u4e4b\u3010\u751f\u4ea7\u8005&#038;\u6d88\u8d39\u8005\u3011\u6a21\u578b"},"content":{"rendered":"<ul>\n<li><strong>\u540c\u6b65\uff1a<\/strong>\u7ebf\u7a0b\u95f4\u901a\u8baf\u8981\u786e\u4fdd\u540c\u6b65\u6267\u884c\uff1b<\/li>\n<li><strong>\u540c\u9501\uff1a<\/strong>\u7ebf\u7a0b\u95f4\u540c\u6b65\u673a\u5236\u9501\u9700\u8981\u662f\u540c\u4e00\u4e2a\u5bf9\u8c61\uff0c\u624d\u80fd\u4fdd\u8bc1\u540c\u6b65\uff0c\u53ef\u4ee5\u901a\u8fc7<code>synchronized<\/code>\u5173\u952e\u5b57\u6216<code>Lock<\/code>\u6765\u5b9e\u73b0\uff1b<\/li>\n<li><strong>\u8f6e\u8be2\uff1a<\/strong>\u901a\u8fc7<code>while<\/code>\u6216\u5176\u4ed6\u5faa\u73af\u65b9\u6cd5\u6765\u83b7\u53d6\u5f53\u524d\u72b6\u6001\uff0c\u662f\u8be5\u7b49\u5f85\u8fd8\u662f\u901a\u77e5\u5bf9\u65b9\uff1b<\/li>\n<li><strong>\u901a\u8baf\uff1a<\/strong>\u6240\u8c13\u7684\u901a\u8baf\u5176\u5b9e\u662f\u901a\u8fc7\u5171\u7528\u9501\u7684<code>wait<\/code>\u6216<code>notify<\/code>\u65b9\u6cd5\u6765\u5b9e\u73b0\u7684\uff1b<\/li>\n<\/ul>\n<p>\u5546\u54c1\u5bf9\u8c61\u7c7b<\/p>\n<pre><code class=\"language-java line-numbers\">\/**\n * \u5546\u54c1\u63cf\u8ff0\u7c7b\uff0c\u5f53\u524d\u53ea\u662f\u7b80\u5355\u5730\u6807\u8bb0\u5546\u54c1\u7684\u6709\u4e0e\u65e0\n *\/\npublic class Goods {\n    \/\/ \u662f\u5426\u8fd8\u6709\u5e93\u5b58\n    boolean isEmpty;\n\n    public Goods() {\n        this.isEmpty = true;\n    }\n}\n<\/code><\/pre>\n<p>\u6d88\u8d39\u8005<\/p>\n<pre><code class=\"language-java line-numbers\">\/**\n * \u6d88\u8d39\u8005\u6a21\u578b\n *\/\npublic class Consumer extends Thread {\n    Goods goods;\n\n    public Consumer(Goods goods) {\n        this.goods = goods;\n    }\n\n    @Override\n    public void run() {\n        \/\/ \u5faa\u73af\u68c0\u6d4b\n        while (true) {\n            \/\/ \u540c\u6b65\u6267\u884c\n            synchronized (goods) {\n                \/\/ \u5982\u679c\u5e93\u5b58\u4e0d\u4e3a\u7a7a\u5219\u5f00\u59cb\u6d88\u8d39\uff0c\u6d88\u8d39\u7ed3\u675f\u540e\uff0c\u5c06\u5e93\u5b58\u6807\u8bb0\u4e3a\u7a7a\n                if (!goods.isEmpty) {\n                    System.out.println(\"---------------------\");\n                    System.out.println(\"\u6d88\u8d39\");\n                    try {\n                        Thread.sleep(1000);\n                        System.out.println(\"...\");\n                    } catch (InterruptedException e) {\n                        e.printStackTrace();\n                    }\n                    System.out.println(\"\u9700\u6c42\");\n                    goods.isEmpty = true;\n                }\n                \/\/ \u901a\u77e5\u751f\u4ea7\uff0c\u5f53\u524d\u7ebf\u7a0b\u7b49\u5f85\uff0c\u76f4\u63a5\u5230\u751f\u4ea7\u5524\u9192\n                goods.notify();\n                try {\n                    goods.wait();\n                } catch (Exception e) {\n                    e.printStackTrace();\n                }\n            }\n        }\n    }\n}\n<\/code><\/pre>\n<p>\u751f\u4ea7\u8005<br \/>\n<!--more--><\/p>\n<pre><code class=\"language-java line-numbers\">\/**\n * \u751f\u4ea7\u8005\u6a21\u578b\n *\/\npublic class Producer extends Thread {\n    Goods goods;\n\n    public Producer(Goods goods) {\n        this.goods = goods;\n    }\n\n    @Override\n    public void run() {\n        while (true) {\n            synchronized (goods) {\n                \/\/ \u5982\u679c\u5e93\u5b58\u6807\u8bb0\u4e3a\u7a7a\u5219\u5f00\u59cb\u751f\u4ea7\uff0c\u751f\u4ea7\u7ed3\u675f\u540e\uff0c\u5c06\u5e93\u5b58\u6807\u8bb0\u4fee\u6539\u4e3a\u975e\u7a7a\n                if (goods.isEmpty) {\n                    System.out.println(\"+++++++++++++++++++++\");\n                    System.out.println(\"\u751f\u4ea7\");\n                    try {\n                        Thread.sleep(1000);\n                        System.out.println(\"...\");\n                    } catch (Exception e) {\n                        e.printStackTrace();\n                    }\n                    System.out.println(\"\u63d0\u4f9b\");\n                    goods.isEmpty = false;\n                }\n                \/\/ \u901a\u77e5\u6d88\u8d39\u5e76\u4e14\u5f53\u524d\u7ebf\u7a0b\u5904\u4e8e\u7b49\u5f85\uff0c\u76f4\u5230\u88ab\u6d88\u8d39\u5524\u9192\n                goods.notify();\n                try {\n                    goods.wait();\n                } catch (InterruptedException e) {\n                    e.printStackTrace();\n                }\n            }\n        }\n    }\n}\n<\/code><\/pre>\n<p>\u8c03\u7528<\/p>\n<pre><code class=\"language-java line-numbers\">    public static void main(String[] args) {\n        Goods goods = new Goods();\n        Consumer consumer = new Consumer(goods);\n        Producer producer = new Producer(goods);\n        consumer.start();\n        producer.start();\n    }\n<\/code><\/pre>\n<p>\u8fd0\u884c\u7ed3\u679c<\/p>\n<pre><code class=\"language-cmd line-numbers\">+++++++++++++++++++++\n\u751f\u4ea7\n...\n\u63d0\u4f9b\n---------------------\n\u6d88\u8d39\n...\n\u9700\u6c42\n+++++++++++++++++++++\n\u751f\u4ea7\n...\n\u63d0\u4f9b\n---------------------\n\u6d88\u8d39\n...\n\u9700\u6c42\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u540c\u6b65\uff1a\u7ebf\u7a0b\u95f4\u901a\u8baf\u8981\u786e\u4fdd\u540c\u6b65\u6267\u884c\uff1b \u540c\u9501\uff1a\u7ebf\u7a0b\u95f4\u540c\u6b65\u673a\u5236\u9501\u9700\u8981\u662f\u540c\u4e00\u4e2a\u5bf9\u8c61\uff0c\u624d\u80fd\u4fdd\u8bc1\u540c\u6b65\uff0c\u53ef\u4ee5\u901a\u8fc7synchronized\u5173\u952e\u5b57\u6216Lock\u6765\u5b9e\u73b0\uff1b \u8f6e\u8be2\uff1a\u901a\u8fc7while\u6216\u5176\u4ed6\u5faa\u73af\u65b9\u6cd5\u6765\u83b7\u53d6\u5f53\u524d\u72b6\u6001\uff0c\u662f\u8be5\u7b49\u5f85\u8fd8\u662f\u901a\u77e5\u5bf9\u65b9\uff1b \u901a\u8baf\uff1a\u6240\u8c13\u7684\u901a\u8baf\u5176\u5b9e\u662f\u901a\u8fc7\u5171\u7528\u9501\u7684wait\u6216notify\u65b9\u6cd5\u6765\u5b9e\u73b0\u7684\uff1b \u5546\u54c1\u5bf9\u8c61\u7c7b \/** * \u5546\u54c1\u63cf\u8ff0\u7c7b\uff0c\u5f53\u524d\u53ea\u662f\u7b80\u5355\u5730\u6807\u8bb0\u5546\u54c1\u7684\u6709\u4e0e\u65e0 *\/ public class Goods { \/\/ \u662f\u5426\u8fd8\u6709\u5e93\u5b58 boolean isEmpty; public Goods() { this.isEmpty = true; } } \u6d88\u8d39\u8005 \/** * \u6d88\u8d39\u8005\u6a21\u578b *\/ public class Consumer extends Thread { Goods goods; public Consumer(Goods goods) { this.goods = goods; } @Override public void run() { \/\/ \u5faa\u73af\u68c0\u6d4b while (true) [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[35],"tags":[79,192,262,261,263],"class_list":["post-1574","post","type-post","status-publish","format-standard","hentry","category-java","tag-79","tag-192","tag-262","tag-261","tag-263"],"_links":{"self":[{"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/posts\/1574","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/comments?post=1574"}],"version-history":[{"count":0,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/posts\/1574\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/media?parent=1574"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/categories?post=1574"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/tags?post=1574"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}