{"id":653,"date":"2013-12-20T10:53:43","date_gmt":"2013-12-20T02:53:43","guid":{"rendered":"http:\/\/coderbee.net\/?p=653"},"modified":"2013-12-23T21:43:28","modified_gmt":"2013-12-23T13:43:28","slug":"juc-%e6%ba%90%e7%a0%81%e5%88%86%e6%9e%90-%e4%b8%89-wait-notify-notifyall-%e4%b8%8e-%e6%9d%a1%e4%bb%b6%e5%af%b9%e8%b1%a1","status":"publish","type":"post","link":"https:\/\/coderbee.net\/index.php\/concurrent\/20131220\/653","title":{"rendered":"JUC \u6e90\u7801\u5206\u6790 \u56db wait notify notifyAll \u4e0e \u6761\u4ef6\u5bf9\u8c61"},"content":{"rendered":"<h2>\u5185\u7f6e\u9501 \u4e0e wait notify \u673a\u5236<\/h2>\n<p>\u6bcf\u4e2aJava\u5bf9\u8c61\u90fd\u6709\u4e00\u4e2a\u5185\u7f6e\u9501\uff0c\u901a\u8fc7 <code>synchronized<\/code> \u5173\u952e\u5b57\u4f7f\u7528\u3002\u7ebf\u7a0b\u4e4b\u95f4\u53ef\u4ee5\u901a\u8fc7 Object \u7c7b\u7684 <code>wait, notify, notifyAll<\/code> \u8fdb\u884c\u534f\u8c03\u3002<\/p>\n<p><code>wait, notify, notifyAll<\/code> \u65b9\u6cd5\u8bf4\u660e\uff1a<\/p>\n<ul>\n<li><code>wait<\/code>\uff1a\u5728\u5176\u4ed6\u7ebf\u7a0b\u8c03\u7528\u6b64\u5bf9\u8c61\u7684 <code>notify()<\/code> \u65b9\u6cd5\u6216 <code>notifyAll()<\/code> \u65b9\u6cd5\u524d\uff0c\u5bfc\u81f4\u5f53\u524d\u7ebf\u7a0b\u7b49\u5f85\u3002\u5f53\u524d\u7ebf\u7a0b\u5fc5\u987b\u62e5\u6709\u6b64\u5bf9\u8c61\u76d1\u89c6\u5668\u3002\u8be5\u7ebf\u7a0b\u53d1\u5e03\u5bf9\u6b64\u76d1\u89c6\u5668\u7684\u6240\u6709\u6743\u5e76\u7b49\u5f85\uff0c\u76f4\u5230\u5176\u4ed6\u7ebf\u7a0b\u901a\u8fc7\u8c03\u7528 <code>notify<\/code> \u65b9\u6cd5\uff0c\u6216 <code>notifyAll<\/code> \u65b9\u6cd5\u901a\u77e5\u5728\u6b64\u5bf9\u8c61\u7684\u76d1\u89c6\u5668\u4e0a\u7b49\u5f85\u7684\u7ebf\u7a0b\u9192\u6765\u3002\u7136\u540e\u8be5\u7ebf\u7a0b\u5c06\u7b49\u5230\u91cd\u65b0\u83b7\u5f97\u5bf9\u76d1\u89c6\u5668\u7684\u6240\u6709\u6743\u540e\u624d\u80fd\u7ee7\u7eed\u6267\u884c\u3002<\/li>\n<li><code>notify<\/code>\uff1a\u5524\u9192\u5728\u6b64\u5bf9\u8c61\u76d1\u89c6\u5668\u4e0a\u7b49\u5f85\u7684\u5355\u4e2a\u7ebf\u7a0b\uff0c\u76f4\u5230\u5f53\u524d\u7ebf\u7a0b\u653e\u5f03\u6b64\u5bf9\u8c61\u4e0a\u7684\u9501\u5b9a\uff0c\u624d\u80fd\u7ee7\u7eed\u6267\u884c\u88ab\u5524\u9192\u7684\u7ebf\u7a0b\u3002\u5982\u679c\u6240\u6709\u7ebf\u7a0b\u90fd\u5728\u6b64\u5bf9\u8c61\u4e0a\u7b49\u5f85\uff0c\u5219\u4f1a\u9009\u62e9\u5524\u9192\u5176\u4e2d\u4e00\u4e2a\u7ebf\u7a0b\uff0c\u9009\u62e9\u662f\u4efb\u610f\u6027\u7684\uff0c\u88ab\u5524\u9192\u7684\u7ebf\u7a0b\u5c06\u4ee5\u5e38\u89c4\u65b9\u5f0f\u4e0e\u5728\u8be5\u5bf9\u8c61\u4e0a\u4e3b\u52a8\u540c\u6b65\u7684\u5176\u4ed6\u6240\u6709\u7ebf\u7a0b\u8fdb\u884c\u7ade\u4e89\u3002<\/li>\n<li><code>notifyAll<\/code>\uff1a\u5524\u9192\u5728\u6b64\u5bf9\u8c61\u76d1\u89c6\u5668\u4e0a\u7b49\u5f85\u7684\u6240\u6709\u7ebf\u7a0b\uff0c\u76f4\u5230\u5f53\u524d\u7ebf\u7a0b\u653e\u5f03\u6b64\u5bf9\u8c61\u4e0a\u7684\u9501\u5b9a\uff0c\u624d\u80fd\u7ee7\u7eed\u6267\u884c\u88ab\u5524\u9192\u7684\u7ebf\u7a0b\u3002\u88ab\u5524\u9192\u7684\u7ebf\u7a0b\u5c06\u4ee5\u5e38\u89c4\u65b9\u5f0f\u4e0e\u5728\u8be5\u5bf9\u8c61\u4e0a\u4e3b\u52a8\u540c\u6b65\u7684\u5176\u4ed6\u6240\u6709\u7ebf\u7a0b\u8fdb\u884c\u7ade\u4e89\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e00\u4e2a\u6709\u754c\u7f13\u5b58\u4e3a\u4f8b\uff0c\u5c55\u793a\u4e86\u5185\u7f6e\u9501\u548c <code>wait\u3001notify\u3001notifyAll<\/code> \u7684\u4e00\u822c\u7528\u6cd5\uff1a<\/p>\n<pre><code class=\"java\">public class BoundedBuffer&lt;T&gt; {\n       private final Object[] buffer;\n       private final int length;\n\n       public BoundedBuffer(int length ) {\n             if (length &lt; 0) {\n                   throw new IllegalArgumentException(\"length &lt; 0\");\n            }\n             this.length = length ;\n             buffer = new Object[length ];\n      }\n\n       \/\/ synchronized \u7528\u4e8e\u65b9\u6cd5\u4e0a\uff0c\u8868\u793a\u4e00\u4e2a\u540c\u6b65\u65b9\u6cd5\uff0c\u7ebf\u7a0b\u8fdb\u5165\u65b9\u6cd5\u524d\u81ea\u52a8\u83b7\u5f97\u5185\u7f6e\u9501\n       public synchronized void put(T obj) throws InterruptedException {\n             \/\/ \u7ebf\u7a0b\u88ab\u5524\u9192\u65f6\uff0c\u6761\u4ef6\u4e0d\u4e00\u5b9a\u6ee1\u8db3\uff08\u865a\u5047\u5524\u9192\uff09\uff0c\u6240\u4ee5\u9700\u8981\u5728\u5faa\u73af\u91cc\u8fdb\u884c\u6d4b\u8bd5\u3001\u7b49\u5f85\n             while (isFull()) {\n                   \/\/ \u5728\u5f53\u524d\u7684\u5bf9\u8c61\u5b9e\u4f8b\u4e0a\u7b49\u5f85\uff0c\u7531\u5176\u4ed6\u7ebf\u7a0b\u8c03\u7528 notifyAll \u6216 notify \u65b9\u6cd5\u5524\u9192\n                  wait();\n            }\n\n            doPut(obj);\n\n             \/\/ \u5524\u9192\u5728\u6b64\u5bf9\u8c61\u76d1\u89c6\u5668\u4e0a\u7b49\u5f85\uff08\u901a\u8fc7wait\u65b9\u6cd5\u8fdb\u5165\u7b49\u5f85\uff09\u7684\u6240\u6709\u7ebf\u7a0b\u3002\n             \/\/ \u76f4\u5230\u5f53\u524d\u7ebf\u7a0b\u653e\u5f03\u6b64\u5bf9\u8c61\u4e0a\u7684\u9501\u5b9a\uff0c\u624d\u80fd\u7ee7\u7eed\u6267\u884c\u88ab\u5524\u9192\u7684\u7ebf\u7a0b\u3002\n            notifyAll();\n\n      }\n\n       public synchronized T take() throws InterruptedException {\n            T object = null;\n             while (isEmpty()) {\n                  wait();\n            }\n            object = doTake();\n            notifyAll();\n             return object;\n      }\n\n       private void doPut(T obj) { \/\/ not implemented\n      }\n\n       private T doTake() { \/\/ not implemented\n             return null ;\n      }\n\n       private boolean isEmpty() { \/\/ not implemented\n             return false ;\n      }\n\n       private boolean isFull() { \/\/ not implemented\n             return false ;\n      }\n}\n<\/code><\/pre>\n<p><!--more--><\/p>\n<h2>Condition \u63a5\u53e3<\/h2>\n<p><code>java.util.concurrent.locks.Condition<\/code> \u5c06 <code>Object<\/code> \u7684\u76d1\u89c6\u5668\u65b9\u6cd5\uff08<code>wait\u3001notify<\/code> \u548c <code>notifyAll<\/code>\uff09\u5206\u89e3\u6210\u622a\u7136\u4e0d\u540c\u7684\u5bf9\u8c61\uff0c\u4ee5\u4fbf\u901a\u8fc7\u5c06\u8fd9\u4e9b\u5bf9\u8c61\u4e0e\u4efb\u610f <code>Lock<\/code> \u5b9e\u73b0\u7ec4\u5408\u4f7f\u7528\uff0c<strong>\u4e3a\u6bcf\u4e2a\u5bf9\u8c61\u63d0\u4f9b\u591a\u4e2a\u7b49\u5f85 set\uff08wait-set\uff09<\/strong>\u3002\u5176\u4e2d\uff0c<code>Lock<\/code> \u66ff\u4ee3\u4e86 <code>synchronized<\/code> \u65b9\u6cd5\u548c\u8bed\u53e5\u7684\u4f7f\u7528\uff0c<code>Condition<\/code> \u66ff\u4ee3\u4e86 <code>Object<\/code> \u76d1\u89c6\u5668\u65b9\u6cd5\u7684\u4f7f\u7528\u3002<\/p>\n<p><code>Condition<\/code> \u5b9e\u4f8b\u5b9e\u8d28\u4e0a\u88ab\u7ed1\u5b9a\u5230\u4e00\u4e2a\u9501\u4e0a\uff0c\u8981\u83b7\u5f97 <code>Condition<\/code> \u5b9e\u4f8b\u65f6\u53ef\u4ee5\u5728\u76ee\u6807 <code>Lock<\/code> \u4e0a\u8c03\u7528\u5176 <code>newCondition()<\/code> \u65b9\u6cd5\u3002<\/p>\n<p>\u4e0b\u9762\u662f\u7528\u663e\u5f0f\u9501\u52a0Condition\u5b9e\u73b0\u7684\u6709\u754c\u7f13\u5b58\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"java\">public class BoundedBufferWithCondition {\n       final Lock lock = new ReentrantLock(); \/\/ \u663e\u5f0f\u9501\n\n       \/\/ \u6761\u4ef6\u5bf9\u8c61\u7ef4\u62a4\u4e86\u5728\u6b64\u6761\u4ef6\u4e0a\u7684\u7b49\u5f85\u7ebf\u7a0b\n       final Condition notFull = lock.newCondition(); \/\/ \u975e\u6ee1\u7684\u7b49\u5f85\u7ebf\u7a0b\u961f\u5217\n       final Condition notEmpty = lock.newCondition(); \/\/ \u975e\u7a7a\u7684\u7b49\u5f85\u7ebf\u7a0b\u961f\u5217\n\n       final Object[] items = new Object[100];\n       int putptr , takeptr , count ;\n\n       public void put(Object x) throws InterruptedException {\n             \/\/ \u663e\u5f0f\u9501\u8981\u663e\u5f0f\u83b7\u53d6\n             lock.lock();\n\n             \/\/ \u9501\u4fdd\u62a4\u7684\u4ee3\u7801\u5fc5\u987b\u653e\u5728 try-finally \u8bed\u53e5\u5757\u4e2d\u6267\u884c\uff0c\u5728 finally \u8bed\u53e5\u5757\u4e2d\u786e\u4fdd\u663e\u5f0f\u9501\u91ca\u653e\n             try {\n                   while (count == items.length)\n                         notFull.await(); \/\/ \u7f13\u5b58\u662f\u6ee1\u7684\uff0cput \u7ebf\u7a0b\u52a0\u5165\u7684\u662f\u975e\u6ee1\u7b49\u5f85\u7ebf\u7a0b\u961f\u5217\n\n                   items[putptr ] = x;\n                   if (++putptr == items.length)\n                         putptr = 0;\n                  ++ count;\n\n                   notEmpty.signal(); \/\/ \u7f13\u5b58\u4e0d\u7a7a\uff0c\u9700\u8981\u901a\u77e5\u7684\u662f \u975e\u7a7a\u7684\u7b49\u5f85\u7ebf\u7a0b\uff0c\u4e0d\u4f1a\u901a\u77e5\u5230 put \u7ebf\u7a0b\n\n            } finally {\n                   \/\/ \u5728 finally \u8bed\u53e5\u5757\u4e2d\u786e\u4fdd\u663e\u5f0f\u9501\u91ca\u653e\n                   lock.unlock();\n            }\n      }\n\n       public Object take() throws InterruptedException {\n             lock.lock();\n             try {\n                   while (count == 0)\n                         notEmpty.await();\n                  Object x = items[takeptr ];\n                   if (++takeptr == items.length)\n                         takeptr = 0;\n                  -- count;\n                   notFull.signal();\n                   return x;\n            } finally {\n                   lock.unlock();\n            }\n      }\n}\n<\/code><\/pre>\n<h2>AbstractQueuedSynchronizer.ConditionObject<\/h2>\n<p><code>AbstractQueuedSynchronizer.ConditionObject<\/code> \u662fJUC\u5305\u91cc Condition \u63a5\u53e3\u7684\u4e00\u4e2a\u5b9e\u73b0\uff0c\u7531\u4e8e\u8fd9\u4e2a\u5b9e\u73b0\u4e0d\u662f\u7ebf\u7a0b\u5b89\u5168\u7684\uff0c\u6240\u4ee5\u5b83\u9700\u8981\u5728\u52a0\u9501\u7684\u60c5\u51b5\u4e0b\u8bbf\u95ee\u3002\u7528\u6cd5\u89c1\u4e0a\u8282\u3002<\/p>\n<h3>\u5c5e\u6027<\/h3>\n<p>\u5c5e\u6027\u5e76\u6ca1\u6709\u7528 <code>volatile<\/code> \u4fee\u9970\u7b26\u4fee\u9970\u3002\u6bcf\u4e2a\u6761\u4ef6\u90fd\u7ef4\u62a4\u4e00\u4e2a\u7b49\u5f85\u961f\u5217\u3002<\/p>\n<pre><code class=\"java\">private transient Node firstWaiter; \/\/ \u6761\u4ef6\u961f\u5217\u7684\u5934\u7ed3\u70b9\nprivate transient Node lastWaiter; \/\/ \u6761\u4ef6\u961f\u5217\u7684\u5c3e\u7ed3\u70b9\n<\/code><\/pre>\n<h3>\u961f\u5217\u7ef4\u62a4<\/h3>\n<p>\u4e3b\u8981\u6709\u6dfb\u52a0\u5230\u7b49\u5f85\u961f\u5217\u548c\u4ece\u961f\u5217\u79fb\u9664\u5df2\u53d6\u6d88\u7ed3\u70b9\u3002<\/p>\n<pre><code class=\"java\">private Node addConditionWaiter() {\n    Node t = lastWaiter;\n    \/\/ \u5982\u679c\u6700\u540e\u7684\u7ed3\u70b9\u4e0d\u5904\u4e8e\u6761\u4ef6\u7b49\u5f85\uff0c\u6e05\u9664\n    if (t != null &amp;&amp; t.waitStatus != Node.CONDITION) {\n        unlinkCancelledWaiters();\n        t = lastWaiter;\n    }\n    Node node = new Node(Thread.currentThread(), Node.CONDITION);\n    \/\/ \u5355\u5411\u94fe\u8868\u7684\u6dfb\u52a0\u64cd\u4f5c\n    if (t == null)\n        firstWaiter = node;\n    else\n        t.nextWaiter = node;\n    lastWaiter = node;\n    return node;\n}\n\n\/\/ \u628a\u5df2\u53d6\u6d88\u7684\u7ed3\u70b9\u4ece\u961f\u5217\u91cc\u79fb\u9664\u3002\u8fd9\u4e2a\u65b9\u6cd5\u9700\u8981\u907f\u514d\u5728\u7f3a\u5c11\u4fe1\u53f7\u7684\u65f6\u5019\u6301\u6709\u5783\u573e\u3002\nprivate void unlinkCancelledWaiters() {\n    Node t = firstWaiter;\n    Node trail = null;\n    \/\/ \u4ece\u5934\u5b8c\u6574\u904d\u5386\u961f\u5217\uff0c\u79fb\u9664\u975e\u7b49\u5f85\u7ed3\u70b9\u3002\n    while (t != null) {\n        Node next = t.nextWaiter;\n        if (t.waitStatus != Node.CONDITION) {\n            t.nextWaiter = null;\n             if (trail == null) {\n                firstWaiter = next;\n            } else {\n                trail.nextWaiter = next;\n            }\n            if (next == null) {\n                lastWaiter = trail;\n                 \/\/ break;\n            }\n        } else {\n            trail = t;\n        }\n        t = next;\n    }\n}\n<\/code><\/pre>\n<h3>await<\/h3>\n<p>await\u6709\u4e2a\u5148\u91ca\u653e\u9501\uff0c\u7b49\u5f85\u3001\u88ab\u5524\u9192\u540e\u518d\u83b7\u53d6\u9501\u7684\u8fc7\u7a0b\u3002\u91ca\u653e\u9501\u524d\u8981\u4fdd\u5b58\u9501\u7684\u72b6\u6001\uff0c\u7528\u4e8e\u6062\u590d\u3002<\/p>\n<pre><code class=\"java\">public final void await() throws InterruptedException {\n    if (Thread.interrupted())\n        throw new InterruptedException();\n    Node node = addConditionWaiter(); \/\/ \u628a\u7ed3\u70b9\u6dfb\u52a0\u5230\u7b49\u5f85\u7684\u6761\u4ef6\u961f\u5217\n\n    \/\/ \u6dfb\u52a0\u5230\u6761\u4ef6\u961f\u5217\u7684\u64cd\u4f5c\u5fc5\u987b\u5728\u4e0b\u9762\u7684\u91ca\u653e\u9501\u4e4b\u524d\u5b8c\u6210\n    int savedState = fullyRelease(node); \/\/ \u91ca\u653e\u9501\uff0c\u8fd4\u56de\u91ca\u653e\u524d\u7684\u72b6\u6001\uff0c\u56e0\u4e3a\u7ebf\u7a0b\u5524\u9192\u540e\u8981\u6062\u590d\u9501\u7684\u72b6\u6001\u3002\n    int interruptMode = 0;\n    while (!isOnSyncQueue(node)) { \/\/ \u5982\u679c\u7ed3\u70b9\u4e0d\u5728\u540c\u6b65\u83b7\u53d6\u7b49\u5f85\u961f\u5217\u4e0a\uff0c\u5219\u8fdb\u5165\u7b49\u5f85\uff08\u91ca\u653e\u9501\u4e4b\u540e\u5176\u4ed6\u7684\u5524\u9192\u7ebf\u7a0b\u4f1a\u628a\u5b83\u52a0\u5165\u540c\u6b65\u961f\u5217\uff09\n        LockSupport.park( this);\n\n        \/\/ checkInterruptWhileWaiting \u5728\u7ed3\u70b9\u6ca1\u6709\u53d6\u6d88\u7684\u60c5\u51b5\u4e0b\u4f1a\u628a\u7ed3\u70b9\u52a0\u5165sync\u961f\u5217\u3002\n        if ((interruptMode = checkInterruptWhileWaiting(node)) != 0)\n            break;\n    }\n    if (acquireQueued(node, savedState) &amp;&amp; interruptMode != THROW_IE) \/\/ \u91cd\u65b0\u83b7\u53d6\u9501\n        interruptMode = REINTERRUPT;\n    if (node.nextWaiter != null) \/\/ clean up if cancelled\n        unlinkCancelledWaiters();\n    if (interruptMode != 0)\n        reportInterruptAfterWait(interruptMode);\n}\n\n\/\/ \u5982\u679c\u7ed3\u70b9\u5df2\u7ecf\u5728\u7b49\u5f85\u83b7\u53d6\u7684\u540c\u6b65\u961f\u5217\u4e0a\u5c31\u8fd4\u56detrue\uff0c\u5426\u5219false\u3002\nfinal boolean isOnSyncQueue(Node node) {\n    if (node.waitStatus == Node.CONDITION || node.prev == null )\n        return false ;\n\n    \/\/ \u5982\u679c\u6709\u540e\u7ee7\uff0c\u80af\u5b9a\u5728\u961f\u5217\u4e0a\n    if (node.next != null ) \/\/ If has successor, it must be on queue\n        return true ;\n\n\n    \/*\n     * node.prev \u53ef\u4ee5\u662f\u975e\u7a7a\u7684\uff0c\u4f46\u7531\u4e8eCAS\u5931\u8d25\u8fd8\u6ca1\u6709\u52a0\u5165\u961f\u5217\u3002\n     * \u6240\u4ee5\u9700\u8981\u4ece\u5c3e\u90e8\u904d\u5386\u6765\u786e\u8ba4\u5b83\u52a0\u5165\u961f\u5217\u4e86\u3002\u5728\u8c03\u7528\u8fd9\u4e2a\u65b9\u6cd5\u65f6\uff0c\u5b83\u5c06\u603b\u662f\u5728\u9760\u8fd1\u5c3e\u90e8\u7684\u5730\u65b9\uff0c\n     * \u9664\u975eCAS\u5931\u8d25\uff08\u51e0\u4e4e\u4e0d\u53ef\u80fd\u7684\uff09\uff0c\u5b83\u5c31\u5728\u5c3e\u90e8\uff0c\u6240\u4ee5\u6211\u4eec\u51e0\u4e4e\u4e0d\u7528\u79fb\u52a8\u592a\u591a\u3002\n     *\/\n    return findNodeFromTail(node);\n}\n\nprivate boolean findNodeFromTail(Node node) {\n    Node t = tail;\n    for (;;) {\n        if (t == node)\n            return true ;\n        if (t == null)\n            return false ;\n        t = t.prev;\n    }\n}\n<\/code><\/pre>\n<h3>signal<\/h3>\n<pre><code class=\"java\">public final void signal() {\n    if (!isHeldExclusively()) \/\/ \u5fc5\u987b\u662f\u9501\u7684\u6301\u6709\u8005\u624d\u80fd\u91ca\u653e\u9501\n        throw new IllegalMonitorStateException();\n    Node first = firstWaiter;\n    if (first != null)\n        doSignal(first); \/\/ \u5524\u9192\u6761\u4ef6\u961f\u5217\u7684\u5934\u7ed3\u70b9\uff0c\u5982\u679c\u6709\n}\n\nprivate void doSignal(Node first) {\n    do {\n        if ( (firstWaiter = first.nextWaiter) == null)\n            lastWaiter = null;\n        first.nextWaiter = null;\n    } while (!transferForSignal(first) &amp;&amp; \/\/ transferForSignal\n             (first = firstWaiter) != null);\n}\n\n\/\/ \u628a\u7ed3\u70b9\u4ece\u6761\u4ef6\u961f\u5217\u8f6c\u79fb\u5230sync\u961f\u5217\nfinal boolean transferForSignal(Node node) {\n       \/\/ \u4e0d\u80fd\u4fee\u6539waitStatus\u8bf4\u660e\u7ed3\u70b9\u5df2\u7ecf\u88ab\u53d6\u6d88\n    if (!compareAndSetWaitStatus(node, Node.CONDITION, 0))\n        return false ;\n\n    \/\/ \u628a\u7ed3\u70b9\u52a0\u5165sync\u961f\u5217\uff0c\u5982\u679c\u53ef\u4ee5\u8bbe\u7f6e\u524d\u9a71waitStatus \u4ee5\u6307\u660e\u8fd9\u4e2a\u7ebf\u7a0b(\u53ef\u80fd)\u5728\u7b49\u5f85\u3002\n    \/\/ \u5982\u679c\u524d\u9a71\u5df2\u53d6\u6d88\u6216\u8bbe\u7f6e\u524d\u9a71waitStatus\u5931\u8d25\uff0c\u5524\u9192\u7ed3\u70b9\u53bbresync\n    \/\/\uff08\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u7ed3\u70b9\u7684waitStatus\u662f\u5904\u4e8e\u77ed\u6682\u3001\u65e0\u5bb3\u7684\u9519\u8bef\u72b6\u6001\uff09\u3002\/\/ \u56e0\u4e3a\u524d\u9762\u8bbe\u7f6e\u4e3a 0\n    Node p = enq(node);\n    int ws = p.waitStatus;\n    if (ws &gt; 0 || !compareAndSetWaitStatus(p, ws, Node.SIGNAL))\n        LockSupport.unpark(node.thread);\n    return true;\n}\n<\/code><\/pre>\n<p>AQS \u5e76\u6ca1\u6709\u4f7f\u7528\u5185\u7f6e\u7684\u76d1\u89c6\u5668\u9501\u6765\u5b9e\u73b0\u540c\u6b65\uff0c\u800c\u662f\u57fa\u4e8e <code>volatile<\/code> \u548c CAS \u64cd\u4f5c\u6765\u5b9e\u73b0\u540c\u6b65\uff0c\u5185\u5b58\u53ef\u89c6\u6027\u662f\u901a\u8fc7\u8fd9\u4e24\u4e2a\u64cd\u4f5c\u6240\u4ea7\u751f\u7684\u5185\u5b58\u5c4f\u969c\u6307\u4ee4\u6765\u5b9e\u73b0\u7684\u3002\u8fd9\u90fd\u662f\u57fa\u4e8e Java \u5b58\u50a8\u6a21\u578b\u7684\uff0c\u6240\u4ee5\u8981\u6df1\u5165\u7406\u89e3 JUC \u5305\uff0c\u8fd8\u662f\u9700\u8981\u5148\u7406\u89e3 Java \u5b58\u50a8\u6a21\u578b\uff0c\u5177\u4f53\u53ef\u89c1<a href=\"http:\/\/coderbee.net\/index.php\/concurrent\/20131219\/650\">\u8fd9\u91cc<\/a>\u3002<\/p>\n<hr\/>\n<p>\u6b22\u8fce\u5173\u6ce8\u6211\u7684\u5fae\u4fe1\u516c\u4f17\u53f7: <strong>coderbee\u7b14\u8bb0<\/strong>\uff0c\u53ef\u4ee5\u66f4\u53ca\u65f6\u56de\u590d\u4f60\u7684\u8ba8\u8bba\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"258\" height=\"258\" src=\"https:\/\/coderbee.net\/wp-content\/uploads\/2019\/01\/coderbee-note.jpg\" class=\"alignnone size-full wp-image-1707\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5185\u7f6e\u9501 \u4e0e wait notify \u673a\u5236 \u6bcf\u4e2aJava\u5bf9\u8c61\u90fd\u6709\u4e00\u4e2a\u5185\u7f6e\u9501\uff0c\u901a\u8fc7  &hellip; <a href=\"https:\/\/coderbee.net\/index.php\/concurrent\/20131220\/653\">\u7ee7\u7eed\u9605\u8bfb <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[121],"tags":[122,137,139,140,138],"_links":{"self":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/653"}],"collection":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/comments?post=653"}],"version-history":[{"count":2,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/653\/revisions"}],"predecessor-version":[{"id":677,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/653\/revisions\/677"}],"wp:attachment":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/media?parent=653"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/categories?post=653"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/tags?post=653"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}