{"id":10629,"date":"2023-11-28T21:40:19","date_gmt":"2023-11-28T13:40:19","guid":{"rendered":"https:\/\/wx.kaifamiao.info\/?p=10629"},"modified":"2023-11-28T21:40:24","modified_gmt":"2023-11-28T13:40:24","slug":"java-zhong-waitnotifynotifyall-shi-xian-xian-cheng","status":"publish","type":"post","link":"http:\/\/wx.kaifamiao.info\/index.php\/2023\/11\/28\/java-zhong-waitnotifynotifyall-shi-xian-xian-cheng\/","title":{"rendered":"Java \u4e2d wait\/notify\/notifyAll \u5b9e\u73b0\u7ebf\u7a0b\u95f4\u901a\u4fe1\uff1f"},"content":{"rendered":"<p>\u5728Java\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u914d\u5408\u8c03\u7528Object\u5bf9\u8c61\u7684wait()\u65b9\u6cd5\u548cnotify()\u65b9\u6cd5\u6216notifyAll<br \/>\n()\u65b9\u6cd5\u6765\u5b9e\u73b0\u7ebf\u7a0b\u95f4\u7684\u901a\u4fe1\u3002\u5728\u7ebf\u7a0b\u4e2d\u8c03\u7528wait()\u65b9\u6cd5\uff0c\u5c06\u963b\u585e\u7b49\u5f85\u5176\u4ed6\u7ebf\u7a0b\u7684\u901a\u77e5(\u5176\u4ed6\u7ebf\u7a0b\u8c03\u7528notify()\u65b9\u6cd5\u6216notifyAll()\u65b9\u6cd5)\uff0c\u5728\u7ebf\u7a0b\u4e2d\u8c03\u7528notify()\u65b9\u6cd5\u6216notifyAll()\u65b9\u6cd5\uff0c\u5c06\u901a\u77e5\u5176\u4ed6\u7ebf\u7a0b\u4ecewait()\u65b9\u6cd5\u5904\u8fd4\u56de\u3002<\/p>\n<p>Object\u662f\u6240\u6709\u7c7b\u7684\u8d85\u7c7b\uff0c\u5b83\u67095\u4e2a\u65b9\u6cd5\u7ec4\u6210\u4e86\u7b49\u5f85\/\u901a\u77e5\u673a\u5236\u7684\u6838\u5fc3\uff1anotify() .notifyAll()\u3001wait()\u3001wait (long)\u548c wait (long, int)\u3002\u5728 Java \u4e2d\uff0c\u6240\u6709\u7684\u7c7b\u90fd\u4ece Object \u7ee7 \u627f\u800c\u6765\uff0c\u56e0\u6b64\uff0c\u6240\u6709\u7684\u7c7b\u90fd\u62e5\u6709\u8fd9\u4e9b\u5171\u6709\u65b9\u6cd5\u53ef\u4f9b\u4f7f\u7528\u3002\u800c\u4e14\uff0c\u7531\u4e8e\u4ed6\u4eec\u90fd\u88ab\u58f0\u660e\u4e3afinal, \u56e0\u6b64\u5728\u5b50\u7c7b\u4e2d\u4e0d\u80fd\u8986\u5199\u4efb\u4f55\u4e00\u4e2a\u65b9\u6cd5\u3002<br \/>\n\u8fd9\u91cc\u8be6\u7ec6\u8bf4\u660e\u4e00\u4e0b\u5404\u4e2a\u65b9\u6cd5\u5728\u4f7f\u7528\u4e2d\u9700\u8981\u6ce8\u610f\u7684\u51e0\u70b9\uff1a<br \/>\n1\u3001\twait()<br \/>\npublic final void wait() throws InterruptedException,IllegalMonitorStateException<br \/>\n\u8be5\u65b9\u6cd5\u7528\u6765\u5c06\u5f53\u524d\u7ebf\u7a0b\u7f6e\u5165\u4f11\u7720\u72b6\u6001\uff0c\u76f4\u5230\u63a5\u5230\u901a\u77e5\u6216\u88ab\u4e2d\u65ad\u4e3a\u6b62\u3002\u5728\u8c03\u7528wait()\u4e4b\u524d\uff0c \u7ebf\u7a0b\u5fc5\u987b\u8981\u83b7\u5f97\u8be5\u5bf9\u8c61\u7684\u5bf9\u8c61\u7ea7\u522b\u9501\uff0c\u5373\u53ea\u80fd\u5728\u540c\u6b65\u65b9\u6cd5\u6216\u540c\u6b65\u5757\u4e2d\u8c03\u7528wait()\u65b9\u6cd5\u3002\u8fdb \u5165wait()\u65b9\u6cd5\u540e\uff0c\u5f53\u524d\u7ebf\u7a0b\u91ca\u653e\u9501\u3002\u5728\u4ecewait()\u8fd4\u56de\u524d\uff0c\u7ebf\u7a0b\u4e0e\u5176\u4ed6\u7ebf\u7a0b\u7ade\u4e89\u91cd\u65b0\u83b7\u5f97 \u9501\u3002\u5982\u679c\u8c03\u7528wait()\u65f6,\u6ca1\u6709\u6301\u6709\u9002\u5f53\u7684\u9501\uff0c\u5219\u629b\u51faIllegalMonitorStateException,\u5b83 \u662fRuntimeException\u7684\u4e00\u4e2a\u5b50\u7c7b\uff0c\u56e0\u6b64\uff0c\u4e0d\u9700\u8981try-catch\u7ed3\u6784\u3002<br \/>\n2\u3001notify()<\/p>\n<p>public final native void notify() throws IllegalMonitorStateException<br \/>\n\u8be5\u65b9\u6cd5\u4e5f\u8981\u5728\u540c\u6b65\u65b9\u6cd5\u6216\u540c\u6b65\u5757\u4e2d\u8c03\u7528\uff0c\u5373\u5728\u8c03\u7528\u524d\uff0c\u7ebf\u7a0b\u4e5f\u5fc5\u987b\u8981\u83b7\u5f97\u8be5\u5bf9\u8c61\u7684\u5bf9\u8c61\u7ea7\u522b\u9501, \u7684\u5982\u679c\u8c03\u7528notify()\u65f6\u6ca1\u6709\u6301\u6709\u9002\u5f53\u7684\u9501\uff0c\u4e5f\u4f1a\u629b\u51faIllegalMonitorStateException\u3002<br \/>\n\u8be5\u65b9\u6cd5\u7528\u6765\u901a\u77e5\u90a3\u4e9b\u53ef\u80fd\u7b49\u5f85\u8be5\u5bf9\u8c61\u7684\u5bf9\u8c61\u9501\u7684\u5176\u4ed6\u7ebf\u7a0b\u3002\u5982\u679c\u6709\u591a\u4e2a\u7ebf\u7a0b\u7b49\u5f85\uff0c\u5219\u7ebf\u7a0b\u89c4\u5212 \u5668\u4efb\u610f\u6311\u9009\u51fa\u5176\u4e2d\u4e00\u4e2await()\u72b6\u6001\u7684\u7ebf\u7a0b\u6765\u53d1\u51fa\u901a\u77e5\uff0c\u5e76\u4f7f\u5b83\u7b49\u5f85\u83b7\u53d6\u8be5\u5bf9\u8c61\u7684\u5bf9\u8c61\u9501 (notify\u540e\uff0c\u5f53\u524d\u7ebf\u7a0b\u4e0d\u4f1a\u9a6c\u4e0a\u91ca\u653e\u8be5\u5bf9\u8c61\u9501\uff0cwait\u6240\u5728\u7684\u7ebf\u7a0b\u5e76\u4e0d\u80fd\u9a6c\u4e0a\u83b7\u53d6\u8be5\u5bf9\u8c61\u9501\uff0c \u8981\u7b49\u5230\u7a0b\u5e8f\u9000\u51fasynchronized\u4ee3\u7801\u5757\u540e\uff0c\u5f53\u524d\u7ebf\u7a0b\u624d\u4f1a\u91ca\u653e\u9501\uff0cwait\u6240\u5728\u7684\u7ebf\u7a0b\u4e5f\u624d\u53ef\u4ee5\u83b7 \u53d6\u8be5\u5bf9\u8c61\u9501)\uff0c\u4f46\u4e0d\u60ca\u52a8\u5176\u4ed6\u540c\u6837\u5728\u7b49\u5f85\u88ab\u8be5\u5bf9\u8c61notify\u7684\u7ebf\u7a0b\u4eec\u3002\u5f53\u7b2c\u4e00\u4e2a\u83b7\u5f97\u4e86\u8be5\u5bf9\u8c61 \u9501\u7684wait\u7ebf\u7a0b\u8fd0\u884c\u5b8c\u6bd5\u4ee5\u540e\uff0c\u5b83\u4f1a\u91ca\u653e\u6389\u8be5\u5bf9\u8c61\u9501\uff0c\u6b64\u65f6\u5982\u679c\u8be5\u5bf9\u8c61\u6ca1\u6709\u518d\u6b21\u4f7f\u7528notify \u8bed\u53e5\uff0c\u5219\u5373\u4fbf\u8be5\u5bf9\u8c61\u5df2\u7ecf\u7a7a\u95f2\uff0c\u5176\u4ed6wait\u72b6\u6001\u7b49\u5f85\u7684\u7ebf\u7a0b\u7531\u4e8e\u6ca1\u6709\u5f97\u5230\u8be5\u5bf9\u8c61\u7684\u901a\u77e5\uff0c\u4f1a\u7ee7 \u7eed\u963b\u585e\u5728wait\u72b6\u6001\uff0c\u76f4\u5230\u8fd9\u4e2a\u5bf9\u8c61\u53d1\u51fa\u4e00\u4e2anotify\u6216notifyAll\u3002\u8fd9\u91cc\u9700\u8981\u6ce8\u610f\uff1a\u5b83\u4eec\u7b49\u5f85 \u7684\u662f\u88abnotify\u6216notifyAll,\u800c\u4e0d\u662f\u9501\u3002\u8fd9\u4e0e\u4e0b\u9762\u7684notifyAll()\u65b9\u6cd5\u6267\u884c\u540e\u7684\u60c5\u51b5\u4e0d\u540c\u3002<br \/>\n3\u3001notifyAll()<br \/>\npublic final native void notifyAll() throws IllegalMonitorStateException<br \/>\n\u8be5\u65b9\u6cd5\u4e0enotify()\u65b9\u6cd5\u7684\u5de5\u4f5c\u65b9\u5f0f\u76f8\u540c\uff0c\u91cd\u8981\u7684\u4e00\u70b9\u5dee\u5f02\u662f\uff1a<br \/>\nnotifyAl1\u4f7f\u6240\u6709\u539f\u6765\u5728\u8be5\u5bf9\u8c61\u4e0await\u7684\u7ebf\u7a0b\u7edf\u7edf\u9000\u51fawait\u7684\u72b6\u6001(\u5373\u5168\u90e8\u88ab\u5524\u9192\uff0c\u4e0d\u518d\u7b49 \u5f85notify\u6216notifyAll,\u4f46\u7531\u4e8e\u6b64\u65f6\u8fd8\u6ca1\u6709\u83b7\u53d6\u5230\u8be5\u5bf9\u8c61\u9501\uff0c\u56e0\u6b64\u8fd8\u4e0d\u80fd\u7ee7\u7eed\u5f80\u4e0b\u6267\u884c), \u53d8\u6210\u7b49\u5f85\u83b7\u53d6\u8be5\u5bf9\u8c61\u4e0a\u7684\u9501\uff0c\u4e00\u65e6\u8be5\u5bf9\u8c61\u9501\u88ab\u91ca\u653e(notifyAll\u7ebf\u7a0b\u9000\u51fa\u8c03\u7528\u4e86 notifyAll\u7684 synchronized\u4ee3\u7801\u5757\u7684\u65f6\u5019)\uff0c\u4ed6\u4eec\u5c31\u4f1a\u53bb\u7ade\u4e89\u3002\u5982\u679c\u5176\u4e2d\u4e00\u4e2a\u7ebf\u7a0b\u83b7\u5f97\u4e86\u8be5\u5bf9\u8c61\u9501\uff0c\u5b83\u5c31 \u4f1a\u7ee7\u7eed\u5f80\u4e0b\u6267\u884c\uff0c\u5728\u5b83\u9000\u51fasynchronized\u4ee3\u7801\u5757\uff0c\u91ca\u653e\u9501\u540e\uff0c\u5176\u4ed6\u7684\u5df2\u7ecf\u88ab\u5524\u9192\u7684\u7ebf\u7a0b\u5c06\u4f1a \u7ee7\u7eed\u7ade\u4e89\u83b7\u53d6\u8be5\u9501\uff0c\u4e00\u76f4\u8fdb\u884c\u4e0b\u53bb\uff0c\u76f4\u5230\u6240\u6709\u88ab\u5524\u9192\u7684\u7ebf\u7a0b\u90fd\u6267\u884c\u5b8c\u6bd5\u3002<br \/>\n4\u3001\twait (long)\u548c wait (long,int)<br \/>\n\u663e\u7136\uff0c\u8fd9\u4e24\u4e2a\u65b9\u6cd5\u662f\u8bbe\u7f6e\u7b49\u5f85\u8d85\u65f6\u65f6\u95f4\u7684\uff0c\u540e\u8005\u5728\u8d85\u503c\u65f6\u95f4\u4e0a\u52a0\u4e0ans,\u7cbe\u5ea6\u4e5f\u96be\u4ee5\u8fbe\u5230\uff0c\u56e0 \u6b64\uff0c\u8be5\u65b9\u6cd5\u5f88\u5c11\u4f7f\u7528\u3002\u5bf9\u4e8e\u524d\u8005\uff0c\u5982\u679c\u5728\u7b49\u5f85\u7ebf\u7a0b\u63a5\u5230\u901a\u77e5\u6216\u88ab\u4e2d\u65ad\u4e4b\u524d\uff0c\u5df2\u7ecf\u8d85\u8fc7\u4e86\u6307\u5b9a\u7684 \u6beb\u79d2\u6570\uff0c\u5219\u5b83\u901a\u8fc7\u7ade\u4e89\u91cd\u65b0\u83b7\u5f97\u9501\uff0c\u5e76\u4ecewait (long)\u8fd4\u56de\u3002\u53e6\u5916\uff0c\u9700\u8981\u77e5\u9053\uff0c\u5982\u679c\u8bbe\u7f6e\u4e86 \u8d85\u65f6\u65f6\u95f4\uff0c\u5f53wait()\u8fd4\u56de\u65f6\uff0c\u6211\u4eec\u4e0d\u80fd\u786e\u5b9a\u5b83\u662f\u56e0\u4e3a\u63a5\u5230\u4e86\u901a\u77e5\u8fd8\u662f\u56e0\u4e3a\u8d85\u65f6\u800c\u8fd4\u56de\u7684\uff0c \u56e0\u4e3await()\u65b9\u6cd5\u4e0d\u4f1a\u8fd4\u56de\u4efb\u4f55\u76f8\u5173\u7684\u4fe1\u606f\u3002\u4f46\u4e00\u822c\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e\u6807\u5fd7\u4f4d\u6765\u5224\u65ad\uff0c\u5728notify \u4e4b\u524d\u6539\u53d8\u6807\u5fd7\u4f4d\u7684\u503c\uff0c\u5728wait()\u65b9\u6cd5\u540e\u8bfb\u53d6\u8be5\u6807\u5fd7\u4f4d\u7684\u503c\u6765\u5224\u65ad\uff0c\u5f53\u7136\u4e3a\u4e86\u4fdd\u8bc1notify\u4e0d \u88ab\u9057\u6f0f\uff0c\u6211\u4eec\u8fd8\u9700\u8981\u53e6\u5916\u4e00\u4e2a\u6807\u5fd7\u4f4d\u6765\u5faa\u73af\u5224\u65ad\u662f\u5426\u8c03\u7528wait()\u65b9\u6cd5\u3002<br \/>\n\u6df1\u5165\u7406\u89e3\uff1a<br \/>\n\u5982\u679c\u7ebf\u7a0b\u8c03\u7528\u4e86\u5bf9\u8c61\u7684wait()\u65b9\u6cd5\uff0c\u90a3\u4e48\u7ebf\u7a0b\u4fbf\u4f1a\u5904\u4e8e\u8be5\u5bf9\u8c61\u7684\u7b49\u5f85\u6c60\u4e2d\uff0c\u7b49\u5f85\u6c60\u4e2d\u7684\u7ebf \u7a0b\u4e0d\u4f1a\u53bb\u7ade\u4e89\u8be5\u5bf9\u8c61\u7684\u9501\u3002<br \/>\n\u5f53\u6709\u7ebf\u7a0b\u8c03\u7528\u4e86\u5bf9\u8c61\u7684notifyAll()\u65b9\u6cd5(\u5524\u9192\u6240\u6709wait\u7ebf\u7a0b)\u6216notify()\u65b9\u6cd5(\u53ea\u968f\u673a\u5524\u9192\u4e00\u4e2await\u7ebf\u7a0b)\uff0c\u88ab\u5524\u9192\u7684\u7684\u7ebf\u7a0b\u4fbf\u4f1a\u8fdb\u5165\u8be5\u5bf9\u8c61\u7684\u9501\u6c60\u4e2d\uff0c\u9501\u6c60\u4e2d\u7684\u7ebf\u7a0b\u4f1a\u53bb\u7ade\u4e89 \u8be5\u5bf9\u8c61\u9501\u3002<br \/>\n\u4f18\u5148\u7ea7\u9ad8\u7684\u7ebf\u7a0b\u7ade\u4e89\u5230\u5bf9\u8c61\u9501\u7684\u6982\u7387\u5927\uff0c\u5047\u82e5\u67d0\u7ebf\u7a0b\u6ca1\u6709\u7ade\u4e89\u5230\u8be5\u5bf9\u8c61\u9501\uff0c\u5b83\u8fd8\u4f1a\u7559\u5728\u9501\u6c60\u4e2d\uff0c \u552f\u6709\u7ebf\u7a0b\u518d\u6b21\u8c03\u7528wait()\u65b9\u6cd5\uff0c\u5b83\u624d\u4f1a\u91cd\u65b0\u56de\u5230\u7b49\u5f85\u6c60\u4e2d\u3002\u800c\u7ade\u4e89\u5230\u5bf9\u8c61\u9501\u7684\u7ebf\u7a0b\u5219\u7ee7\u7eed \u5f80\u4e0b\u6267\u884c\uff0c\u76f4\u5230\u6267\u884c\u5b8c\u4e86 synchronized\u4ee3\u7801\u5757\uff0c\u5b83\u4f1a\u91ca\u653e\u6389\u8be5\u5bf9\u8c61\u9501\uff0c\u8fd9\u65f6\u9501\u6c60\u4e2d\u7684\u7ebf\u7a0b\u4f1a \u7ee7\u7eed\u7ade\u4e89\u8be5\u5bf9\u8c61\u9501\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728Java\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u914d\u5408\u8c03\u7528Object\u5bf9\u8c61\u7684wait()\u65b9\u6cd5\u548cnotify()\u65b9\u6cd5\u6216notifyAll ( [&hellip;]<\/p>\n","protected":false},"author":7,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[118],"tags":[],"class_list":["post-10629","post","type-post","status-publish","format-standard","hentry","category-java"],"_links":{"self":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/10629","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/comments?post=10629"}],"version-history":[{"count":1,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/10629\/revisions"}],"predecessor-version":[{"id":10630,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/10629\/revisions\/10630"}],"wp:attachment":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/media?parent=10629"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/categories?post=10629"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/tags?post=10629"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}