{"id":109863,"date":"2020-06-01T20:50:24","date_gmt":"2020-06-01T12:50:24","guid":{"rendered":"http:\/\/4563.org\/?p=109863"},"modified":"2020-06-01T20:50:24","modified_gmt":"2020-06-01T12:50:24","slug":"java-%e5%a4%9a%e7%ba%bf%e7%a8%8b%e9%97%ae%e9%a2%98","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=109863","title":{"rendered":"Java \u591a\u7ebf\u7a0b\u95ee\u9898"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  Java \u591a\u7ebf\u7a0b\u95ee\u9898               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : linxiaoziruo <\/span>  <span><i><\/i> 1<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p>\u4e3a\u4ec0\u4e48\u4f7f\u7528 notify \u548c notifyAll \u7684\u65f6\u5019\u8981\u5148\u83b7\u5f97\u9501?<\/p>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>13<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"1831111\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : cs419 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u6ca1\u8fd9\u4e2a\u8bf4\u6cd5 \u4e0d\u642d\u560e<\/p>\n<p>\u4f60\u60f3\u60f3<br \/>1. \u4e3a\u5565\u8981\u7528\u9501\uff0c\u4e0d\u7528\u9501\u4f1a\u51fa\u5565\u95ee\u9898 \uff08\u539f\u5b50\u6027\uff09<br \/>2. \u9762\u5bf9\u8fd9\u4e2a\u95ee\u9898 \u600e\u4e48\u7528 notify \u53bb\u89e3\u51b3 \uff08\u7ebf\u7a0b\u7b49\u5f85\uff09<\/p>\n<p>\u5982\u679c\u786e\u5b9e\u8981\u7ebf\u7a0b\u7b49\u5f85<br \/>\u4e00\u822c\u4e0d\u7528 notify \u7528 LockSupport                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1831112\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : linxiaoziruo <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @cs419 \u600e\u4e48\u4f1a\u4e0d\u642d\u560e\u5462\uff0c\u4f7f\u7528 notify \u548c notifyAll \u7684\u65f6\u5019\u5fc5\u987b\u8981\u5728 syncnorize \u7684\u4e34\u754c\u533a\u5185\uff0c\u5373\u5fc5\u987b\u5148\u83b7\u5f97\u8fd9\u4e2a\u5bf9\u8c61\u9501\u624d\u80fd\u6267\u884c notify\uff0c\u5426\u5219\u4f1a\u629b\u51fa IllegalMonitorStateException                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1831113\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : linxiaoziruo <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @cs419 \u9501\u662f\u89e3\u51b3\u5171\u4eab\u8d44\u6e90\u7ade\u4e89\u5f15\u8d77\u7684\u7ebf\u7a0b\u5b89\u5168\u95ee\u9898\uff0c\u548c\u539f\u5b50\u6027\u6709\u4ec0\u4e48\u5173\u8054\u5462\uff0c\u539f\u5b50\u6027\u6307\u7684\u662f\u4e0d\u80fd\u4e2d\u65ad\u7684\u64cd\u4f5c\uff0c\u548c\u9501\u662f\u4e24\u4e2a\u6ca1\u6709\u5173\u8054\u7684\u6982\u5ff5\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1831114\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : 23571113 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             notify \u5c31\u662f\u901a\u77e5\u91ca\u653e\u9501\u554a, \u4f60\u6ca1\u9501\u600e\u4e48\u91ca\u653e.                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1831115\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : pursuer <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             notify\uff0cwait\uff0c\u548c\u6761\u4ef6\u5224\u65ad\u901a\u5e38\u662f\u4e00\u8d77\u7528\u7684\uff0c\u6761\u4ef6\u5224\u65ad\u540e\u9009\u62e9\u8fdb\u5165\u7b49\u5f85\u6216\u7ee7\u7eed\u8fd0\u884c\uff0c\u8fd9\u4e2a\u65f6\u5019\u8981\u4fdd\u8bc1\u6761\u4ef6\u5224\u65ad\u540e\uff0c\u6761\u4ef6\u4e0d\u4f1a\u88ab\u5176\u4ed6\u7ebf\u7a0b\u6539\u53d8\uff0c\u6240\u4ee5\u901a\u5e38\u90fd\u4f1a\u6709\u4e2a\u9501\u4fdd\u969c\u7c7b\u4f3c\u7684\u573a\u666f\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1831116\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : sioncheng <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             notify  wait \u662f\u4e00\u79cd\u534f\u4f5c\u5427\uff0c\u53cc\u65b9\u9700\u8981\u80fd\u83b7\u5f97\u76f8\u540c\u7684\u9501\u624d\u80fd\u8bf4\u660e\u662f\u662f\u5728\u534f\u4f5c\u5e72\u4e00\u4e2a\u4e8b\u60c5\uff0c\u8981\u4e0d\u7136\uff0c\u4e0d\u76f8\u5e72\u7684\u53c2\u4e0e\u8005\u778e notify \u80fd\u884c\u5417                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1831117\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : lux182 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u91ca\u653e monitor<br \/>\u5524\u9192\u7b49\u5f85\u961f\u5217\u7684\u7ebf\u7a0b                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1831118\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : seki4713 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u9632\u6b62\u4f60\u6761\u4ef6\u5224\u65ad\u6210\u529f\u540e\u7a81\u7136\u88ab\u62a2\u5360 \u7136\u540e\u6761\u4ef6\u88ab\u4fee\u6539 \u4fdd\u8bc1\u5224\u65ad\u6761\u4ef6\u5230\u8fdb\u5165\u4e34\u754c\u533a\u7684\u6574\u4e2a\u64cd\u4f5c\u662f\u539f\u5b50\u7684                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1831119\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : bigbyto <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u8fd9\u4e2a\u95ee\u9898\u5176\u5b9e\u771f\u4e0d\u597d\u56de\u7b54\uff0c\u5c31\u50cf\u5927\u5bb6\u4e60\u4ee5\u4e3a\u5e38\u7684\u95ee\u9898\uff0c\u7a81\u7136\u88ab\u95ee\u4e3a\u4ec0\u4e48\u4e86\u3002\u5176\u5b9e\u4e0a\u9762\u7684\u8001\u54e5\u90fd\u6ca1\u56de\u7b54\u5230\u70b9\u4e0a\uff0c\u6211\u89c9\u5f97\u4e3a\u4ec0\u4e48 notify \u5fc5\u987b\u8981 synchorized\uff0c\u6839\u672c\u539f\u56e0\u5728\u4e8e wait set \u7684\u64cd\u4f5c\u662f\u539f\u5b50\u7684\u3002\u5148\u770b\u770b JLS \u5bf9 wait set \u7684\u63cf\u8ff0\u3002<\/p>\n<p>&gt; Every object, in addition to having an associated monitor, has an associated *wait set*. A wait set is a set of threads.<br \/>&gt;<br \/>&gt; When an object is first created, its wait set is empty. **Elementary actions that add threads to and remove threads from wait sets are atomic**. Wait sets are manipulated solely through the methods `Object.wait`, `Object.notify`, and `Object.notifyAll`.<br \/>&gt;<br \/>&gt; Ref: https:\/\/docs.oracle.com\/javase\/specs\/jls\/se8\/html\/jls-17.html<\/p>\n<p>\u7b80\u5355\u6765\u8bf4\uff0c\u5f53\u5bf9\u8c61\u521b\u5efa\u65f6\uff0c\u4f1a\u987a\u5e26\u521b\u5efa Monitor \u548c Wait Set\uff0c\u8fd9\u4e9b\u5e94\u8be5\u662f\u5728 C \u8bed\u8a00\u5c42\u9762\u53bb\u521b\u5efa\u7684\u3002\u7136\u540e\u5b83\u544a\u8bc9\u6211\u4eec\u5bf9 Wait Set \u7684\u64cd\u4f5c\u90fd\u662f Atomic \u7684\uff0c\u8fd9\u5c31\u80fd\u89e3\u91ca\uff0c\u4e3a\u4ec0\u4e48 wait \u548c notify \u5fc5\u987b\u83b7\u5f97\u9501\uff0c\u56e0\u4e3a\u6ca1\u6709\u9501\uff0c\u5c31\u6ca1\u529e\u6cd5\u4fdd\u8bc1\u5bf9 wait set \u7684\u64cd\u4f5c\u662f\u539f\u5b50\u7684\u3002                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1831120\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : JasonLaw <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u6765\u81ea Java Concurrency in Practice \u3002<\/p>\n<p>Just as each Java object can act as a lock, each object can also act as a condition queue, and the wait, notify, and notifyAll methods in Object constitute the API for intrinsic condition queues. An object\u2019s intrinsic lock and its intrinsic condition queue are related: in order to call any of the condition queue methods on object X, you must hold the lock on X. This is because the mechanism for waiting for state-based conditions is necessarily tightly bound to the mechanism for preserving state consistency: you cannot wait for a condition unless you can examine the state, and you cannot release another thread from a condition wait unless you can modify the state.<\/p>\n<p>Object.wait atomically releases the lock and asks the OS to suspend the current thread, allowing other threads to acquire the lock and therefore modify the object state. Upon waking, it reacquires the lock before returning. Intuitively, calling wait means \u201cI want to go to sleep, but wake me when something interesting happens\u201d, and calling the notification methods means \u201csomething interesting happened\u201d.                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1831121\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : willxiang <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             &#8220;\u5fc5\u987b\u5728 synchronized \u5757\u4e2d\u624d\u80fd\u8c03\u7528 wait()\u65b9\u6cd5\uff0c\u56e0\u4e3a wait()\u65b9\u6cd5\u8c03\u7528\u65f6\uff0c\u4f1a\u91ca\u653e\u7ebf\u7a0b\u83b7\u5f97\u7684\u9501\uff0cwait()\u65b9\u6cd5\u8fd4\u56de\u540e\uff0c\u7ebf\u7a0b\u53c8\u4f1a\u91cd\u65b0\u8bd5\u56fe\u83b7\u5f97\u9501\u3002&#8221;<\/p>\n<p>https:\/\/www.liaoxuefeng.com\/wiki\/1252599548343744\/1306580911915042                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1831122\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : cs419 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u5ffd\u7136\u53d1\u73b0\u88ab\u964d\u6743\u4e86 \u6ca1\u6536\u5230\u6d88\u606f\u901a\u77e5<br \/>\u5947\u602a\u7684\u77e5\u8bc6\u589e\u52a0\u4e86 <br \/>\u6ca1\u89c9\u7740 wait \u597d\u7528 <br \/>\u5e73\u65f6\u8981\u4e48\u666e\u901a\u7684\u591a\u7ebf\u7a0b \u8981\u4e48 LockSupport \u8981\u4e48 JUC                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1831123\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : Kamiyu0087 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u76f4\u63a5\u4ece\u5b9e\u9645\u4f7f\u7528\u7684\u60c5\u51b5\u6765\u770b\u7684\u8bdd\uff0c\u4f60\u5982\u679c notify \u7684\u65f6\u5019\u4e0d\u53bb\u83b7\u5f97\u9501\u7684\u8bdd\uff0c\u6ca1\u6cd5\u4fdd\u8bc1 notify \u5728 wait \u4e4b\u540e\u518d\u6267\u884c\u554a<br \/>&#8220;`java<br \/> final Object lock = new Object();<br \/> synchronized (lock) {<br \/> new Thread(() -&gt; lock.notify()).start();<\/p>\n<p> try {<br \/> lock.wait();<br \/> } catch (InterruptedException e) {<br \/> e.printStackTrace();<br \/> }<br \/> }<br \/>&#8220;`                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>Java \u591a\u7ebf\u7a0b\u95ee\u9898 \u8cc7\u6df1\u5927\u4f6c :&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/109863"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=109863"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/109863\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=109863"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=109863"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=109863"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}