{"id":2163,"date":"2021-06-07T21:36:32","date_gmt":"2021-06-07T13:36:32","guid":{"rendered":"https:\/\/coderbee.net\/?p=2163"},"modified":"2021-06-07T21:36:32","modified_gmt":"2021-06-07T13:36:32","slug":"concurrenthashmap","status":"publish","type":"post","link":"https:\/\/coderbee.net\/index.php\/concurrent\/20210607\/2163","title":{"rendered":"ConcurrentHashMap"},"content":{"rendered":"<h1>1. \u4e3a\u4ec0\u4e48 key \u548c value \u4e0d\u5141\u8bb8\u4e3a null<\/h1>\n<p>HashMap \u4e2d\u5141\u8bb8 key\u3001value \u4e3a null\uff0ckey \u4e3a null \u65f6\u54c8\u5e0c\u503c\u4e3a 0 \u3002<\/p>\n<p>ConcurrentHashMap \u4e2d\u90fd\u4e0d\u80fd\u4e3a null \u662f\u56e0\u4e3a\u4f5c\u8005 Doug Lea \u8ba4\u4e3a\uff1a\u5728\u5e76\u53d1\u7f16\u7a0b\u4e2d\uff0cnull \u503c\u5bb9\u6613\u5f15\u6765\u6b67\u4e49\uff0c\u5f53\u8c03\u7528 get(key) \u8fd4\u56de null \u65f6\uff0c\u65e0\u6cd5\u786e\u5b9a\u662f key \u5bf9\u5e94\u7684 value \u5c31\u662f null \uff0c\u8fd8\u662f\u8bf4\u8fd9\u4e2a key \u4e0d\u5b58\u5728\u3002<br \/>\n\u975e\u5e76\u53d1\u7f16\u7a0b\u4e2d\u53ef\u4ee5\u901a\u8fc7\u8c03\u7528 containsKey \u65b9\u6cd5\u6765\u5224\u65ad\uff0c\u4f46\u5e76\u53d1\u7f16\u7a0b\u4e2d\u65e0\u6cd5\u4fdd\u8bc1\u8fd9\u4e24\u4e2a\u65b9\u6cd5\u4e4b\u95f4\u6ca1\u6709\u5176\u4ed6\u7ebf\u7a0b\u6765\u4fee\u6539 key \u503c\u3002<\/p>\n<h1>2. \u5e76\u884c\u5ea6 concurrencyLevel<\/h1>\n<p>ConcurrentHashMap \u6784\u9020\u51fd\u6570\u91cc\u6709\u4e2a\u53c2\u6570 concurrencyLevel \u63d0\u4f9b\u4e86\u5efa\u8bae\u652f\u6301\u7684\u5e76\u884c\u5ea6\u3002<\/p>\n<p>\u5728 JDK 1.7 \u7684\u5b9e\u73b0\u91cc\uff0c\u5206\u6bb5\u9501\u6bb5\u6570\u7ec4\u7684\u5927\u5c0f\u7531 concurrencyLevel \u51b3\u5b9a\uff0c\u4e3a\u5927\u4e8e concurrencyLevel \u7684\u6700\u5c0f\u7684 2 \u6b21\u5e42\u503c\uff0c\u4f46\u4e0d\u80fd\u8d85\u8fc7 <code>2^16<\/code>\uff0c\u521d\u59cb\u5316\u540e\u4e0d\u80fd\u4fee\u6539\u3002<\/p>\n<p>\u5728 JDK 1.8 \u91cc\uff0cconcurrencyLevel \u4f1a\u5f71\u54cd Node \u6570\u7ec4\u7684\u521d\u59cb\u5bb9\u91cf\uff0c\u7531\u4e8e\u5e76\u53d1\u7c92\u5ea6\u662f\u6570\u7ec4\u7684\u5143\u7d20\uff0c\u4ece\u800c\u5f71\u54cd\u5e76\u53d1\u5ea6\u3002<\/p>\n<p>concurrencyLevel \u5e76\u4e0d\u662f\u6307\u5b9a\u4e86\u7cbe\u786e\u7684\u5e76\u53d1\u5ea6\u3002<\/p>\n<p><!--more--><\/p>\n<h1>3. ConcurrentHashMap JDK7<\/h1>\n<p>JDK 1.7 \u5e95\u5c42\u7ed3\u6784\uff1a\u5206\u6bb5\u9501 Segment \u6570\u7ec4 + HashEntry \u6570\u7ec4 + HashEntry \u94fe\u8868\u3002<\/p>\n<p>Segment \u7ee7\u627f\u81ea ReentrantLock\uff0c\u81ea\u8eab\u76f8\u5f53\u4e8e\u4e00\u4e2a\u5b9a\u5236\u7684\u54c8\u5e0c\u8868 \u3002<\/p>\n<p>\u5b9a\u4f4d\u5143\u7d20\u65f6\u9996\u5148\u8981\u5728\u6bb5\u6570\u7ec4\u8fdb\u884c\u5b9a\u4f4d\u3001\u518d\u5230\u6bb5\u91cc\u7684\u54c8\u5e0c\u8868\u8fdb\u884c\u5b9a\u4f4d\uff0c\u9700\u8981\u4e24\u6b21 hash \u8ba1\u7b97\u3002<\/p>\n<p>HashEntry \u7684 value \u548c next \u5b57\u6bb5\u7528 volatile \u4fee\u9970\uff0c\u7528\u4e8e\u4fdd\u8bc1\u53ef\u89c1\u6027\u3002<\/p>\n<p>\u63d2\u5165\u8282\u70b9\u78b0\u5230\u54c8\u5e0c\u51b2\u7a81\u65f6\uff0c\u5728\u94fe\u8868\u8868\u5934\u63d2\u5165\u3002<\/p>\n<pre><code class=\"java\">final int segmentMask;\nfinal int segmentShift;\n\n\/**\n * \u6bb5\u6570\u7ec4\uff0c\u6bcf\u4e2a\u5143\u7d20\u90fd\u662f\u4e00\u4e2a\u5b9a\u5236\u7684\u54c8\u5e0c\u8868\n *\/\nfinal Segment&lt;K,V&gt;[] segments;\n\ntransient Set&lt;K&gt; keySet;\ntransient Set&lt;Map.Entry&lt;K,V&gt;&gt; entrySet;\ntransient Collection&lt;V&gt; values;\n\nstatic final class Segment&lt;K,V&gt; extends ReentrantLock implements Serializable {\n    transient volatile HashEntry&lt;K,V&gt;[] table;\n    transient int count;\n    transient int modCount;\n    transient int threshold;\n    final float loadFactor;\n}\n\nstatic final class HashEntry&lt;K,V&gt; {\n    final int hash;\n    final K key;\n    volatile V value;\n    volatile HashEntry&lt;K,V&gt; next;\n}\n<\/code><\/pre>\n<h1>4. JDK 1.8 \u7684\u5b9e\u73b0<\/h1>\n<p>JDK 1.8 \u7684 ConcurrentHashMap \u662f\u5728 JDK 1.8 \u7684 HashMap \u4e0a\u5b9e\u73b0\u7ebf\u7a0b\u5b89\u5168\u7684\u3002<\/p>\n<p>\u5b58\u50a8\u7ed3\u6784\u91c7\u7528 Node \u6570\u7ec4 + \u94fe\u8868 + \u7ea2\u9ed1\u6811 \u7ed3\u6784\u3002\u7701\u53bb\u4e86\u5206\u6bb5\u9501\u6570\u7ec4\u90a3\u4e00\u5c42\uff0c\u7ed3\u6784\u4e0a\u4e0e HashMap \u76f8\u540c\u3002<\/p>\n<p>\u53d6\u6d88\u5206\u6bb5\u9501\uff0c\u901a\u8fc7 CAS + synchronized \u6765\u5b9e\u73b0\u66f4\u7ec6\u7c92\u5ea6\u7684\u9501\u4fdd\u62a4\u3002<\/p>\n<h2>4.1 \u4e3a\u4ec0\u4e48\u4f7f\u7528 synchronized \u800c\u4e0d\u662f ReentrantLock \uff1f<\/h2>\n<ol>\n<li>synchronized \u9501\u5728 JDK 1.6 \u505a\u4e86\u5927\u91cf\u7684\u6027\u80fd\u4f18\u5316\uff0c\u5f15\u5165\u591a\u79cd\u9501\u5f62\u6001\u4ee5\u63d0\u5347\u6027\u80fd\uff1a\u65e0\u9501 -> \u504f\u5411\u9501 -> \u8f7b\u91cf\u7ea7\u9501 -> \u91cd\u91cf\u7ea7\u9501 \u3002<\/li>\n<li>\u5982\u679c Node \u901a\u8fc7\u7ee7\u627f AQS \u6765\u83b7\u5f97\u540c\u6b65\u652f\u6301\uff0c\u4f1a\u5bfc\u81f4\u4e0d\u5fc5\u8981\u7684\u5185\u5b58\u5f00\u9500\uff0c\u56e0\u4e3a\u4e0d\u662f\u6240\u6709\u7684 Node \u90fd\u9700\u8981\u540c\u6b65\u652f\u6301\u7684\uff0c\u53ea\u6709 Node \u6570\u7ec4\u91cc\u7684\u624d\u9700\u8981\uff0cNode \u94fe\u8868\u4e0a\u7684\u662f\u4e0d\u9700\u8981\u7684\u3002<\/li>\n<\/ol>\n<h2>4.2 sizeCtl \u5b57\u6bb5<\/h2>\n<p>volatile \u4fee\u9970\u7684 sizeCtl \u7528\u4ee5\u63a7\u5236\u6570\u7ec4\u521d\u59cb\u5316\u6216\u6269\u5bb9\u7684\u5e76\u53d1\u63a7\u5236\u3002<\/p>\n<p>\u542b\u4e49\u5982\u4e0b\uff1a<br \/>\n* <code>sizeCtl = 0<\/code>\uff1a\u9ed8\u8ba4\u72b6\u6001\uff0c\u8868\u793a\u6570\u7ec4\u672a\u521d\u59cb\u5316\u3002<br \/>\n* <code>sizeCtl = -1<\/code>\uff1a\u8868\u793a\u5f53\u524d\u6b63\u5728\u521d\u59cb\u5316\u6570\u7ec4\u3002<br \/>\n* <code>sizeCtl &lt; -1<\/code>\uff1a\u8868\u793a\u6709 N-1 \u4e2a\u7ebf\u7a0b\u6b63\u5728\u6267\u884c\u6269\u5bb9\u64cd\u4f5c\u3002<code>-3<\/code> \u8868\u793a\u6709 <code>3-1<\/code> \u4e2a\u7ebf\u7a0b\u6b63\u5728\u6269\u5bb9\u3002<br \/>\n* <code>sizeCtl &gt; 0<\/code>\uff1a\u8868\u793a\u4e0b\u6b21\u6269\u5bb9\u65f6\u7684\u6570\u7ec4\u5927\u5c0f\u3002<\/p>\n<h2>4.3 \u5b89\u5168\u521d\u59cb\u5316\u6570\u7ec4<\/h2>\n<p>\u65b0\u7684\u5bb9\u91cf\u4e3a sizeCtl \u7684\u503c\uff0c\u6784\u5efa\u6570\u7ec4\u540e\u628a sizeCtl \u8d4b\u503c\u4e3a <code>n - (n &gt;&gt;&gt; 2)<\/code>\uff0c\u4e0b\u6b21\u6269\u5bb9\u65f6\u4f7f\u7528\uff08n \u4e3a\u672c\u6b21\u6269\u5bb9\u65f6\u4f7f\u7528\u7684\u957f\u5ea6\uff09\u3002<\/p>\n<pre><code>private final Node&lt;K,V&gt;[] initTable() {\n    Node&lt;K,V&gt;[] tab; int sc;\n    while ((tab = table) == null || tab.length == 0) {\n        if ((sc = sizeCtl) &lt; 0)  \/\/ \u6709\u7ebf\u7a0b\u5728\u6269\u5bb9\uff0c\u8ba9\u51fa CPU\n            Thread.yield(); \/\/ lost initialization race; just spin\n        \/\/ sizeCtl \u4e3a -1 \u8868\u793a\u6b63\u5728\u6269\u5bb9\uff0c\u901a\u8fc7 CAS \u8fdb\u884c\u62a2\u5360\n        else if (U.compareAndSwapInt(this, SIZECTL, sc, -1)) {\n            try {\n                if ((tab = table) == null || tab.length == 0) {\n                    int n = (sc &gt; 0) ? sc : DEFAULT_CAPACITY;\n                    @SuppressWarnings(\"unchecked\")\n                    Node&lt;K,V&gt;[] nt = (Node&lt;K,V&gt;[])new Node&lt;?,?&gt;[n];\n                    table = tab = nt;\n                    sc = n - (n &gt;&gt;&gt; 2);\n                }\n            } finally {\n                sizeCtl = sc;\n            }\n            break;\n        }\n    }\n    return tab;\n}\n<\/code><\/pre>\n<h2>4.3 get\/put \u53ef\u89c1\u6027\u4fdd\u8bc1<\/h2>\n<ol>\n<li>table \u672c\u8eab\u7528 volatile \u4fee\u9970\uff0c\u4fdd\u8bc1\u5bf9\u6570\u7ec4\u5f15\u7528\u7684\u53ef\u89c1\u6027\u3002<\/li>\n<li>\u901a\u8fc7 <code>Unsafe.getObjectVolatile<\/code> \u6765\u4fdd\u8bc1\u5bf9\u6570\u7ec4\u5143\u7d20\u8bbf\u95ee\u7684\u53ef\u89c1\u6027\u3002<\/li>\n<li>\u5f53\u6570\u7ec4\u5143\u7d20\u4e3a null \u65f6\uff0c\u901a\u8fc7 <code>Unsafe.compareAndSwapObject<\/code> \u6765\u4fdd\u8bc1\u5e76\u53d1\u5b89\u5168\u5730\u66f4\u65b0\u6570\u7ec4\u5143\u7d20\u3002<\/li>\n<li>\u5f53\u6570\u7ec4\u5143\u7d20\u4e0d\u4e3a\u7a7a\u65f6\uff0c\u901a\u8fc7 synchronized \u6765\u4fdd\u8bc1\u5e76\u53d1\u5b89\u5168\u3002<\/li>\n<\/ol>\n<h2>4.4 put \u903b\u8f91<\/h2>\n<ol>\n<li>\u6839\u636e key \u8ba1\u7b97\u51fa hash \u503c\u3002<\/li>\n<li>\u5224\u65ad\u662f\u5426\u9700\u8981\u8fdb\u884c\u521d\u59cb\u5316\u6570\u7ec4\u3002<\/li>\n<li>\u5b9a\u4f4d\u5230\u6570\u7ec4\u5143\u7d20 f\uff0c\u5224\u65ad f\uff1a<\/li>\n<\/ol>\n<ul>\n<li>3.1 \u5982\u679c\u4e3a null\uff0c\u5c1d\u8bd5\u901a\u8fc7 CAS \u6dfb\u52a0\u5230\u6570\u7ec4\u4e0a\u3002<\/li>\n<li>3.2 \u5982\u679c f.hash == MOVED(-1)\uff0c\u8bf4\u660e\u5176\u4ed6\u7ebf\u7a0b\u5728\u6269\u5bb9\uff0c\u5c1d\u8bd5\u53c2\u4e0e\u4e00\u8d77\u6269\u5bb9\u3002<\/li>\n<li>3.3 \u5982\u679c\u4ee5\u4e0a2\u70b9\u4e0d\u6ee1\u8db3\uff0csynchronized \u9501\u4f4f\u8282\u70b9 f\uff0c\u5224\u65ad\u662f\u94fe\u8868\u5219\u52a0\u5165\u5230\u94fe\u8868\u672b\u5c3e\uff0c\u662f\u7ea2\u9ed1\u6811\u5219\u63d2\u5165\u6811\u3002<\/li>\n<li>3.4 \u5982\u679c f \u65e2\u4e0d\u662f\u94fe\u8868\u4e5f\u4e0d\u662f\u7ea2\u9ed1\u6811\uff0c\u91cd\u65b0\u68c0\u6d4b\u6570\u7ec4\u5143\u7d20\u3002<\/li>\n<\/ul>\n<ol>\n<li>\u589e\u52a0 map \u7684\u5143\u7d20\u8ba1\u6570\u3002<\/li>\n<\/ol>\n<pre><code>final V putVal(K key, V value, boolean onlyIfAbsent) {\n    if (key == null || value == null) throw new NullPointerException();\n    int hash = spread(key.hashCode());  \/\/ \u8ba1\u7b97\u76ee\u6807\u69fd\u4f4d\n    int binCount = 0;\n    for (Node&lt;K,V&gt;[] tab = table;;) {\n        Node&lt;K,V&gt; f; int n, i, fh;\n        if (tab == null || (n = tab.length) == 0)\n            tab = initTable();  \/\/ \u521d\u59cb\u5316\u6570\u7ec4\n\n        \/\/ \u76ee\u6807\u69fd\u4f4d\u4e3a\u7a7a\uff0c\u901a\u8fc7 CAS \u8fdb\u884c\u8bbe\u7f6e\n        else if ((f = tabAt(tab, i = (n - 1) &amp; hash)) == null) {\n            if (casTabAt(tab, i, null,\n                         new Node&lt;K,V&gt;(hash, key, value, null)))\n                break;                   \/\/ no lock when adding to empty bin\n        }\n\n        \/\/ \u6b63\u5728\u8fdb\u884c resize \u64cd\u4f5c\uff0c\u534f\u52a9\u8f6c\u79fb\u5143\u7d20\u3002MOVED:-1\n        else if ((fh = f.hash) == MOVED)\n            tab = helpTransfer(tab, f);\n        else {\n            \/\/ \u5143\u7d20\u4e0d\u4e3a\u7a7a\uff0c\u9501\u5b9a\u76ee\u6807\u4e0b\u6807\u7684\u5143\u7d20\u3002\n            \/\/ \u7c92\u5ea6\u66f4\u7ec6\uff0c\u80fd\u652f\u6301\u7684\u5e76\u53d1\u5ea6\u6bd4 1.7 \u7248\u672c\u7684\u5206\u6bb5\u9501\u66f4\u9ad8\u3002\n            V oldVal = null;\n            synchronized (f) {\n                if (tabAt(tab, i) == f) {\n                    if (fh &gt;= 0) {  \/\/ \u54c8\u5e0c\u503c\u4e3a\u8d1f\u6570\u8868\u793a\u88ab\u8f6c\u79fb\u4e86\u3002\n                        binCount = 1;\n                        \/\/ \u904d\u5386\u94fe\u8868\n                        for (Node&lt;K,V&gt; e = f;; ++binCount) {\n                            K ek;\n                            if (e.hash == hash &amp;&amp;\n                                ((ek = e.key) == key ||\n                                 (ek != null &amp;&amp; key.equals(ek)))) {\n                                \/\/ key \u5df2\u5b58\u5728\uff0c\u66ff\u6362 value\uff0c\u8df3\u51fa\u5faa\u73af\u3002\n                                oldVal = e.val;\n                                if (!onlyIfAbsent)\n                                    e.val = value;\n                                break;\n                            }\n                            Node&lt;K,V&gt; pred = e;\n                            if ((e = e.next) == null) {\n                                \/\/ key \u4e0d\u5b58\u5728\uff0c\u52a0\u5165\u94fe\u8868\u672b\u5c3e\uff0c\u8df3\u51fa\u5faa\u73af\u3002\n                                pred.next = new Node&lt;K,V&gt;(hash, key,\n                                                          value, null);\n                                break;\n                            }\n                        }\n                    }\n                    else if (f instanceof TreeBin) {\n                        Node&lt;K,V&gt; p;\n                        \/\/ \u5982\u679c\u662f\u7ea2\u9ed1\u6811\uff0c\u56fa\u5b9a\u503c2\uff0c\u8df3\u8fc7\u4e0b\u9762\u7684\u8f6c\u6362\u4e3a\u6811\u7684\u68c0\u67e5\u3002\n                        binCount = 2;\n                        if ((p = ((TreeBin&lt;K,V&gt;)f).putTreeVal(hash, key,\n                                                       value)) != null) {\n                            oldVal = p.val;\n                            if (!onlyIfAbsent)\n                                p.val = value;\n                        }\n                    }\n                    \/\/ else \u8282\u70b9\u53ef\u80fd\u88ab\u8f6c\u79fb\u4e86\uff0c\u91cd\u65b0\u68c0\u6d4b\u5143\u7d20\u3002\n                }\n            }\n            if (binCount != 0) {\n                \/\/ \u53ea\u6709\u63d2\u5165\u5230\u94fe\u8868\u6210\u529f\u624d\u4f1a\u8fdb\u5165\u5230\u8fd9\u91cc\uff0c\u6ca1\u6709\u63d2\u5165\u94fe\u8868\u6216\u7ea2\u9ed1\u6811\u4ecd\u7136\u662f 0\uff0c\n                \/\/ \u63d2\u5165\u5230\u7ea2\u9ed1\u6811\u65f6\u662f 2\uff0c\u63d2\u5165\u94fe\u8868\u65f6\u662f\u94fe\u8868\u7684\u5b9e\u9645\u957f\u5ea6\u3002\n                if (binCount &gt;= TREEIFY_THRESHOLD)\n                    treeifyBin(tab, i);\n                if (oldVal != null)\n                    return oldVal;\n                break;\n            }\n        }\n    }\n    addCount(1L, binCount);\n    return null;\n}\n<\/code><\/pre>\n<h2>4.5 get \u903b\u8f91<\/h2>\n<ol>\n<li>\u901a\u8fc7 key \u7684 hashCode \u8ba1\u6570 hash \u503c h\u3002<\/li>\n<li>\u5982\u679c\u6570\u7ec4\u4e3a\u7a7a\u3001\u6216 <code>e = table[h &amp; (n-1)]<\/code> \u4e3a\u7a7a\uff0c\u8fd4\u56de null\u3002<\/li>\n<li>\u5982\u679c\u662f\u6570\u7ec4\u5143\u7d20 e \u7684\u9996\u8282\u70b9\uff0c\u76f4\u63a5\u8fd4\u56de\u3002<\/li>\n<li>\u5982\u679c\u5143\u7d20 e \u662f\u7ea2\u9ed1\u6811\uff0c\u4ece\u7ea2\u9ed1\u6811\u67e5\u627e\u3002<\/li>\n<li>\u5982\u679c\u662f\u94fe\u8868\uff0c\u5219\u904d\u5386\u5224\u65ad\u3002<\/li>\n<\/ol>\n<p>\u4e0d\u52a0\u9501\u7684\u539f\u56e0\uff1a<br \/>\n1. \u56e0\u4e3a Node \u7684 value \u548c next \u662f\u7528 volatile \u4fee\u9970\uff0c\u63d0\u4f9b\u5728\u591a\u7ebf\u7a0b\u4e0b\u7684\u53ef\u89c1\u6027\u3002<br \/>\n\u7531\u4e8e\u6ca1\u6709\u52a0\u9501\uff0c\u4e0d\u80fd\u5b8c\u5168\u4fdd\u8bc1\u4e00\u81f4\u6027\uff0c\u662f\u5f31\u4e00\u81f4\u6027\u7684\u3002<br \/>\n2. ConcurrentHashMap \u5199\u6570\u636e\u662f\u4e0d\u9700\u8981\u52062\u6b21\u5199\u5165\u7684\uff08\u6ca1\u6709\u4e2d\u95f4\u72b6\u6001\uff09\uff0c\u8bfb\u64cd\u4f5c\u4e5f\u4e0d\u9700\u89812\u6b21\u8bfb\u53d6\uff0c\u56e0\u6b64\u4e0d\u4f1a\u6709\u4e2d\u95f4\u72b6\u6001\u3002\uff08\u5982\u679c\u6709\u4e2d\u95f4\u72b6\u6001\uff0c\u5219\u9700\u8981\u52a0\u9501\u3002\uff09<\/p>\n<pre><code>public V get(Object key) {\n    Node&lt;K,V&gt;[] tab; Node&lt;K,V&gt; e, p; int n, eh; K ek;\n    int h = spread(key.hashCode());\n    if ((tab = table) != null &amp;&amp; (n = tab.length) &gt; 0 &amp;&amp;\n        (e = tabAt(tab, (n - 1) &amp; h)) != null) {\n        if ((eh = e.hash) == h) {\n            if ((ek = e.key) == key || (ek != null &amp;&amp; key.equals(ek)))\n                return e.val;\n        }\n        else if (eh &lt; 0)\n            return (p = e.find(h, key)) != null ? p.val : null;\n        while ((e = e.next) != null) {\n            if (e.hash == h &amp;&amp;\n                ((ek = e.key) == key || (ek != null &amp;&amp; key.equals(ek))))\n                return e.val;\n        }\n    }\n    return null;\n}\n<\/code><\/pre>\n<h2>4.6 \u8ba1\u6570\u4f18\u5316<\/h2>\n<p>\u5143\u7d20\u8ba1\u6570\u5f15\u5165\uff1a\u57fa\u7840\u8ba1\u6570\u5b57\u6bb5 baseCount + \u8ba1\u6570\u5355\u5143\u6570\u7ec4 counterCells \u7684\u673a\u5236\u3002\u4e0e <code>Striped64<\/code> \u4e00\u6837\u7684\u673a\u5236\u3002<\/p>\n<p>\u7ade\u4e89\u4e0d\u6fc0\u70c8\u65f6\uff0c\u901a\u8fc7 CAS \u5728\u57fa\u7840\u8ba1\u6570\u5b57\u6bb5 baseCount \u4e0a\u66f4\u65b0\u8ba1\u6570\u503c\uff0cCAS \u66f4\u65b0\u5931\u8d25\u5219\u9000\u5316\u5230\u901a\u8fc7 \u8ba1\u6570\u5355\u5143\u8fdb\u884c\u66f4\u65b0\uff0c\u968f\u673a\u9009\u62e9\u4e00\u4e2a\u5355\u5143\u8fdb\u884c\u66f4\u65b0\u3002<\/p>\n<pre><code>\/\/ \u57fa\u7840\u7684\u8ba1\u6570\u503c\uff0c\u901a\u8fc7 CAS \u66f4\u65b0\u3002\nprivate transient volatile long baseCount;\n\n\/\/ \u8ba1\u6570\u5355\u5143\u6570\u7ec4\uff0c\u957f\u5ea6\u4e3a 2 \u7684\u5e42\nprivate transient volatile CounterCell[] counterCells;\n\n@sun.misc.Contended static final class CounterCell {\n    volatile long value;\n    CounterCell(long x) { value = x; }\n}\n<\/code><\/pre>\n<h2>4.7 \u5c0f\u7ed3 JDK1.8 \u5b9e\u73b0<\/h2>\n<ol>\n<li>\u53d6\u6d88\u4e86\u5206\u6bb5\u9501\u6570\u7ec4\u3002\u5e95\u5c42\u7ed3\u6784\u91c7\u7528\uff1a\u6570\u7ec4+\u94fe\u8868+\u7ea2\u9ed1\u6811\u3002<\/li>\n<li>\u901a\u8fc7 CAS + synchronized \u6765\u5b9e\u73b0\u7ebf\u7a0b\u5b89\u5168\u3002<\/li>\n<li>\u94fe\u8868\u957f\u5ea6\u8fbe\u5230 8 \uff08\u5343\u4e07\u5206\u4e4b\u4e00\u7684\u6982\u7387\uff09\u65f6\u8f6c\u5316\u4e3a\u7ea2\u9ed1\u6811\uff0c\u7ea2\u9ed1\u6811\u8282\u70b9\u51cf\u5c11\u5230 6 \u4e2a\u65f6\u518d\u8f6c\u5316\u6210\u94fe\u8868\u3002<\/li>\n<li>\u94fe\u8868\u662f\u5728\u5c3e\u90e8\u63d2\u5165\u65b0\u8282\u70b9\u3002\u56e0\u4e3a\u63d2\u5165\u8282\u70b9\u540e\u9700\u8981\u5224\u65ad\u94fe\u8868\u7684\u957f\u5ea6\u662f\u5426\u8fbe\u5230\u8f6c\u5316\u4e3a\u7ea2\u9ed1\u6811\u7684\u9608\u503c 8\uff0c\u8ba1\u7b97\u94fe\u8868\u957f\u5ea6\u9700\u8981\u904d\u5386\u94fe\u8868\uff0c\u5728\u5c3e\u90e8\u63d2\u5165\u5c31\u501f\u52a9\u4e86\u8fd9\u4e2a\u904d\u5386\u64cd\u4f5c\u3002\uff08\u5982\u679c\u4e0d\u9700\u8981\u8ba1\u7b97\u957f\u5ea6\uff0c\u5728\u5934\u90e8\u63d2\u5165\u662f\u66f4\u7b80\u5355\u7684\uff09<\/li>\n<li>\u5143\u7d20\u8ba1\u6570\u4e0a\u91c7\u7528 <code>long baseCounter<\/code> + <code>CounterCell[]<\/code> \u8fdb\u884c\u4f18\u5316\u3002\u5e76\u53d1\u4e0d\u6fc0\u70c8\u65f6\u76f4\u63a5\u66f4\u65b0 baseCounter\uff0c\u6fc0\u70c8\u65f6\u5728 CounterCell \u4e0a\u66f4\u65b0\uff0c\u5206\u6563\u7ade\u4e89\u3002<\/li>\n<li>key\/value \u4e0d\u80fd\u4e3a null\u3002\u6570\u7ec4\u5728\u9700\u8981\u65f6\u521d\u59cb\u5316\uff0c\u9ed8\u8ba4\u957f\u5ea6 16 \u3002<\/li>\n<li>\u901a\u8fc7 volatile \u548c Unsafe \u6765\u4fdd\u8bc1\u6570\u7ec4\u5143\u7d20\u7684\u53ef\u89c1\u6027\u3002\u901a\u8fc7 CAS \u4fdd\u8bc1\u5b89\u5168\u5730\u66f4\u65b0\u6570\u7ec4\u5143\u7d20\u3002<\/li>\n<li>\u7ea2\u9ed1\u6811\u8282\u70b9\u5360\u7528\u5185\u5b58\u662f\u94fe\u8868\u8282\u70b9\u7684\u4e24\u500d\uff0c\u5c3d\u91cf\u4f7f\u7528\u94fe\u8868\u4ee5\u8282\u7701\u5185\u5b58\u3002\u94fe\u8868\u7684\u7ef4\u62a4\u4e5f\u66f4\u7b80\u5355\u3002<\/li>\n<\/ol>\n<hr \/>\n<p>\u6b22\u8fce\u5173\u6ce8\u6211\u7684\u5fae\u4fe1\u516c\u4f17\u53f7: <strong>coderbee\u7b14\u8bb0<\/strong> \u3002<br \/>\n<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>1. \u4e3a\u4ec0\u4e48 key \u548c value \u4e0d\u5141\u8bb8\u4e3a null HashMap \u4e2d\u5141\u8bb8 &hellip; <a href=\"https:\/\/coderbee.net\/index.php\/concurrent\/20210607\/2163\">\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":[357,231],"_links":{"self":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/2163"}],"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=2163"}],"version-history":[{"count":3,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/2163\/revisions"}],"predecessor-version":[{"id":2166,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/2163\/revisions\/2166"}],"wp:attachment":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/media?parent=2163"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/categories?post=2163"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/tags?post=2163"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}