{"id":1056,"date":"2022-10-22T14:06:33","date_gmt":"2022-10-22T06:06:33","guid":{"rendered":"https:\/\/qaqaq.top\/?p=1056"},"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%e7%9a%84%e4%be%8b%e9%a2%98%e3%80%81sleep%e5%92%8cwait%e7%9a%84%e5%bc%82%e5%90%8c","status":"publish","type":"post","link":"https:\/\/qaqaq.top\/?p=1056","title":{"rendered":"\u591a\u7ebf\u7a0b-\u7ebf\u7a0b\u901a\u4fe1\u7684\u4f8b\u9898\u3001sleep()\u548cwait()\u7684\u5f02\u540c"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code>package top.qaqaq.java.P441;\r\n\r\n\/**\r\n * \u7ebf\u7a0b\u901a\u4fe1\u7684\u4f8b\u5b50\uff1a\u4f7f\u7528\u4e24\u4e2a\u7ebf\u7a0b\u6253\u5370 1-100\u3002\u7ebf\u7a0b1, \u7ebf\u7a0b2 \u4ea4\u66ff\u6253\u5370\r\n *\r\n * \u6d89\u53ca\u5230\u7684\u4e09\u4e2a\u65b9\u6cd5\uff1a\r\n * wait():\u4e00\u65e6\u6267\u884c\u6b64\u65b9\u6cd5\uff0c\u5f53\u524d\u7ebf\u7a0b\u5c31\u8fdb\u5165\u963b\u585e\u72b6\u6001\uff0c\u5e76\u91ca\u653e\u540c\u6b65\u76d1\u89c6\u5668\u3002\r\n * notify():\u4e00\u65e6\u6267\u884c\u6b64\u65b9\u6cd5\uff0c\u5c31\u4f1a\u5524\u9192\u88abwait\u7684\u4e00\u4e2a\u7ebf\u7a0b\u3002\u5982\u679c\u6709\u591a\u4e2a\u7ebf\u7a0b\u88abwait\uff0c\u5c31\u5524\u9192\u4f18\u5148\u7ea7\u9ad8\u7684\u90a3\u4e2a\r\n * notifyAll():\u4e00\u65e6\u6267\u884c\u6b64\u65b9\u6cd5\uff0c\u5c31\u4f1a\u5524\u9192\u6240\u6709\u88abwait\u7684\u7ebf\u7a0b\u3002\r\n *\r\n * \u8bf4\u660e\uff1a\r\n * 1.wait(), notify(), notifyAll()\u4e09\u4e2a\u65b9\u6cd5\u5fc5\u987b\u4f7f\u7528\u5728\u540c\u6b65\u4ee3\u7801\u5757\u6216\u540c\u6b65\u65b9\u6cd5\u4e2d\u3002\r\n * 2.wait(),notify(),notifyAll()\u4e09\u4e2a\u65b9\u6cd5\u7684\u8c03\u7528\u8005\u5fc5\u987b\u662f\u540c\u6b65\u4ee3\u7801\u5757\u6216\u540c\u6b65\u65b9\u6cd5\u4e2d\u7684\u540c\u6b65\u76d1\u89c6\u5668\u3002\r\n *      \u5426\u5219\uff0c\u4f1a\u51fa\u73b0IllegalMonitorStateException\u5f02\u5e38\r\n * 3.wait(),notify(),notifyAll()\u4e09\u4e2a\u65b9\u6cd5\u662f\u5b9a\u4e49\u5728java.lang.Object\u7c7b\u4e2d\u3002\r\n *\r\n * \u9762\u8bd5\u9898\uff1asleep() \u548c wait() \u7684\u5f02\u540c\uff1f\r\n * 1.\u76f8\u540c\u70b9\uff1a\u4e00\u65e6\u6267\u884c\u65b9\u6cd5\uff0c\u90fd\u53ef\u4ee5\u4f7f\u5f97\u5f53\u524d\u7684\u7ebf\u7a0b\u8fdb\u5165\u963b\u585e\u72b6\u6001\u3002\r\n * 2.\u4e0d\u540c\u70b9\uff1a1\uff09\u4e24\u4e2a\u65b9\u6cd5\u58f0\u660e\u7684\u4f4d\u7f6e\u4e0d\u540c\uff1aThread\u7c7b\u4e2d\u58f0\u660esleep(), Object\u7c7b\u4e2d\u58f0\u660ewait()\r\n *           2\uff09\u8c03\u7528\u7684\u8981\u6c42\u4e0d\u540c\uff1asleep()\u53ef\u4ee5\u5728\u4efb\u4f55\u9700\u8981\u7684\u573a\u666f\u4e0b\u8c03\u7528\u3002 wait()\u5fc5\u987b\u4f7f\u7528\u5728\u540c\u6b65\u4ee3\u7801\u5757\u6216\u540c\u6b65\u65b9\u6cd5\u4e2d\r\n *           3\uff09\u5173\u4e8e\u662f\u5426\u91ca\u653e\u540c\u6b65\u76d1\u89c6\u5668\uff1a\u5982\u679c\u4e24\u4e2a\u65b9\u6cd5\u90fd\u4f7f\u7528\u5728\u540c\u6b65\u4ee3\u7801\u5757\u6216\u540c\u6b65\u65b9\u6cd5\u4e2d\uff0csleep()\u4e0d\u4f1a\u91ca\u653e\u9501\uff0cwait()\u4f1a\u91ca\u653e\u9501\u3002\r\n *\r\n * @author RichieZhang\r\n * @create 2022-10-21 \u4e0b\u5348 8:01\r\n *\/\r\nclass Number implements Runnable{\r\n\r\n    private int number = 1;\r\n    private Object obj = new Object();\r\n\r\n    @Override\r\n    public void run() {\r\n\r\n        while (true){\r\n\r\n            synchronized (obj) {\r\n\r\n                obj.notify();\r\n\r\n                if(number &lt;= 100){\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                    System.out.println(Thread.currentThread().getName() + \":\" + number);\r\n                    number++;\r\n\r\n                    try {\r\n                        \/\/\u4f7f\u5f97\u8c03\u7528\u5982\u4e0bwait()\u65b9\u6cd5\u7684\u7ebf\u7a0b\u8fdb\u5165\u963b\u585e\u72b6\u6001\r\n                        obj.wait();\r\n                    } catch (InterruptedException e) {\r\n                        e.printStackTrace();\r\n                    }\r\n                }else {\r\n                    break;\r\n                }\r\n            }\r\n        }\r\n\r\n    }\r\n}\r\n\r\npublic class CommunicationTest {\r\n\r\n    public static void main(String&#91;] args) {\r\n\r\n        Number number = new Number();\r\n\r\n        Thread t1 = new Thread(number);\r\n        Thread t2 = new Thread(number);\r\n\r\n        t1.setName(\"\u7ebf\u7a0b1\");\r\n        t2.setName(\"\u7ebf\u7a0b2\");\r\n\r\n        t1.start();\r\n        t2.start();\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-1056","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\/1056"}],"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=1056"}],"version-history":[{"count":1,"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/posts\/1056\/revisions"}],"predecessor-version":[{"id":1057,"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/posts\/1056\/revisions\/1057"}],"wp:attachment":[{"href":"https:\/\/qaqaq.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1056"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/qaqaq.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1056"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/qaqaq.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1056"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}