{"id":126034,"date":"2020-05-08T09:35:26","date_gmt":"2020-05-08T01:35:26","guid":{"rendered":"http:\/\/4563.org\/?p=126034"},"modified":"2020-05-08T09:35:26","modified_gmt":"2020-05-08T01:35:26","slug":"%e4%b8%ba%e5%95%a5-aqs-%e7%9a%84-condtionobject-%e7%9a%84-firstwaiter-%e4%b8%8d%e9%9c%80%e8%a6%81%e6%98%af-volatile-%e7%9a%84%ef%bc%9f","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=126034","title":{"rendered":"\u4e3a\u5565 AQS \u7684 CondtionObject \u7684 firstWaiter \u4e0d\u9700\u8981\u662f volatile \u7684\uff1f"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  \u4e3a\u5565 AQS \u7684 CondtionObject \u7684 firstWaiter \u4e0d\u9700\u8981\u662f volatile \u7684\uff1f               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : amiwrong123 <\/span>  <span><i><\/i> 8<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p>\u5176\u5b9e\u5173\u4e8e\u8fd9\u4e00\u70b9\uff0c\u7f51\u4e0a\u5927\u90e8\u5206\u4eba\u90fd\u4f1a\u8bf4\uff0c\u8981 await()\u5fc5\u7136\u8981\u5148 lock()\uff0c\u65e2\u7136 lock()\u4e86\u5c31\u8868\u793a\u6ca1\u6709\u7ade\u4e89\uff0c\u6ca1\u6709\u7ade\u4e89\u81ea\u7136\u4e5f\u6ca1\u5fc5\u8981\u4f7f\u7528 volatile+CAS \u7684\u673a\u5236\u53bb\u4fdd\u8bc1\u4ec0\u4e48\u3002\u6240\u4ee5 firstWaiter \u4e0d\u9700\u8981\u662f volatile \u7684\u3002<\/p>\n<p>\u4f46\u662f\u5b9e\u9645\u4e0a\u6211\u611f\u89c9\u8fd8\u662f\u4f1a\u6709\u95ee\u9898\uff0c\u6bd4\u5982\u4f60\u6267\u884c\u4e0b\u9762\u8fd9\u6bb5\u7a0b\u5e8f\uff1a<\/p>\n<pre><code>        final Lock lock=new ReentrantLock();         Condition Emptycondition = lock.newCondition();         Emptycondition.await();  \/\/\u8fd9\u6837\u4f1a\u629b\u51fa\u5f02\u5e38 <\/code><\/pre>\n<p>\u6bd4\u5982\u4f60\u8ba9\u4e00\u4e2a\u5b50\u7ebf\u7a0b\u53bb\u505a\u4e0a\u9762\u7684\u4e8b\uff0c\u4f1a\u629b\u51fa\u5f02\u5e38\uff0c\u56e0\u4e3a await()\u7684\u65f6\u5019\u6ca1\u6709\u83b7\u53d6\u9501\u5462\u3002\u4f46\u662f\u5728\u629b\u51fa\u5f02\u5e38\u4e4b\u524d\uff0c\u4f60\u5df2\u7ecf\u6267\u884c\u4e86\u4e00\u90e8\u5206\u4ee3\u7801\u4e86\uff1a<\/p>\n<pre><code>        public final void await() throws InterruptedException {             if (Thread.interrupted())                 throw new InterruptedException();             Node node = addConditionWaiter();             int savedState = fullyRelease(node);\/\/\u5728\u8fd9\u91cc\u629b\u51fa\u7684\u5f02\u5e38\uff0c\u610f\u5473\u7740\u5df2\u7ecf\u6267\u884c\u4e86 addConditionWaiter             int interruptMode = 0;             while (!isOnSyncQueue(node)) {                 LockSupport.park(this);                 if ((interruptMode = checkInterruptWhileWaiting(node)) != 0)                     break;             }             if (acquireQueued(node, savedState) &amp;&amp; interruptMode != THROW_IE)                 interruptMode = REINTERRUPT;             if (node.nextWaiter != null) \/\/ clean up if cancelled                 unlinkCancelledWaiters();             if (interruptMode != 0)                 reportInterruptAfterWait(interruptMode);         }      final int fullyRelease(Node node) {         boolean failed = true;         try {             int savedState = getState();             if (release(savedState)) {\/\/\u5177\u4f53\u7684\u8bf4\uff0c\u662f\u8fd9\u91cc\u629b\u51fa\u5f02\u5e38\u4e86                 failed = false;                 return savedState;             } else {                 throw new IllegalMonitorStateException();             }         } finally {             if (failed)\/\/\u629b\u51fa\u5f02\u5e38\u540e\uff0cfailed \u4e3a\u771f\uff0c\u4f46\u5584\u540e\u5904\u7406\u53ea\u662f\u5c06 node \u7684\u72b6\u6001\u53d8\u6210 CANCELLED                 node.waitStatus = Node.CANCELLED;         }     } <\/code><\/pre>\n<p>\u518d\u6765\u770b addConditionWaiter \u7684\u903b\u8f91\uff1a<\/p>\n<pre><code>        private Node addConditionWaiter() {             Node t = lastWaiter;             \/\/ If lastWaiter is cancelled, clean out.             if (t != null &amp;&amp; t.waitStatus != Node.CONDITION) {                 unlinkCancelledWaiters();                 t = lastWaiter;             }                  Node node = new Node(Thread.currentThread(), Node.CONDITION);             if (t == null)                 firstWaiter = node;\/\/\u5bf9\u4e0d\u4fdd\u8bc1\u53ef\u89c1\u6027\u7684\u53d8\u91cf\u8fdb\u884c\u8d4b\u503c\u4e86             else                 t.nextWaiter = node;             lastWaiter = node;\/\/\u5bf9\u4e0d\u4fdd\u8bc1\u53ef\u89c1\u6027\u7684\u53d8\u91cf\u8fdb\u884c\u8d4b\u503c\u4e86             return node;         } <\/code><\/pre>\n<p>\u597d\u4e86\uff0c\u73b0\u5728\u6267\u884c\u5b8c\u6211\u7684\u90a3\u4e09\u884c\u6d4b\u8bd5\u4ee3\u7801\u540e\uff0cEmptycondition \u7684\u6761\u4ef6\u961f\u5217\u4e0a\u6709\u4e86\u4e00\u4e2a node\uff0c\u800c\u4e14\u8fd9\u4e2a node \u5305\u88c5\u4e86\u4e00\u4e2a\u6b7b\u6389\u7684\u7ebf\u7a0b\u3002\u56e0\u4e3a\u629b\u51fa\u5f02\u5e38<\/p>\n<p>\u73b0\u5728\u5047\u8bbe\u6709\u53e6\u4e00\u4e2a\u7ebf\u7a0b\u6267\u884c\u6b63\u5e38\u6d41\u7a0b\u7684\u4ee3\u7801\uff1a<\/p>\n<pre><code>        final Lock lock=new ReentrantLock();         Condition Emptycondition = lock.newCondition();         lock.lock();         try{             Emptycondition.await();           } finally {             lock.unlock();         }          <\/code><\/pre>\n<p>\u73b0\u5728\u6761\u4ef6\u662f\uff1a<\/p>\n<ol>\n<li>\u7ebf\u7a0b A \u6267\u884c \u6d4b\u8bd5\u4ee3\u7801\uff0c\u4f1a\u6267\u884c\u5230 addConditionWaiter<\/li>\n<li>\u7ebf\u7a0b B \u6267\u884c \u6b63\u5e38\u6d41\u7a0b\u4ee3\u7801\uff0c\u4e5f\u4f1a\u6267\u884c\u5230 addConditionWaiter<\/li>\n<li>\u4e24\u4e2a\u7ebf\u7a0b\u53ef\u80fd\u540c\u65f6\u6267\u884c addConditionWaiter\uff0c\u5374\u6ca1\u6709\u5bf9 firstWaiter \u8fdb\u884c volatile+CAS \u7684\u4fdd\u62a4,\u96be\u9053\u8fd9\u6837\u4e0d\u4f1a\u6709\u95ee\u9898\u5417\uff1f<\/li>\n<\/ol><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>11<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"2006471\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : sagaxu <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u56e0\u4e3a\u4f60\u7684\u4ee3\u7801\u6ca1\u6709\u505a\u5230\u8981 await()\u5fc5\u7136\u8981\u5148 lock()                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"2006472\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : amiwrong123 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @sagaxu <br \/>\u989d\uff0c\u6211\u597d\u50cf\u6ca1\u61c2\u4f60\u610f\u601d\u3002\u6240\u4ee5\u4f60\u60f3\u8bf4 firstWaiter \u4e0d\u52a0 volatile\uff0c\u5c31\u4f1a\u6709\u95ee\u9898\u5457                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"2006473\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : muyunn <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u597d\u957f\u65f6\u95f4\u4e0d\u7528\uff0c\u5dee\u70b9\u88ab\u4e3b\u7ed5\u8fdb\u53bb\uff0c\u4f60\u5fc5\u987b\u5148\u8c03\u7528 lock.lock()\u83b7\u53d6\u9501\uff0c\u7136\u540e\u624d\u53ef\u4ee5 await                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"2006474\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : vk42 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             lz \u662f\u60f3\u8bf4\u4e0d\u5148 lock \u7684\u60c5\u51b5\u4e0b\u540e\u679c\u4e0d\u53ef\u63a7\uff1f\u4f46\u672c\u6765\u6ca1\u6309\u89c4\u5219\u5148 lock \u4e0d\u5c31\u662f bug \u5417\uff1f\u76f4\u63a5\u4fee bug \u4e0d\u662f\u66f4\u7b80\u5355\u4e48                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"2006475\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : luckyrayyy <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u6211\u7684\u54e5\u554a\uff0c\u4f60\u8981\u7814\u7a76\u4e00\u5e74 aqs \uff1f                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"2006476\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : amiwrong123 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @muyunn <br \/>\u662f\u7684\uff0c\u6807\u51c6\u7528\u6cd5\u662f\u8fd9\u6837                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"2006477\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : amiwrong123 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @vk42 <br \/>\u597d\u5427\uff0c\u6240\u4ee5\u76f8\u5f53\u4e8e\u51b3\u5b9a\u6743\u7ed9\u4e86\u7528\u6237\uff0c\u4f5c\u8005\u9ed8\u8ba4\u4f7f\u7528\u8005\u662f\u6309\u7167\u6b63\u786e\u7528\u6cd5\u4f7f\u7528\u7684                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"2006478\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : amiwrong123 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @luckyrayyy <br \/>\u54c8\u54c8\u54c8\uff0c\u6700\u8fd1\u53d1\u5e16\u592a\u591a\u88ab\u53d1\u73b0\u4e86\u5417\u3002\u8fd9\u51e0\u5929 condition \u7684\u5b9e\u73b0\u770b\u5b8c\uff0c\u5c31\u4e0d\u770b aqs \u4e86                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"2006479\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : yamasa <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u4f69\u670d\u8fd9\u725b\u89d2\u5c16\u7cbe\u795e\u3002\u867d\u7136\u6211\u89c9\u5f97\u73b0\u884c\u73af\u5883\u4e0b\u6b7b\u78d5 jdk \u6e90\u7801\u4e0d\u5982\u5237 leetcode \u6765\u7684\u73b0\u5b9e\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"2006480\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : amiwrong123 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @yamasa <br \/>\u54ce\uff0c\u6240\u4ee5\u6211\u662f\u53c8\u52aa\u529b\u9519\u65b9\u5411\u4e86\u5417 ==                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"2006481\" 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>                                                             \u6211\u89c9\u5f97\u5982\u679c\u4f60\u7528\u6700\u7b80\u6d01\u7684\u8bed\u8a00\u8868\u8ff0\u6e05\u695a\u4e86\uff0c\u53ef\u80fd\u4f60\u4e5f\u5c31\u627e\u5230\u7b54\u6848\u4e86                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>\u4e3a\u5565 AQS \u7684 Condtion&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\/126034"}],"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=126034"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/126034\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=126034"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=126034"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=126034"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}