{"id":134809,"date":"2020-07-21T07:57:43","date_gmt":"2020-07-20T23:57:43","guid":{"rendered":"http:\/\/4563.org\/?p=134809"},"modified":"2020-07-21T07:57:43","modified_gmt":"2020-07-20T23:57:43","slug":"concurrenthashmapaddcount-%e4%b8%ba%e4%bb%80%e4%b9%88%e5%8f%aa%e8%a6%81-cas-%e5%a4%b1%e8%b4%a5%e4%ba%86%ef%bc%8c%e5%b0%b1%e8%82%af%e5%ae%9a%e4%b8%8d%e6%89%a9%e5%ae%b9%e4%ba%86%ef%bc%9f","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=134809","title":{"rendered":"ConcurrentHashMap#addCount() \u4e3a\u4ec0\u4e48\u53ea\u8981 CAS \u5931\u8d25\u4e86\uff0c\u5c31\u80af\u5b9a\u4e0d\u6269\u5bb9\u4e86\uff1f"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  ConcurrentHashMap#addCount() \u4e3a\u4ec0\u4e48\u53ea\u8981 CAS \u5931\u8d25\u4e86\uff0c\u5c31\u80af\u5b9a\u4e0d\u6269\u5bb9\u4e86\uff1f               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : amiwrong123 <\/span>  <span><i><\/i> 6<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<pre><code>    private final void addCount(long x, int check) {         CounterCell[] as; long b, s;         if ((as = counterCells) != null ||             !U.compareAndSwapLong(this, BASECOUNT, b = baseCount, s = b + x)) {             CounterCell a; long v; int m;             boolean uncontended = true;             if (as == null || (m = as.length - 1) &lt; 0 ||                 (a = as[ThreadLocalRandom.getProbe() &amp; m]) == null ||                 !(uncontended =                   U.compareAndSwapLong(a, CELLVALUE, v = a.value, v + x))) {                  \/\/\u6267\u884c\u5230\u8fd9\u91cc\uff0c\u8981\u4e48 CAS \u4fee\u6539 baseCount \u5931\u8d25\u4e86\uff0c\u8981\u4e48 CAS \u4fee\u6539\u67d0\u4e2a cell \u7684\u503c\u5931\u8d25\u4e86                  \/\/\u4e4b\u540e\u5c06\u76f4\u63a5 return                 fullAddCount(x, uncontended);                 return;             }             if (check &lt;= 1)                 return;             s = sumCount();         }         \/\/\u6269\u5bb9\u90e8\u5206         if (check &gt;= 0) {             Node&lt;K,V&gt;[] tab, nt; int n, sc;             while (s &gt;= (long)(sc = sizeCtl) &amp;&amp; (tab = table) != null &amp;&amp;                    (n = tab.length) &lt; MAXIMUM_CAPACITY) {                 int rs = resizeStamp(n);                 if (sc &lt; 0) {                     if ((sc &gt;&gt;&gt; RESIZE_STAMP_SHIFT) != rs || sc == rs + 1 ||                         sc == rs + MAX_RESIZERS || (nt = nextTable) == null ||                         transferIndex &lt;= 0)                         break;                     if (U.compareAndSwapInt(this, SIZECTL, sc, sc + 1))                         transfer(tab, nt);                 }                 else if (U.compareAndSwapInt(this, SIZECTL, sc,                                              (rs &lt;&lt; RESIZE_STAMP_SHIFT) + 2))                     transfer(tab, null);                 s = sumCount();             }         }     } <\/code><\/pre>\n<p>\u5c31\u662f\u4e0d\u5927\u7406\u89e3\u8fd9\u4e2a\u610f\u601d\u3002<\/p>\n<p>\u8fd9\u6837\u7684\u505a\u6cd5\uff0c\u5c82\u4e0d\u662f\u4f1a\u5bfc\u81f4\uff0c\u8be5\u6269\u5bb9\u7684\u65f6\u5019\u4e0d\u4f1a\u6269\u5bb9\u3002\u56e0\u4e3a\u6bd5\u7adf\u53ea\u6709 CAS \u76f4\u63a5\u6210\u529f\u7684\u7ebf\u7a0b\uff0c\u624d\u53ef\u80fd\u6269\u5bb9\u3002<\/p>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>2<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"2474724\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : jasonyee <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             CAS \u5931\u8d25\u8bf4\u660e\u88ab\u5176\u4ed6\u7ebf\u7a0b\u6269\u5bb9\u4e86 \u5c31\u4e0d\u9700\u8981\u672c\u7ebf\u7a0b\u64cd\u4f5c\u4e86                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"2474725\" 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>                                                             @jasonyee <br \/>\u6bd4\u5982\u8fd9\u79cd\u573a\u666f\uff0c\u7ebf\u7a0b A CAS \u6210\u529f\uff0c\u6b64\u65f6\u5927\u5c0f\u4e3a\u9608\u503c-1 \uff1b\u7136\u540e\u7ebf\u7a0b B CAS \u5931\u8d25\uff0c\u4e4b\u540e\u6267\u884c fulladdcount \u540e\u5927\u5c0f\u521a\u597d\u4e3a\u9608\u503c\uff0c\u4f46\u7531\u4e8e\u5931\u8d25\uff0c\u4e0d\u4f1a\u53bb\u6269\u5bb9\uff1b\u53ea\u6709\u7b49\u5230\u7b2c 3 \u4e2a\u7ebf\u7a0b\u5230\u6765\uff0c\u624d\u53ef\u80fd\u53bb\u6269\u5bb9\u4e86\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>ConcurrentHashMap&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\/134809"}],"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=134809"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/134809\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=134809"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=134809"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=134809"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}