{"id":283,"date":"2020-03-28T22:12:56","date_gmt":"2020-03-28T14:12:56","guid":{"rendered":"https:\/\/www.zeroheart.xyz\/wordpress\/?p=283"},"modified":"2020-03-30T17:15:19","modified_gmt":"2020-03-30T09:15:19","slug":"%e5%85%ac%e5%b9%b3%e9%94%81%e5%92%8c%e9%9d%9e%e5%85%ac%e5%b9%b3%e9%94%81%e7%9a%84%e5%ae%9e%e7%8e%b0","status":"publish","type":"post","link":"https:\/\/www.zeroheart.xyz\/wordpress\/?p=283","title":{"rendered":"\u516c\u5e73\u9501\u548c\u975e\u516c\u5e73\u9501\u7684\u5b9e\u73b0"},"content":{"rendered":"\n<p>ReentrantLock\u4e2d\uff0c\u63d0\u4f9b\u516c\u5e73\u9501\u548c\u975e\u516c\u5e73\u9501\uff0c\u901a\u8fc7\u6784\u9020\u51fd\u6570ReentrantLock(boolean fair)\u5b9e\u73b0\u3002<\/p>\n\n\n\n<p>\u5177\u4f53\u60c5\u51b5\u662f\u5728\u5185\u5b58\u4e2d\u7ef4\u62a4\u4e86\u4e00\u4e2a\u53cc\u5411\u94fe\u8868\uff0c\u975e\u516c\u5e73\u60c5\u51b5\u4e0b\u83b7\u53d6\u9501\u65f6\uff0c\u65b0\u52a0\u5165\u7684\u7ebf\u7a0b\u6709\u4f18\u52bf\uff0c\u53ef\u4ee5\u83b7\u5f97\u9501\u3002\u4ee3\u7801\u6bb5\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"> <br><br> final boolean nonfairTryAcquire(int acquires) {<br>         final Thread current = Thread.currentThread();<br> &nbsp; &nbsp; &nbsp; &nbsp; int c = getState();<br> &nbsp; &nbsp; &nbsp; &nbsp; if (c == 0) {&nbsp;<br>         \/\/\u5982\u679c\u6ca1\u6709\u7ebf\u7a0b\u83b7\u53d6\u9501\uff0c\u5219\u5f53\u524d\u7ebf\u7a0bCAS\u83b7\u53d6\u9501\u3002\u5e76\u8bbe\u7f6e\u81ea\u5df1\u4e3a\u5f53\u524d\u9501\u7684\u72ec\u5360\u7ebf\u7a0b<br> &nbsp; &nbsp; &nbsp; &nbsp;     if (compareAndSetState(0, acquires)) {<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;     setExclusiveOwnerThread(current);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;     return true;<br> &nbsp; &nbsp; &nbsp; &nbsp;     }<br> &nbsp;&nbsp; &nbsp; &nbsp;  }<br> &nbsp; &nbsp; &nbsp; &nbsp; else if (current == getExclusiveOwnerThread()) {<br> &nbsp; &nbsp; &nbsp; &nbsp;      \/\/ \u5982\u679c\u5b58\u5728\u9501\u7ade\u4e89\uff0c\u5224\u65ad\u83b7\u53d6\u9501\u7684\u7ebf\u7a0b\u662f\u5426\u662f\u5f53\u524d\u7ebf\u7a0b\uff0c \u56e0\u4e3aReentrantLock\u662f\u53ef\u91cd\u5165\u9501\uff0c<br> &nbsp; &nbsp; &nbsp; &nbsp;     \/\/\u7528state\u53d8\u91cf\u8868\u793a\u91cd\u5165\u6b21\u6570\uff0c\u5373\uff1a\u4f7f\u5f53\u524dstate+1\uff1b<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int nextc = c + acquires;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (nextc &lt; 0) \/\/ overflow<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; throw new Error(\"Maximum lock count exceeded\");<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; setState(nextc);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return true;<br> &nbsp; &nbsp; &nbsp; &nbsp; }<br> &nbsp; &nbsp; &nbsp; &nbsp; \/\/\u5982\u679c\u4e0d\u662f\u5f53\u524d\u7ebf\u7a0b\uff0c\u5219\u4e0d\u80fd\u83b7\u53d6\u540c\u6b65\u72b6\u6001<br> &nbsp; &nbsp; &nbsp; &nbsp; return false;<br>}<br><\/pre>\n\n\n\n<p>\u53ef\u4ee5\u6ce8\u610f\u5230\u53ea\u8981\u7ebf\u7a0b\u83b7\u53d6\u9501\uff0c\u5e76\u4e14\u9501\u4e3a\u7a7a\u95f2\u7684\u60c5\u51b5\u4e0b\uff0c\u76f4\u63a5\u5f97\u5230\u9501\u4e86\u3002\u800c\u516c\u5e73\u9501\u591a\u4e86\u4e00\u4e2a\u5224\u65ad\u3002!hasQueuedPredecessors() <\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">if (c == 0) {<br>    if (<strong><em>!hasQueuedPredecessors()<\/em><\/strong> &amp;&amp; compareAndSetState(0, acquires)) {<br>        setExclusiveOwnerThread(current);<br>        return true;<br>    }<br>}<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">public final boolean hasQueuedPredecessors() {<br>         Node t = tail; \/\/ Read fields in reverse initialization order<br>         Node h = head;<br>         Node s;<br>         return h != t &amp;&amp; ((s = h.next) == null || s.thread != Thread.currentThread());<br>}<\/pre>\n\n\n\n<p>hasQueuedPredecessors()\u65b9\u6cd5\uff0c\u4e3b\u8981\u5224\u65ad\u4e0a\u8ff0\u7684\u53cc\u5411\u94fe\u8868\uff0c\u5982\u679c\u961f\u5217\u4e3a\u7a7a\u6216\u8005\u961f\u5217\u4e0d\u4e3a\u7a7a\uff0c\u5f53\u524d\u7ebf\u7a0b\u4e3a\u7b2c\u4e00\u4e2a\u7b49\u5f85\u7684\u7ebf\u7a0b\uff0c\u90a3\u4e48\u83b7\u5f97\u9501\u3002<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>ReentrantLock\u4e2d\uff0c\u63d0\u4f9b\u516c\u5e73\u9501\u548c\u975e\u516c\u5e73\u9501\uff0c\u901a\u8fc7\u6784\u9020\u51fd\u6570ReentrantLock(boolean f [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[],"_links":{"self":[{"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/283"}],"collection":[{"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=283"}],"version-history":[{"count":11,"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/283\/revisions"}],"predecessor-version":[{"id":294,"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/283\/revisions\/294"}],"wp:attachment":[{"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=283"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=283"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=283"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}