{"id":56188,"date":"2025-08-14T14:15:52","date_gmt":"2025-08-14T06:15:52","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/56188.html"},"modified":"2025-08-14T14:15:52","modified_gmt":"2025-08-14T06:15:52","slug":"juc%e5%ad%a6%e4%b9%a0%e7%ac%94%e8%ae%b0-concurrenthashmap","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/56188.html","title":{"rendered":"JUC\u5b66\u4e60\u7b14\u8bb0-----ConcurrentHashMap"},"content":{"rendered":"<h2>ConcurrentHashMap<\/h2>\n<h3>\u5982\u4f55\u6b63\u786e\u4f7f\u7528ConcurrentHashMap&#xff1f;<\/h3>\n<h4>\u9519\u8bef\u793a\u4f8b<\/h4>\n<p>public static void main(String[] args) {<br \/>\n    demo(<br \/>\n        \/\/ \u521b\u5efa map \u96c6\u5408&#xff0c;\u4f7f\u7528 ConcurrentHashMap<br \/>\n        () -&gt; new ConcurrentHashMap&lt;String, Integer&gt;(),<\/p>\n<p>        (map, words) -&gt; {<br \/>\n            for (String word : words) {<br \/>\n                Integer counter &#061; map.get(word);<br \/>\n                int newValue &#061; counter &#061;&#061; null ? 1 : counter &#043; 1;<br \/>\n                map.put(word, newValue);<br \/>\n            }<br \/>\n        }<br \/>\n    );<br \/>\n}<\/p>\n<p>ConcurrentHashMap\u7684\u7ebf\u7a0b\u5b89\u5168\u662f\u6307\u5b83\u6bcf\u4e00\u4e2a\u65b9\u6cd5\u662f\u539f\u5b50\u7684&#xff0c;get\/put\u662f\u539f\u5b50\u7684&#xff0c;\u4f46\u662fget&#043;put\u4e0d\u662f\u539f\u5b50\u7684\u3002<\/p>\n<h4>\u5982\u4f55\u89e3\u51b3<\/h4>\n<p>public static void main(String[] args) {<br \/>\n    demo(<br \/>\n        \/\/ \u521b\u5efa ConcurrentHashMap \u7684\u5de5\u5382\u65b9\u6cd5<br \/>\n        () -&gt; new ConcurrentHashMap&lt;String, Integer&gt;(),<\/p>\n<p>        \/\/ \u5904\u7406 map \u548c words \u7684\u903b\u8f91<br \/>\n        (map, words) -&gt; {<br \/>\n            for (String word : words) {<br \/>\n                \/\/ \u540c\u6b65\u5757&#xff0c;\u9501\u5b9a map \u5bf9\u8c61<br \/>\n                synchronized (map) {<br \/>\n                    Integer counter &#061; map.get(word);<br \/>\n                    int newValue &#061; counter &#061;&#061; null ? 1 : counter &#043; 1;<br \/>\n                    map.put(word, newValue);<br \/>\n                }<br \/>\n            }<br \/>\n        }<br \/>\n    );<br \/>\n}<\/p>\n<p>\u53ef\u4ee5\u76f4\u63a5\u52a0\u9501\u6765\u4fdd\u8bc1\u591a\u4e2a\u64cd\u4f5c\u7684\u539f\u5b50\u6027&#xff0c;\u4f46\u662f\u5e76\u4e0d\u597d&#xff0c;ConcurrentHashMap\u5e95\u5c42\u662f\u901a\u8fc7\u66f4\u52a0\u7ec6\u7c92\u5ea6\u7684\u9501\u6765\u5b9e\u73b0\u7684&#xff0c;\u76f4\u63a5\u7ed9\u6574\u4e2amap\u52a0\u9501\u5148\u5f53\u4e8e\u76f4\u63a5\u9000\u5316\u4e86&#xff0c;\u5e76\u53d1\u5ea6\u548c\u6027\u80fd\u90fd\u4e0d\u9ad8<\/p>\n<p>\u89e3\u6cd5&#xff1a;<\/p>\n<p>public class Main {<br \/>\n    public static void main(String[] args) {<br \/>\n        \/\/ \u8c03\u7528 demo \u65b9\u6cd5&#xff0c;\u4f20\u5165\u4e24\u4e2a Lambda \u8868\u8fbe\u5f0f&#xff1a;<br \/>\n        \/\/ 1. \u7528\u4e8e\u521b\u5efa ConcurrentHashMap \u7684\u5de5\u5382<br \/>\n        \/\/ 2. \u7528\u4e8e\u5904\u7406 map \u548c words \u7684\u903b\u8f91<br \/>\n        demo(<br \/>\n            \/\/ \u521b\u5efa ConcurrentHashMap&lt;String, LongAdder&gt; \u7684\u5de5\u5382\u65b9\u6cd5<br \/>\n            \/\/ \u5ef6\u8fdf\u521b\u5efa\u96c6\u5408&#xff0c;\u901a\u8fc7 Lambda \u8868\u8fbe\u5f0f\u5c01\u88c5\u521b\u5efa\u903b\u8f91<br \/>\n            () -&gt; new ConcurrentHashMap&lt;String, LongAdder&gt;(),<\/p>\n<p>            \/\/ \u5904\u7406 map \u548c words \u7684\u903b\u8f91&#xff0c;BiConsumer \u63a5\u6536\u4e24\u4e2a\u53c2\u6570&#xff1a;<br \/>\n            \/\/ ConcurrentHashMap&lt;String, LongAdder&gt; map \u548c \u5355\u8bcd\u96c6\u5408 words&#xff08;\u63a8\u6d4b\u4e3a String \u6570\u7ec4\/\u96c6\u5408&#xff09;<br \/>\n            (map, words) -&gt; {<br \/>\n                \/\/ \u904d\u5386\u5355\u8bcd\u96c6\u5408\u4e2d\u7684\u6bcf\u4e2a\u5355\u8bcd<br \/>\n                for (String word : words) {<br \/>\n                    \/\/ computeIfAbsent \u662f ConcurrentHashMap \u7684\u539f\u5b50\u64cd\u4f5c&#xff1a;<br \/>\n                    \/\/ 1. \u68c0\u67e5 map \u4e2d\u662f\u5426\u5b58\u5728\u6307\u5b9a key&#xff08;word&#xff09;<br \/>\n                    \/\/ 2. \u5982\u679c\u4e0d\u5b58\u5728&#xff0c;\u5219\u6267\u884c (key) -&gt; new LongAdder() \u521b\u5efa\u65b0\u7684 LongAdder \u4f5c\u4e3a value<br \/>\n                    \/\/ 3. \u5982\u679c\u5b58\u5728&#xff0c;\u5219\u76f4\u63a5\u83b7\u53d6\u5df2\u6709\u7684 LongAdder<br \/>\n                    \/\/ \u8be5\u64cd\u4f5c\u662f\u7ebf\u7a0b\u5b89\u5168\u7684&#xff0c;\u9002\u5408\u591a\u7ebf\u7a0b\u73af\u5883<br \/>\n                    LongAdder value &#061; map.computeIfAbsent(word, (key) -&gt; new LongAdder());<\/p>\n<p>                    \/\/ \u6267\u884c\u7d2f\u52a0\u64cd\u4f5c&#xff0c;LongAdder \u662f\u7ebf\u7a0b\u5b89\u5168\u7684\u7d2f\u52a0\u5668&#xff0c;\u6bd4 AtomicLong \u66f4\u9002\u5408\u9ad8\u5e76\u53d1\u573a\u666f<br \/>\n                    value.increment();<\/p>\n<p>                    \/\/ \u4ee5\u4e0b\u662f\u88ab\u6ce8\u91ca\u7684\u65e7\u5b9e\u73b0\u903b\u8f91&#xff0c;\u4fdd\u7559\u7528\u4e8e\u5bf9\u6bd4\u8bf4\u660e&#xff1a;<br \/>\n                    \/*<br \/>\n                    \/\/ \u65e7\u903b\u8f91&#xff1a;\u5148 get \u518d put&#xff0c;\u975e\u539f\u5b50\u64cd\u4f5c&#xff08;\u591a\u7ebf\u7a0b\u4e0b\u53ef\u80fd\u6709\u95ee\u9898&#xff09;<br \/>\n                    Integer counter &#061; map.get(word);<br \/>\n                    int newValue &#061; counter &#061;&#061; null ? 1 : counter &#043; 1;<br \/>\n                    map.put(word, newValue);<br \/>\n                    *\/<br \/>\n                }<br \/>\n            }<br \/>\n        );<br \/>\n    }<\/p>\n<p>computeIfAbsent\u4fdd\u8bc1\u4e09\u4e2a\u64cd\u4f5c\u7684\u539f\u5b50\u6027&#xff0c;LongAdder\u4fdd\u8bc1\u7d2f\u52a0\u64cd\u4f5c\u662f\u539f\u5b50\u7684<\/p>\n<\/p>\n<h3>HashMap\u5e76\u53d1\u6b7b\u94fe&#xff08;jdk7&#xff09;<\/h3>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"526\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/08\/20250814061547-689d7f1377d6f.png\" width=\"652\" \/><\/p>\n<p>\u7a76\u5176\u539f\u56e0&#xff0c;\u662f\u56e0\u4e3a\u5728\u591a\u7ebf\u7a0b\u73af\u5883\u4e0b\u4f7f\u7528\u4e86\u975e\u7ebf\u7a0b\u5b89\u5168\u7684 map \u96c6\u5408<\/p>\n<p>JDK 8 \u867d\u7136\u5c06\u6269\u5bb9\u7b97\u6cd5\u505a\u4e86\u8c03\u6574&#xff0c;\u4e0d\u518d\u5c06\u5143\u7d20\u52a0\u5165\u94fe\u8868\u5934&#xff08;\u800c\u662f\u4fdd\u6301\u4e0e\u6269\u5bb9\u524d\u4e00\u6837\u7684\u987a\u5e8f&#xff09;&#xff0c;\u4f46\u4ecd\u4e0d\u610f\u5473\u7740\u80fd \u591f\u5728\u591a\u7ebf\u7a0b\u73af\u5883\u4e0b\u80fd\u591f\u5b89\u5168\u6269\u5bb9&#xff0c;\u8fd8\u4f1a\u51fa\u73b0\u5176\u5b83\u95ee\u9898&#xff08;\u5982\u6269\u5bb9\u4e22\u6570\u636e&#xff09;<\/p>\n<h3>jdk8<\/h3>\n<h4 style=\"background-color:transparent\">\u6784\u9020\u5668\u5206\u6790<\/h4>\n<p>\u53ef\u4ee5\u770b\u5230\u5b9e\u73b0\u4e86\u61d2\u60f0\u521d\u59cb\u5316&#xff0c;\u5728\u6784\u9020\u65b9\u6cd5\u4e2d\u4ec5\u4ec5\u8ba1\u7b97\u4e86 table \u7684\u5927\u5c0f&#xff0c;\u4ee5\u540e\u5728\u7b2c\u4e00\u6b21\u4f7f\u7528\u65f6\u624d\u4f1a\u771f\u6b63\u521b\u5efa<\/p>\n<p>\/\/ ConcurrentHashMap \u7684\u6784\u9020\u51fd\u6570&#xff0c;\u5141\u8bb8\u6307\u5b9a\u521d\u59cb\u5bb9\u91cf\u3001\u8d1f\u8f7d\u56e0\u5b50\u3001\u5e76\u53d1\u7ea7\u522b<br \/>\npublic ConcurrentHashMap(int initialCapacity, float loadFactor, int concurrencyLevel) {<br \/>\n    \/\/ 1. \u53c2\u6570\u5408\u6cd5\u6027\u6821\u9a8c<br \/>\n    \/\/ \u8d1f\u8f7d\u56e0\u5b50\u5fc5\u987b &gt; 0&#xff0c;\u521d\u59cb\u5bb9\u91cf\u4e0d\u80fd &lt; 0&#xff0c;\u5e76\u53d1\u7ea7\u522b\u4e0d\u80fd &lt;&#061; 0<br \/>\n    if (!(loadFactor &gt; 0.0f) || initialCapacity &lt; 0 || concurrencyLevel &lt;&#061; 0) {<br \/>\n        throw new IllegalArgumentException();<br \/>\n    }<br \/>\n    \/\/ 2. \u8c03\u6574\u521d\u59cb\u5bb9\u91cf&#xff08;\u4e0e\u5e76\u53d1\u7ea7\u522b\u5173\u8054&#xff09;<br \/>\n    \/\/ \u5982\u679c\u521d\u59cb\u5bb9\u91cf\u5c0f\u4e8e\u5e76\u53d1\u7ea7\u522b&#xff0c;\u5c06\u521d\u59cb\u5bb9\u91cf\u8c03\u6574\u4e3a\u5e76\u53d1\u7ea7\u522b<br \/>\n    \/\/ \u539f\u56e0&#xff1a;ConcurrentHashMap \u5185\u90e8\u7528\u5206\u6bb5&#xff08;\u6876&#xff09;\u5b58\u50a8\u6570\u636e&#xff0c;\u81f3\u5c11\u8981\u4fdd\u8bc1\u6876\u6570\u91cf &gt;&#061; \u5e76\u53d1\u7ea7\u522b&#xff0c;<br \/>\n    \/\/ \u907f\u514d\u54c8\u5e0c\u51b2\u7a81\u8fc7\u4e8e\u96c6\u4e2d&#xff0c;\u5f71\u54cd\u5e76\u53d1\u6027\u80fd<br \/>\n    if (initialCapacity &lt; concurrencyLevel) {<br \/>\n        initialCapacity &#061; concurrencyLevel;<br \/>\n    }<br \/>\n    \/\/ 3. \u8ba1\u7b97\u8868\u7684\u76ee\u6807\u5927\u5c0f&#xff08;\u57fa\u4e8e\u521d\u59cb\u5bb9\u91cf\u548c\u8d1f\u8f7d\u56e0\u5b50&#xff09;<br \/>\n    \/\/ \u516c\u5f0f&#xff1a;size &#061; 1.0 &#043; (\u521d\u59cb\u5bb9\u91cf \/ \u8d1f\u8f7d\u56e0\u5b50)&#xff0c;\u5411\u4e0a\u53d6\u6574&#xff08;\u8f6c\u4e3a long \u907f\u514d\u6ea2\u51fa&#xff09;<br \/>\n    \/\/ \u4f5c\u7528&#xff1a;\u6839\u636e\u521d\u59cb\u5bb9\u91cf\u548c\u8d1f\u8f7d\u56e0\u5b50&#xff0c;\u4f30\u7b97\u9700\u8981\u7684\u54c8\u5e0c\u8868\u5bb9\u91cf&#xff0c;\u4e3a\u540e\u7eed\u8c03\u6574\u4e3a 2 \u7684\u5e42\u505a\u51c6\u5907<br \/>\n    long size &#061; (long)(1.0 &#043; (long)initialCapacity \/ loadFactor);<br \/>\n    \/\/ 4. \u9650\u5236\u5bb9\u91cf\u6700\u5927\u503c<br \/>\n    \/\/ MAXIMUM_CAPACITY \u662f ConcurrentHashMap \u5141\u8bb8\u7684\u6700\u5927\u5bb9\u91cf&#xff08;\u901a\u5e38\u662f 1&lt;&lt;30 \u7b49&#xff0c;\u4e0d\u540c\u7248\u672c\u53ef\u80fd\u6709\u5dee\u5f02&#xff09;<br \/>\n    \/\/ \u5982\u679c\u8ba1\u7b97\u51fa\u7684 size \u8d85\u8fc7\u6700\u5927\u503c&#xff0c;\u5c31\u7528 MAXIMUM_CAPACITY&#xff0c;\u5426\u5219\u8c03\u6574\u4e3a\u6700\u63a5\u8fd1\u7684 2 \u7684\u5e42<br \/>\n    int cap &#061; (size &gt;&#061; (long)MAXIMUM_CAPACITY) ?<br \/>\n        MAXIMUM_CAPACITY :<br \/>\n        \/\/ tableSizeFor \u65b9\u6cd5\u4f1a\u8fd4\u56de\u5927\u4e8e\u7b49\u4e8e\u7ed9\u5b9a\u6570\u503c\u7684\u6700\u5c0f\u7684 2 \u7684\u5e42&#xff08;\u5982\u8f93\u5165 5 \u8fd4\u56de 8&#xff0c;\u8f93\u5165 8 \u8fd4\u56de 8 &#xff09;<br \/>\n        tableSizeFor((int)size);<br \/>\n    \/\/ 5. \u521d\u59cb\u5316 sizeCtl \u5b57\u6bb5<br \/>\n    \/\/ sizeCtl \u5728 ConcurrentHashMap \u4e2d\u662f\u4e00\u4e2a\u591a\u529f\u80fd\u7684\u63a7\u5236\u5b57\u6bb5&#xff1a;<br \/>\n    \/\/ &#8211; \u521d\u59cb\u5316\u65f6&#xff0c;\u5b58\u50a8\u54c8\u5e0c\u8868\u7684\u5bb9\u91cf&#xff08;\u540e\u7eed\u4f1a\u8c03\u6574\u4e3a\u5176\u4ed6\u72b6\u6001&#xff0c;\u5982 -1 \u8868\u793a\u6b63\u5728\u521d\u59cb\u5316 &#xff09;<br \/>\n    \/\/ &#8211; \u6269\u5bb9\u65f6&#xff0c;\u7528\u4e8e\u534f\u8c03\u591a\u7ebf\u7a0b\u7684\u6269\u5bb9\u64cd\u4f5c\u7b49<br \/>\n    this.sizeCtl &#061; cap;<br \/>\n}<\/p>\n<li>\u6821\u9a8c\u53c2\u6570\u5408\u6cd5\u6027&#xff0c;\u786e\u4fdd\u8d1f\u8f7d\u56e0\u5b50\u5927\u4e8e 0\u3001\u521d\u59cb\u5bb9\u91cf\u975e\u8d1f\u3001\u5e76\u53d1\u7ea7\u522b\u4e3a\u6b63\u6570\u3002<\/li>\n<li>\u82e5\u521d\u59cb\u5bb9\u91cf\u5c0f\u4e8e\u5e76\u53d1\u7ea7\u522b&#xff0c;\u5c06\u521d\u59cb\u5bb9\u91cf\u8c03\u6574\u4e3a\u5e76\u53d1\u7ea7\u522b&#xff0c;\u4fdd\u8bc1\u6876\u6570\u91cf\u9002\u914d\u5e76\u53d1\u9700\u6c42\u3002<\/li>\n<li>\u57fa\u4e8e\u521d\u59cb\u5bb9\u91cf &#061; 1.0 &#043; \u521d\u59cb\u5bb9\u91cf \/ \u8d1f\u8f7d\u56e0\u5b50\u7684\u516c\u5f0f\u8ba1\u7b97\u76ee\u6807\u5927\u5c0f&#xff0c;\u4e3a\u540e\u7eed\u8c03\u6574\u505a\u51c6\u5907\u3002<\/li>\n<li>\u9650\u5236\u5bb9\u91cf\u4e0d\u8d85\u8fc7\u6700\u5927\u503c&#xff0c;\u8d85\u51fa\u5219\u7528\u6700\u5927\u503c&#xff0c;\u5426\u5219\u8c03\u6574\u4e3a\u6700\u63a5\u8fd1\u7684 2 \u7684\u5e42\u3002<\/li>\n<li>\u521d\u59cb\u5316 sizeCtl \u5b57\u6bb5&#xff0c;\u5b58\u50a8\u54c8\u5e0c\u8868\u5bb9\u91cf&#xff0c;\u7528\u4e8e\u540e\u7eed\u521d\u59cb\u5316\u548c\u6269\u5bb9\u63a7\u5236<\/li>\n<h4 style=\"background-color:transparent\">get\u6d41\u7a0b<\/h4>\n<p>\u5168\u7a0b\u4e0d\u52a0\u9501<\/p>\n<p>public V get(Object key) {<br \/>\n    Node&lt;K,V&gt;[] tab; Node&lt;K,V&gt; e, p; int n, eh; K ek;<br \/>\n    \/\/ 1. \u8ba1\u7b97 key \u7684\u54c8\u5e0c\u503c&#xff1a;<br \/>\n    \/\/    &#8211; \u5148\u8c03\u7528 key.hashCode() \u83b7\u53d6\u539f\u59cb\u54c8\u5e0c<br \/>\n    \/\/    &#8211; spread \u65b9\u6cd5\u901a\u8fc7\u5f02\u6216\u548c\u4f4d\u8fd0\u7b97&#xff0c;\u786e\u4fdd\u7ed3\u679c\u4e3a\u6b63\u6570&#xff08;\u907f\u514d\u54c8\u5e0c\u51b2\u7a81&#xff09;<br \/>\n    int h &#061; spread(key.hashCode());<\/p>\n<p>    \/\/ 2. \u68c0\u67e5\u54c8\u5e0c\u8868\u72b6\u6001&#xff1a;<br \/>\n    \/\/    &#8211; tab &#061; table&#xff1a;\u83b7\u53d6\u5f53\u524d\u54c8\u5e0c\u8868\u6570\u7ec4<br \/>\n    \/\/    &#8211; n &#061; tab.length &gt; 0&#xff1a;\u54c8\u5e0c\u8868\u5df2\u521d\u59cb\u5316\u4e14\u957f\u5ea6\u6709\u6548<br \/>\n    \/\/    &#8211; e &#061; tabAt(&#8230;)&#xff1a;\u901a\u8fc7 tabAt \u65b9\u6cd5&#xff08;\u7ebf\u7a0b\u5b89\u5168&#xff09;\u83b7\u53d6\u6876\u4f4d\u7f6e\u7684\u5934\u8282\u70b9<br \/>\n    if ((tab &#061; table) !&#061; null &amp;&amp; (n &#061; tab.length) &gt; 0 &amp;&amp;<br \/>\n        (e &#061; tabAt(tab, (n &#8211; 1) &amp; h)) !&#061; null) {<\/p>\n<p>        \/\/ 3. \u68c0\u67e5\u5934\u8282\u70b9\u662f\u5426\u5339\u914d\u76ee\u6807 key&#xff1a;<br \/>\n        \/\/    &#8211; eh &#061; e.hash&#xff1a;\u83b7\u53d6\u5934\u8282\u70b9\u7684\u54c8\u5e0c\u503c<br \/>\n        \/\/    &#8211; \u5148\u5bf9\u6bd4\u54c8\u5e0c\u503c&#xff08;\u5feb\u901f\u7b5b\u9009&#xff09;&#xff0c;\u518d\u5bf9\u6bd4 key \u5bf9\u8c61&#xff08;&#061;&#061; \u6216 equals&#xff09;<br \/>\n        if ((eh &#061; e.hash) &#061;&#061; h) {<br \/>\n            if (((ek &#061; e.key) &#061;&#061; key) || (ek !&#061; null &amp;&amp; key.equals(ek)))<br \/>\n                return e.val; \/\/ \u627e\u5230\u5339\u914d&#xff0c;\u8fd4\u56de\u503c<br \/>\n        }<\/p>\n<p>        \/\/ 4. \u5904\u7406\u7279\u6b8a\u8282\u70b9&#xff08;\u54c8\u5e0c\u503c\u4e3a\u8d1f\u6570\u7684\u60c5\u51b5&#xff09;&#xff1a;<br \/>\n        \/\/    &#8211; eh &lt; 0 \u8868\u793a\u8282\u70b9\u662f ForwardingNode&#xff08;\u6269\u5bb9\u4e2d&#xff09;\u6216 TreeBin&#xff08;\u7ea2\u9ed1\u6811&#xff09;<br \/>\n        \/\/    &#8211; \u8c03\u7528 find \u65b9\u6cd5\u9012\u5f52\u67e5\u627e&#xff08;\u6269\u5bb9\u65f6\u904d\u5386\u65b0\u3001\u65e7\u8868&#xff1b;\u7ea2\u9ed1\u6811\u65f6\u6309\u6811\u7ed3\u6784\u67e5\u627e&#xff09;<br \/>\n        else if (eh &lt; 0)<br \/>\n            return (p &#061; e.find(h, key)) !&#061; null ? p.val : null;<\/p>\n<p>        \/\/ 5. \u904d\u5386\u94fe\u8868\u67e5\u627e\u5339\u914d\u7684 key&#xff1a;<br \/>\n        \/\/    &#8211; \u54c8\u5e0c\u503c\u5339\u914d\u65f6&#xff0c;\u518d\u5bf9\u6bd4 key \u5bf9\u8c61&#xff08;&#061;&#061; \u6216 equals&#xff09;<br \/>\n        while ((e &#061; e.next) !&#061; null) {<br \/>\n            if (e.hash &#061;&#061; h &amp;&amp;<br \/>\n                ((ek &#061; e.key) &#061;&#061; key || (ek !&#061; null &amp;&amp; key.equals(ek))))<br \/>\n                return e.val; \/\/ \u627e\u5230\u5339\u914d&#xff0c;\u8fd4\u56de\u503c<br \/>\n        }<br \/>\n    }<br \/>\n    \/\/ 6. \u672a\u627e\u5230 key&#xff0c;\u8fd4\u56de null<br \/>\n    return null;<br \/>\n}<\/p>\n<li>\n<p>\u8ba1\u7b97\u54c8\u5e0c&#xff1a;<br \/>\n\u901a\u8fc7\u00a0spread\u00a0\u65b9\u6cd5\u8ba1\u7b97\u00a0key\u00a0\u7684\u54c8\u5e0c\u503c&#xff0c;\u786e\u4fdd\u7ed3\u679c\u4e3a\u6b63\u6570&#xff0c;\u907f\u514d\u54c8\u5e0c\u51b2\u7a81\u3002<\/p>\n<\/li>\n<li>\n<p>\u68c0\u67e5\u54c8\u5e0c\u8868\u72b6\u6001&#xff1a;<br \/>\n\u9a8c\u8bc1\u54c8\u5e0c\u8868\u662f\u5426\u521d\u59cb\u5316\u3001\u957f\u5ea6\u662f\u5426\u6709\u6548&#xff0c;\u5e76\u7528\u00a0tabAt\u00a0\u7ebf\u7a0b\u5b89\u5168\u5730\u83b7\u53d6\u6876\u4f4d\u7f6e\u7684\u5934\u8282\u70b9\u3002<\/p>\n<\/li>\n<li>\n<p>\u5339\u914d\u5934\u8282\u70b9&#xff1a;<br \/>\n\u5148\u5bf9\u6bd4\u5934\u8282\u70b9\u7684\u54c8\u5e0c\u503c&#xff0c;\u518d\u7528\u00a0&#061;&#061;\u00a0\u6216\u00a0equals\u00a0\u5bf9\u6bd4\u00a0key&#xff0c;\u5339\u914d\u5219\u76f4\u63a5\u8fd4\u56de\u503c\u3002<\/p>\n<\/li>\n<li>\n<p>\u5904\u7406\u7279\u6b8a\u8282\u70b9&#xff1a;<br \/>\n\u82e5\u5934\u8282\u70b9\u54c8\u5e0c\u4e3a\u8d1f\u6570&#xff08;\u6269\u5bb9\u4e2d\u6216\u7ea2\u9ed1\u6811&#xff09;&#xff0c;\u8c03\u7528\u00a0find\u00a0\u65b9\u6cd5\u9012\u5f52\u67e5\u627e\u76ee\u6807\u00a0key\u3002<\/p>\n<\/li>\n<li>\n<p>\u904d\u5386\u94fe\u8868\u67e5\u627e&#xff1a;<br \/>\n\u54c8\u5e0c\u503c\u5339\u914d\u65f6&#xff0c;\u9010\u4e00\u904d\u5386\u94fe\u8868\u8282\u70b9&#xff0c;\u7528\u00a0&#061;&#061;\u00a0\u6216\u00a0equals\u00a0\u5bf9\u6bd4\u00a0key&#xff0c;\u627e\u5230\u5219\u8fd4\u56de\u503c\u3002<\/p>\n<\/li>\n<li>\n<p>\u672a\u627e\u5230\u8fd4\u56de\u00a0null&#xff1a;<br \/>\n\u82e5\u904d\u5386\u5b8c\u6240\u6709\u8282\u70b9\u4ecd\u672a\u5339\u914d&#xff0c;\u8fd4\u56de\u00a0null\u3002<\/p>\n<\/li>\n<h4 style=\"background-color:transparent\">put\u6d41\u7a0b<\/h4>\n<p>\u4ee5\u4e0b\u6570\u7ec4\u7b80\u79f0&#xff08;table&#xff09;,\u94fe\u8868\u7b80\u79f0&#xff08;bin&#xff09;<\/p>\n<p>public V put(K key, V value) {<br \/>\n    return putVal(key, value, false);<br \/>\n}<\/p>\n<ul>\n<li>\u4f5c\u7528&#xff1a;\u5bf9\u5916\u63d0\u4f9b\u00a0put\u00a0\u64cd\u4f5c\u7684\u5165\u53e3\u3002<\/li>\n<li>\u53c2\u6570&#xff1a;\n<ul>\n<li>key&#xff1a;\u952e&#xff08;\u4e0d\u80fd\u4e3a\u00a0null&#xff09;\u3002<\/li>\n<li>value&#xff1a;\u503c&#xff08;\u4e0d\u80fd\u4e3a\u00a0null&#xff09;\u3002<\/li>\n<li>onlyIfAbsent&#xff1a;\u662f\u5426\u4ec5\u5728\u952e\u4e0d\u5b58\u5728\u65f6\u63d2\u5165&#xff08;false\u00a0\u8868\u793a\u8986\u76d6\u65e7\u503c&#xff09;\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\u8fd4\u56de\u503c&#xff1a;\u65e7\u503c&#xff08;\u5982\u679c\u6ca1\u6709\u65e7\u503c\u5219\u8fd4\u56de\u00a0null&#xff09;\u3002<\/li>\n<\/ul>\n<p>final V putVal(K key, V value, boolean onlyIfAbsent) {<br \/>\n    \/\/ 1. \u6821\u9a8c key \u548c value \u4e0d\u4e3a null<br \/>\n    if (key &#061;&#061; null || value &#061;&#061; null) throw new NullPointerException();<\/p>\n<p>    \/\/ 2. \u8ba1\u7b97 key \u7684\u54c8\u5e0c\u503c<br \/>\n    int hash &#061; spread(key.hashCode());<br \/>\n    int binCount &#061; 0; \/\/ \u8bb0\u5f55\u94fe\u8868\u957f\u5ea6&#xff08;\u7528\u4e8e\u5224\u65ad\u662f\u5426\u9700\u8981\u6811\u5316&#xff09;<\/p>\n<p>    \/\/ 3. \u65e0\u9650\u5faa\u73af&#xff0c;\u786e\u4fdd\u64cd\u4f5c\u5b8c\u6210<br \/>\n    for (Node&lt;K, V&gt;[] tab &#061; table; ; ) {<br \/>\n        Node&lt;K, V&gt; f; \/\/ \u5f53\u524d\u6876\u7684\u5934\u8282\u70b9<br \/>\n        int n, i, fh; \/\/ n: \u54c8\u5e0c\u8868\u957f\u5ea6; i: \u6876\u7684\u4e0b\u6807; fh: \u5934\u8282\u70b9\u7684\u54c8\u5e0c\u503c<\/p>\n<p>        \/\/ 3.1 \u521d\u59cb\u5316\u54c8\u5e0c\u8868&#xff08;\u5ef6\u8fdf\u521d\u59cb\u5316&#xff09;<br \/>\n        if (tab &#061;&#061; null || (n &#061; tab.length) &#061;&#061; 0) {<br \/>\n            tab &#061; initTable(); \/\/ \u4f7f\u7528 CAS \u521d\u59cb\u5316&#xff0c;\u65e0\u9700 synchronized<br \/>\n        }<\/p>\n<p>        \/\/ 3.2 \u5b9a\u4f4d\u6876\u7684\u4f4d\u7f6e&#xff0c;\u5e76\u68c0\u67e5\u6876\u662f\u5426\u4e3a\u7a7a<br \/>\n        else if ((f &#061; tabAt(tab, i &#061; (n &#8211; 1) &amp; hash)) &#061;&#061; null) {<br \/>\n            \/\/ \u6876\u4e3a\u7a7a&#xff0c;\u4f7f\u7528 CAS \u63d2\u5165\u65b0\u8282\u70b9<br \/>\n            if (casTabAt(tab, i, null, new Node&lt;&gt;(hash, key, value, null))) {<br \/>\n                break; \/\/ \u63d2\u5165\u6210\u529f&#xff0c;\u9000\u51fa\u5faa\u73af<br \/>\n            }<br \/>\n        }<\/p>\n<p>        \/\/ 3.3 \u5904\u7406\u6269\u5bb9\u4e2d\u7684 ForwardingNode&#xff08;\u7b80\u5316\u7248&#xff0c;\u5b9e\u9645 JDK \u4e2d\u9700\u8981\u5904\u7406\u6269\u5bb9\u903b\u8f91&#xff09;<br \/>\n        else if ((fh &#061; f.hash) &#061;&#061; -1) {<br \/>\n            \/\/ \u5e2e\u52a9\u6269\u5bb9&#xff08;\u7b80\u5316\u7248&#xff0c;\u5b9e\u9645 JDK \u4e2d\u9700\u8981\u8fc1\u79fb\u6570\u636e&#xff09;<br \/>\n            tab &#061; table; \/\/ \u91cd\u65b0\u83b7\u53d6\u6700\u65b0\u7684\u54c8\u5e0c\u8868<br \/>\n        }<\/p>\n<p>        \/\/ 3.4 \u5904\u7406\u975e\u7a7a\u6876&#xff08;\u94fe\u8868\u6216\u7ea2\u9ed1\u6811&#xff09;<br \/>\n        else {<br \/>\n            V oldVal &#061; null;<br \/>\n            \/\/ \u52a0\u9501&#xff08;\u7b80\u5316\u7248&#xff0c;\u5b9e\u9645 JDK \u4e2d\u4f7f\u7528 synchronized \u6216 CAS \u9501&#xff09;<br \/>\n            synchronized (f) {<br \/>\n                if (tabAt(tab, i) &#061;&#061; f) { \/\/ \u518d\u6b21\u68c0\u67e5\u5934\u8282\u70b9\u662f\u5426\u53d8\u5316<br \/>\n                    if (fh &gt;&#061; 0) { \/\/ \u5934\u8282\u70b9\u662f\u666e\u901a\u8282\u70b9&#xff08;\u94fe\u8868&#xff09;<br \/>\n                        binCount &#061; 1;<br \/>\n                        \/\/ \u904d\u5386\u94fe\u8868<br \/>\n                        for (Node&lt;K, V&gt; e &#061; f; ; &#043;&#043;binCount) {<br \/>\n                            K ek;<br \/>\n                            \/\/ 3.4.1 \u627e\u5230\u76f8\u540c key \u7684\u8282\u70b9<br \/>\n                            if (e.hash &#061;&#061; hash &amp;&amp;<br \/>\n                                    ((ek &#061; e.key) &#061;&#061; key || (ek !&#061; null &amp;&amp; key.equals(ek)))) {<br \/>\n                                oldVal &#061; e.val;<br \/>\n                                if (!onlyIfAbsent) {<br \/>\n                                    e.val &#061; value; \/\/ \u66f4\u65b0\u503c<br \/>\n                                }<br \/>\n                                break;<br \/>\n                            }<br \/>\n                            Node&lt;K, V&gt; pred &#061; e;<br \/>\n                            \/\/ 3.4.2 \u904d\u5386\u5230\u94fe\u8868\u672b\u5c3e&#xff0c;\u63d2\u5165\u65b0\u8282\u70b9<br \/>\n                            if ((e &#061; e.next) &#061;&#061; null) {<br \/>\n                                pred.next &#061; new Node&lt;&gt;(hash, key, value, null);<br \/>\n                                break;<br \/>\n                            }<br \/>\n                        }<br \/>\n                    }<br \/>\n                    \/\/ 3.5 \u5904\u7406\u7ea2\u9ed1\u6811&#xff08;\u7b80\u5316\u7248&#xff0c;\u5b9e\u9645 JDK \u4e2d\u9700\u8981\u6811\u5316\u903b\u8f91&#xff09;<br \/>\n                    else {<br \/>\n                        \/\/ &#8230; \u6811\u5316\u903b\u8f91&#xff08;\u7701\u7565&#xff09; &#8230;<br \/>\n                    }<br \/>\n                }<br \/>\n            }<\/p>\n<p>            \/\/ 3.6 \u5224\u65ad\u662f\u5426\u9700\u8981\u6811\u5316&#xff08;\u7b80\u5316\u7248&#xff0c;\u5b9e\u9645 JDK \u4e2d\u9700\u8981\u5224\u65ad\u94fe\u8868\u957f\u5ea6&#xff09;<br \/>\n            if (binCount !&#061; 0) {<br \/>\n                if (binCount &gt;&#061; 8) {<br \/>\n                    \/\/ \u94fe\u8868\u8f6c\u7ea2\u9ed1\u6811&#xff08;\u7b80\u5316\u7248&#xff0c;\u5b9e\u9645 JDK \u4e2d\u9700\u8981\u6811\u5316\u64cd\u4f5c&#xff09;<br \/>\n                }<br \/>\n                if (oldVal !&#061; null) {<br \/>\n                    return oldVal; \/\/ \u8fd4\u56de\u65e7\u503c<br \/>\n                }<br \/>\n                break;<br \/>\n            }<br \/>\n        }<br \/>\n    }<\/p>\n<p>    \/\/ 4. \u66f4\u65b0 size&#xff08;\u7b80\u5316\u7248&#xff0c;\u5b9e\u9645 JDK \u4e2d\u9700\u8981 CAS \u66f4\u65b0&#xff09;<br \/>\n    \/\/ &#8230; \u7701\u7565\u66f4\u65b0 size \u7684\u903b\u8f91 &#8230;<\/p>\n<p>    return null; \/\/ \u8fd4\u56de\u65e7\u503c&#xff08;\u5982\u679c\u6ca1\u6709\u65e7\u503c\u5219\u8fd4\u56de null&#xff09;<br \/>\n}<\/p>\n<li>\n<p>\u6821\u9a8c\u53c2\u6570&#xff1a;<br \/>\n\u68c0\u67e5\u00a0key\u00a0\u548c\u00a0value\u00a0\u662f\u5426\u4e3a\u00a0null&#xff0c;\u82e5\u4e3a\u00a0null\u00a0\u5219\u629b\u51fa\u00a0NullPointerException\u3002<\/p>\n<\/li>\n<li>\n<p>\u8ba1\u7b97\u54c8\u5e0c&#xff1a;<br \/>\n\u901a\u8fc7\u00a0spread\u00a0\u65b9\u6cd5\u8ba1\u7b97\u00a0key\u00a0\u7684\u54c8\u5e0c\u503c&#xff0c;\u786e\u4fdd\u54c8\u5e0c\u5206\u5e03\u5747\u5300\u3002<\/p>\n<\/li>\n<li>\n<p>\u521d\u59cb\u5316\u54c8\u5e0c\u8868&#xff1a;<br \/>\n\u5982\u679c\u54c8\u5e0c\u8868\u672a\u521d\u59cb\u5316&#xff08;tab &#061;&#061; null\u00a0\u6216\u957f\u5ea6\u4e3a 0&#xff09;&#xff0c;\u5219\u901a\u8fc7\u00a0initTable\u00a0\u5ef6\u8fdf\u521d\u59cb\u5316\u3002<\/p>\n<\/li>\n<li>\n<p>\u5b9a\u4f4d\u6876\u5e76\u63d2\u5165\u8282\u70b9&#xff1a;<\/p>\n<ul>\n<li>\u5982\u679c\u6876\u4e3a\u7a7a&#xff0c;\u4f7f\u7528\u00a0CAS\u00a0\u63d2\u5165\u65b0\u8282\u70b9&#xff0c;\u907f\u514d\u00a0synchronized\u3002<\/li>\n<li>\u5982\u679c\u6876\u4e0d\u4e3a\u7a7a&#xff0c;\u904d\u5386\u94fe\u8868\u6216\u7ea2\u9ed1\u6811&#xff0c;\u627e\u5230\u76f8\u540c\u00a0key\u00a0\u5219\u66f4\u65b0\u503c&#xff0c;\u5426\u5219\u63d2\u5165\u65b0\u8282\u70b9\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u5904\u7406\u6269\u5bb9&#xff1a;<br \/>\n\u5982\u679c\u9047\u5230\u6269\u5bb9\u4e2d\u7684\u00a0ForwardingNode&#xff0c;\u534f\u52a9\u5b8c\u6210\u6269\u5bb9&#xff08;\u7b80\u5316\u7248&#xff0c;\u5b9e\u9645\u903b\u8f91\u66f4\u590d\u6742 &#xff09;\u3002<\/p>\n<\/li>\n<li>\n<p>\u94fe\u8868\u6811\u5316&#xff1a;<br \/>\n\u5982\u679c\u94fe\u8868\u957f\u5ea6\u8d85\u8fc7\u9608\u503c&#xff08;\u9ed8\u8ba4 8&#xff09;&#xff0c;\u5c06\u94fe\u8868\u8f6c\u6362\u4e3a\u7ea2\u9ed1\u6811&#xff0c;\u63d0\u5347\u67e5\u8be2\u6548\u7387\u3002&#xff08;\u4ec5\u4ec5\u53ea\u9501\u4f4f\u6876&#xff09;<\/p>\n<\/li>\n<li>\n<p>\u66f4\u65b0 size&#xff1a;<br \/>\n\u4f7f\u7528\u00a0CAS\u00a0\u66f4\u65b0\u54c8\u5e0c\u8868\u7684\u5927\u5c0f&#xff08;\u7b80\u5316\u7248&#xff0c;\u5b9e\u9645\u903b\u8f91\u66f4\u590d\u6742 &#xff09;\u3002<\/p>\n<\/li>\n<li>\n<p>\u8fd4\u56de\u65e7\u503c&#xff1a;<br \/>\n\u5982\u679c\u63d2\u5165\u524d\u5b58\u5728\u65e7\u503c&#xff0c;\u5219\u8fd4\u56de\u65e7\u503c&#xff1b;\u5426\u5219\u8fd4\u56de\u00a0null\u3002<\/p>\n<\/li>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"493\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/08\/20250814061548-689d7f1426579.png\" width=\"1655\" \/><\/p>\n<p>\u5982\u679c\u9047\u5230\u6269\u5bb9\u4e2d\u7684\u00a0ForwardingNode&#xff0c;\u534f\u52a9\u5b8c\u6210\u6269\u5bb9(\u9501\u4f4f\u94fe\u8868\u5e2e\u5fd9\u6269\u5bb9)<\/p>\n<h4 style=\"background-color:transparent\">initTable\u6d41\u7a0b<\/h4>\n<p>\u4fdd\u8bc1\u53ea\u6709\u4e00\u4e2a\u7ebf\u7a0b\u80fd\u521b\u5efahash\u8868&#xff0c;\u5176\u4ed6\u7ebf\u7a0b\u90fd\u662f\u76f2\u7b49\u5f85<\/p>\n<p>private final Node&lt;K, V&gt;[] initTable() {<br \/>\n    Node&lt;K, V&gt;[] tab;<br \/>\n    int sc;<br \/>\n    \/\/ \u5faa\u73af\u5c1d\u8bd5\u521d\u59cb\u5316&#xff1a;\u53ea\u8981 table \u672a\u521d\u59cb\u5316&#xff08;null \u6216\u957f\u5ea6 0 &#xff09;&#xff0c;\u5c31\u6301\u7eed\u5c1d\u8bd5<br \/>\n    while ((tab &#061; table) &#061;&#061; null || tab.length &#061;&#061; 0) {<br \/>\n        \/\/ sizeCtl &lt; 0 \u8bf4\u660e\u6709\u5176\u4ed6\u7ebf\u7a0b\u6b63\u5728\u521d\u59cb\u5316&#xff0c;\u5f53\u524d\u7ebf\u7a0b\u4e3b\u52a8\u8ba9\u51fa CPU&#xff0c;\u907f\u514d\u7ade\u4e89<br \/>\n        if ((sc &#061; sizeCtl) &lt; 0) {<br \/>\n            Thread.yield();<br \/>\n        }<br \/>\n        \/\/ \u5c1d\u8bd5\u7528 CAS \u5c06 sizeCtl \u4ece sc&#xff08;\u539f\u72b6\u6001&#xff09;\u6539\u4e3a -1&#xff0c;\u6807\u8bb0\u201c\u6b63\u5728\u521d\u59cb\u5316\u201d<br \/>\n        else if (U.compareAndSwapInt(this, SIZECTL, sc, -1)) {<br \/>\n            try {<br \/>\n                \/\/ \u4e8c\u6b21\u68c0\u67e5&#xff1a;\u9632\u6b62\u5176\u4ed6\u7ebf\u7a0b\u5df2\u7ecf\u521d\u59cb\u5316\u4e86 table<br \/>\n                if ((tab &#061; table) &#061;&#061; null || tab.length &#061;&#061; 0) {<br \/>\n                    \/\/ \u786e\u5b9a\u521d\u59cb\u5316\u5bb9\u91cf&#xff1a;\u6709\u81ea\u5b9a\u4e49\u5bb9\u91cf&#xff08;sc &gt; 0 &#xff09;\u5219\u7528&#xff0c;\u5426\u5219\u7528\u9ed8\u8ba4 16<br \/>\n                    int n &#061; (sc &gt; 0) ? sc : DEFAULT_CAPACITY;<br \/>\n                    \/\/ \u521b\u5efa Node \u6570\u7ec4&#xff0c;\u957f\u5ea6\u4e3a n&#xff0c;\u4f5c\u4e3a\u54c8\u5e0c\u8868\u7684\u5b58\u50a8\u7ed3\u6784<br \/>\n                    Node&lt;K, V&gt;[] nt &#061; (Node&lt;K, V&gt;[]) new Node&lt;?, ?&gt;[n];<br \/>\n                    \/\/ \u5c06\u65b0\u6570\u7ec4\u8d4b\u503c\u7ed9 table&#xff0c;\u5b8c\u6210\u521d\u59cb\u5316<br \/>\n                    table &#061; tab &#061; nt;<br \/>\n                    \/\/ \u8ba1\u7b97\u6269\u5bb9\u9608\u503c&#xff1a;n &#8211; (n &gt;&gt;&gt; 2) \u7b49\u4ef7\u4e8e n * 0.75&#xff08;\u8d1f\u8f7d\u56e0\u5b50 0.75 &#xff09;<br \/>\n                    sc &#061; n &#8211; (n &gt;&gt;&gt; 2);<br \/>\n                }<br \/>\n            } finally {<br \/>\n                \/\/ \u66f4\u65b0 sizeCtl \u4e3a\u6269\u5bb9\u9608\u503c&#xff0c;\u540e\u7eed\u7528\u4e8e\u5224\u65ad\u662f\u5426\u9700\u8981\u6269\u5bb9<br \/>\n                sizeCtl &#061; sc;<br \/>\n            }<br \/>\n            break; \/\/ \u521d\u59cb\u5316\u5b8c\u6210&#xff0c;\u8df3\u51fa\u5faa\u73af<br \/>\n        }<br \/>\n    }<br \/>\n    return tab; \/\/ \u8fd4\u56de\u521d\u59cb\u5316\u597d\u7684\u54c8\u5e0c\u8868\u6570\u7ec4<br \/>\n}<\/p>\n<li>\u5faa\u73af\u5c1d\u8bd5\u521d\u59cb\u5316&#xff1a;<br \/>\n\u6301\u7eed\u68c0\u67e5\u00a0table\u00a0\u662f\u5426\u672a\u521d\u59cb\u5316&#xff08;null\u00a0\u6216\u957f\u5ea6\u4e3a 0 &#xff09;&#xff0c;\u672a\u521d\u59cb\u5316\u5219\u8fdb\u5165\u521d\u59cb\u5316\u6d41\u7a0b\u3002<\/li>\n<li>\u5904\u7406\u7ade\u4e89\u7ebf\u7a0b&#xff1a;<br \/>\n\u82e5\u00a0sizeCtl &lt; 0&#xff0c;\u8bf4\u660e\u5176\u4ed6\u7ebf\u7a0b\u6b63\u5728\u521d\u59cb\u5316&#xff0c;\u5f53\u524d\u7ebf\u7a0b\u8c03\u7528\u00a0Thread.yield()\u00a0\u8ba9\u51fa CPU&#xff0c;\u907f\u514d\u51b2\u7a81\u3002<\/li>\n<li>CAS \u6807\u8bb0\u521d\u59cb\u5316&#xff1a;<br \/>\n\u901a\u8fc7\u00a0UNSAFE.compareAndSwapInt\u00a0\u5c06\u00a0sizeCtl\u00a0\u8bbe\u4e3a\u00a0-1&#xff0c;\u6807\u8bb0 \u201c\u5f53\u524d\u7ebf\u7a0b\u6b63\u5728\u521d\u59cb\u5316\u201d&#xff0c;\u4fdd\u8bc1\u7ebf\u7a0b\u5b89\u5168\u3002<\/li>\n<li>\u4e8c\u6b21\u68c0\u67e5 table&#xff1a;<br \/>\n\u518d\u6b21\u786e\u8ba4\u00a0table\u00a0\u672a\u88ab\u5176\u4ed6\u7ebf\u7a0b\u521d\u59cb\u5316&#xff0c;\u9632\u6b62\u7ade\u4e89\u5bfc\u81f4\u91cd\u590d\u521b\u5efa\u3002<\/li>\n<li>\u786e\u5b9a\u521d\u59cb\u5316\u5bb9\u91cf&#xff1a;<br \/>\n\u81ea\u5b9a\u4e49\u5bb9\u91cf\u00a0sc &gt; 0\u00a0\u5219\u7528\u00a0sc&#xff0c;\u5426\u5219\u7528\u9ed8\u8ba4\u5bb9\u91cf&#xff08;\u5982 16 &#xff09;\u3002<\/li>\n<li>\u521b\u5efa Node \u6570\u7ec4&#xff1a;<br \/>\n\u65b0\u5efa\u957f\u5ea6\u4e3a\u00a0n\u00a0\u7684\u00a0Node\u00a0\u6570\u7ec4&#xff0c;\u4f5c\u4e3a\u54c8\u5e0c\u8868\u7684\u5e95\u5c42\u5b58\u50a8\u3002<\/li>\n<li>\u8bbe\u7f6e\u6269\u5bb9\u9608\u503c&#xff1a;<br \/>\n\u8ba1\u7b97\u00a0sizeCtl\u00a0\u4e3a\u00a0n &#8211; (n &gt;&gt;&gt; 2)&#xff08;\u5373\u00a00.75 * n\u00a0&#xff09;&#xff0c;\u4f5c\u4e3a\u540e\u7eed\u6269\u5bb9\u7684\u89e6\u53d1\u6761\u4ef6\u3002<\/li>\n<li>\u5b8c\u6210\u521d\u59cb\u5316&#xff1a;<br \/>\n\u66f4\u65b0\u00a0sizeCtl\u00a0\u5e76\u8df3\u51fa\u5faa\u73af&#xff0c;\u8fd4\u56de\u521d\u59cb\u5316\u597d\u7684\u00a0table\u3002<\/li>\n<\/p>\n<h4 style=\"background-color:transparent\">addCount\u6d41\u7a0b<\/h4>\n<p>\u5229\u7528\u4e86Longadder\u7684\u601d\u60f3&#xff0c;\u8bbe\u7f6e\u4e86\u591a\u4e2a\u7d2f\u52a0\u5355\u5143&#xff0c;\u51cf\u5c11CAS\u64cd\u4f5c\u7684\u51b2\u7a81<\/p>\n<p>private final void addCount(long x, int check) {<br \/>\n    CounterCell[] as; long b, s;<br \/>\n    \/\/ \u5206\u652f 1&#xff1a;\u4f18\u5148\u4f7f\u7528 CounterCells \u6216 CAS \u66f4\u65b0 baseCount<br \/>\n    if ((as &#061; counterCells) !&#061; null ||<br \/>\n        \/\/ \u82e5 counterCells \u4e0d\u5b58\u5728&#xff0c;\u5c1d\u8bd5 CAS \u66f4\u65b0 baseCount&#xff08;\u57fa\u7840\u8ba1\u6570 &#xff09;<br \/>\n        !U.compareAndSwapLong(this, BASECOUNT, b &#061; baseCount, s &#061; b &#043; x)) {<br \/>\n        CounterCell a; long v; int m;<br \/>\n        boolean uncontended &#061; true; \/\/ \u6807\u8bb0 Cell \u7684 CAS \u662f\u5426\u6210\u529f<br \/>\n        \/\/ \u5206\u652f 2&#xff1a;\u5904\u7406 CounterCells \u7684\u7ade\u4e89<br \/>\n        if (as &#061;&#061; null || (m &#061; as.length &#8211; 1) &lt; 0 ||<br \/>\n            \/\/ \u8ba1\u7b97\u7ebf\u7a0b\u5bf9\u5e94\u7684 Cell \u4e0b\u6807&#xff08;\u7ebf\u7a0b\u672c\u5730\u968f\u673a\u6570 &amp; \u957f\u5ea6 -1 &#xff09;<br \/>\n            (a &#061; as[ThreadLocalRandom.getProbe() &amp; m]) &#061;&#061; null ||<br \/>\n            \/\/ \u5c1d\u8bd5 CAS \u7d2f\u52a0 Cell \u7684\u503c<br \/>\n            !(uncontended &#061; U.compareAndSwapLong(a, CELLVALUE, v &#061; a.value, v &#043; x))) {<br \/>\n            \/\/ \u7ade\u4e89\u5931\u8d25&#xff0c;\u8fdb\u5165\u5168\u91cf\u7d2f\u52a0\u903b\u8f91&#xff08;\u521b\u5efa Cell \u6216\u6269\u5bb9 CounterCells &#xff09;<br \/>\n            fullAddCount(x, uncontended);<br \/>\n            return;<br \/>\n        }<br \/>\n        \/\/ \u5206\u652f 3&#xff1a;\u68c0\u67e5\u662f\u5426\u9700\u8981\u89e6\u53d1\u6269\u5bb9&#xff08;check \u53c2\u6570\u63a7\u5236 &#xff09;<br \/>\n        if (check &lt;&#061; 1)<br \/>\n            return;<br \/>\n        \/\/ \u8ba1\u7b97\u603b\u8ba1\u6570&#xff08;baseCount &#043; \u6240\u6709 Cell \u7684\u503c &#xff09;<br \/>\n        s &#061; sumCount();<br \/>\n    }<br \/>\n    \/\/ \u5206\u652f 4&#xff1a;\u6839\u636e\u603b\u8ba1\u6570\u68c0\u67e5\u662f\u5426\u9700\u8981\u6269\u5bb9\u54c8\u5e0c\u8868<br \/>\n    if (check &gt;&#061; 0) {<br \/>\n        Node&lt;K,V&gt;[] tab, nt; int n, sc;<br \/>\n        while (s &gt;&#061; (long)(sc &#061; sizeCtl) &amp;&amp; (tab &#061; table) !&#061; null &amp;&amp;<br \/>\n               (n &#061; tab.length) &lt; MAXIMUM_CAPACITY) {<br \/>\n            int rs &#061; resizeStamp(n);<br \/>\n            if (sc &lt; 0) { \/\/ \u54c8\u5e0c\u8868\u6b63\u5728\u6269\u5bb9<br \/>\n                \/\/ \u68c0\u67e5\u6269\u5bb9\u72b6\u6001&#xff0c;\u907f\u514d\u91cd\u590d\u6269\u5bb9<br \/>\n                if ((sc &gt;&gt;&gt; RESIZE_STAMP_SHIFT) !&#061; rs || sc &#061;&#061; rs &#043; 1 ||<br \/>\n                    sc &#061;&#061; rs &#043; MAX_RESIZERS || (nt &#061; nextTable) &#061;&#061; null ||<br \/>\n                    transferIndex &lt;&#061; 0)<br \/>\n                    break;<br \/>\n                \/\/ \u534f\u52a9\u6269\u5bb9<br \/>\n                if (U.compareAndSwapInt(this, SIZECTL, sc, sc &#043; 1))<br \/>\n                    transfer(tab, nt);<br \/>\n            }<br \/>\n            else { \/\/ \u54c8\u5e0c\u8868\u672a\u5728\u6269\u5bb9&#xff0c;\u5c1d\u8bd5\u89e6\u53d1\u6269\u5bb9<br \/>\n                if (U.compareAndSwapInt(this, SIZECTL, sc,<br \/>\n                                         (rs &lt;&lt; RESIZE_STAMP_SHIFT) &#043; 2))<br \/>\n                    transfer(tab, null);<br \/>\n            }<br \/>\n            s &#061; sumCount(); \/\/ \u91cd\u65b0\u8ba1\u7b97\u603b\u8ba1\u6570&#xff0c;\u5224\u65ad\u662f\u5426\u4ecd\u9700\u6269\u5bb9<br \/>\n        }<br \/>\n    }<br \/>\n}<\/p>\n<li>\n<p>\u4f18\u5148\u7d2f\u52a0\u5c1d\u8bd5&#xff1a;<br \/>\n\u82e5\u00a0CounterCells\u00a0\u5b58\u5728\u5219\u7528\u5176\u7d2f\u52a0&#xff0c;\u5426\u5219\u5c1d\u8bd5\u00a0CAS\u00a0\u66f4\u65b0\u00a0baseCount&#xff0c;\u907f\u514d\u9501\u7ade\u4e89\u3002<\/p>\n<\/li>\n<li>\n<p>\u5904\u7406 Cell \u7ade\u4e89&#xff1a;<br \/>\n\u8ba1\u7b97\u7ebf\u7a0b\u5bf9\u5e94\u7684\u00a0Cell\u00a0\u4e0b\u6807&#xff0c;\u5c1d\u8bd5\u00a0CAS\u00a0\u7d2f\u52a0\u00a0Cell\u00a0\u7684\u503c&#xff1b;\u82e5\u5931\u8d25\u5219\u8fdb\u5165\u00a0fullAddCount\u00a0\u5904\u7406\u3002<\/p>\n<\/li>\n<li>\n<p>\u5168\u91cf\u7d2f\u52a0\u903b\u8f91&#xff1a;<br \/>\n\u7ade\u4e89\u5931\u8d25\u65f6&#xff0c;\u521b\u5efa\u65b0\u00a0Cell\u00a0\u6216\u6269\u5bb9\u00a0CounterCells&#xff0c;\u4fdd\u8bc1\u8ba1\u6570\u4e0d\u4e22\u5931\u3002<\/p>\n<\/li>\n<li>\n<p>\u68c0\u67e5\u6269\u5bb9\u6761\u4ef6&#xff1a;<br \/>\n\u6839\u636e\u00a0check\u00a0\u53c2\u6570\u5224\u65ad\u662f\u5426\u9700\u8981\u89e6\u53d1\u54c8\u5e0c\u8868\u6269\u5bb9&#xff0c;\u901a\u8fc7\u00a0sumCount\u00a0\u8ba1\u7b97\u603b\u8ba1\u6570\u3002<\/p>\n<\/li>\n<li>\n<p>\u89e6\u53d1\u54c8\u5e0c\u8868\u6269\u5bb9&#xff1a;<br \/>\n\u82e5\u603b\u8ba1\u6570\u8d85\u8fc7\u9608\u503c&#xff0c;\u901a\u8fc7\u00a0transfer\u00a0\u65b9\u6cd5\u6269\u5bb9\u54c8\u5e0c\u8868&#xff0c;\u591a\u7ebf\u7a0b\u534f\u4f5c\u8fc1\u79fb\u6570\u636e\u3002<\/p>\n<\/li>\n<h4 style=\"background-color:transparent\">size\u8ba1\u7b97\u6d41\u7a0b<\/h4>\n<p>\u501f\u9274\u00a0LongAdder\u00a0\u7684\u8bbe\u8ba1\u601d\u8def&#xff0c;\u5c06\u8ba1\u6570\u5206\u4e3a\u00a0baseCount&#xff08;\u65e0\u7ade\u4e89 &#xff09;\u548c\u00a0counterCells&#xff08;\u6709\u7ade\u4e89 &#xff09;&#xff0c;size\u00a0\u8ba1\u7b97\u65f6\u518d\u7d2f\u52a0&#xff0c;\u907f\u514d\u5b9e\u65f6\u7ef4\u62a4\u7cbe\u786e\u8ba1\u6570\u5e26\u6765\u7684\u9ad8\u5e76\u53d1\u7ade\u4e89\u3002<\/p>\n<p>\u9ad8\u5e76\u53d1\u4e0b&#xff0c;baseCount\u00a0\u548c\u00a0counterCells\u00a0\u7684\u66f4\u65b0\u53ef\u80fd\u5b58\u5728\u5ef6\u8fdf&#xff0c;size\u00a0\u65b9\u6cd5\u8fd4\u56de\u7684\u662f \u201c\u8fd1\u4f3c\u51c6\u786e\u201d \u7684\u8ba1\u6570&#xff08;\u6700\u7ec8\u4f1a\u4e00\u81f4&#xff0c;\u4f46\u83b7\u53d6\u77ac\u95f4\u53ef\u80fd\u6709\u504f\u5dee &#xff09;&#xff0c;\u8fd9\u662f\u4e3a\u4e86\u6362\u53d6\u9ad8\u5e76\u53d1\u573a\u666f\u4e0b\u7684\u6027\u80fd\u63d0\u5347\u3002<\/p>\n<ul>\n<li>size \u8ba1\u7b97\u5b9e\u9645\u53d1\u751f\u5728 put\u3001remove \u7b49\u6539\u53d8\u96c6\u5408\u5143\u7d20\u7684\u64cd\u4f5c\u4e4b\u4e2d<\/li>\n<li>\u8ba1\u6570\u7d2f\u52a0\u89c4\u5219&#xff1a;\n<ul>\n<li>\u6ca1\u6709\u7ade\u4e89\u53d1\u751f\u65f6&#xff0c;\u5411 baseCount \u7d2f\u52a0\u8ba1\u6570<\/li>\n<li>\u6709\u7ade\u4e89\u53d1\u751f\u65f6&#xff0c;\u65b0\u5efa counterCells&#xff0c;\u5411\u5176\u4e2d\u4e00\u4e2a cell \u7d2f\u52a0\u8ba1\u6570\n<ul>\n<li>counterCells \u521d\u59cb\u6709\u4e24\u4e2a cell<\/li>\n<li>\u82e5\u8ba1\u6570\u7ade\u4e89\u8f83\u6fc0\u70c8&#xff0c;\u4f1a\u521b\u5efa\u65b0\u7684 cell \u6765\u7d2f\u52a0\u8ba1\u6570<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>\/\/ size \u65b9\u6cd5&#xff0c;\u7528\u4e8e\u83b7\u53d6 ConcurrentHashMap \u4e2d\u5143\u7d20\u7684\u6570\u91cf<br \/>\npublic int size() {<br \/>\n    \/\/ \u8c03\u7528 sumCount \u65b9\u6cd5&#xff0c;\u8ba1\u7b97 baseCount \u548c\u6240\u6709 counterCells \u4e2d\u7684\u8ba1\u6570\u503c\u603b\u548c<br \/>\n    long n &#061; sumCount();<br \/>\n    \/\/ \u5904\u7406\u8ba1\u6570\u503c\u7684\u8fb9\u754c\u60c5\u51b5&#xff1a;<br \/>\n    \/\/ &#8211; \u5982\u679c\u603b\u548c n \u5c0f\u4e8e 0&#xff0c;\u8fd4\u56de 0&#xff08;\u7406\u8bba\u4e0a\u4e0d\u4f1a\u51fa\u73b0&#xff0c;\u8fd9\u91cc\u505a\u9632\u5fa1\u6027\u5904\u7406 &#xff09;<br \/>\n    \/\/ &#8211; \u5982\u679c\u603b\u548c n \u8d85\u8fc7 Integer \u7684\u6700\u5927\u503c&#xff0c;\u8fd4\u56de Integer.MAX_VALUE<br \/>\n    \/\/ &#8211; \u5426\u5219&#xff0c;\u5c06 long \u7c7b\u578b\u7684\u603b\u548c\u8f6c\u6362\u4e3a int \u7c7b\u578b\u8fd4\u56de<br \/>\n    return (n &lt; 0L) ? 0 :<br \/>\n            (n &gt; (long) Integer.MAX_VALUE) ? Integer.MAX_VALUE : (int) n;<br \/>\n}<\/p>\n<p>\/\/ sumCount \u65b9\u6cd5&#xff0c;\u8ba1\u7b97 ConcurrentHashMap \u7684\u603b\u8ba1\u6570<br \/>\nfinal long sumCount() {<br \/>\n    \/\/ \u83b7\u53d6\u5f53\u524d\u7684\u5206\u6bb5\u8ba1\u6570\u6570\u7ec4 counterCells<br \/>\n    CounterCell[] as &#061; counterCells;<br \/>\n    CounterCell a;<br \/>\n    \/\/ \u5148\u5c06\u57fa\u7840\u8ba1\u6570 baseCount \u7684\u503c\u7d2f\u52a0\u5230\u603b\u548c\u4e2d&#xff0c;baseCount \u7528\u4e8e\u65e0\u7ade\u4e89\u573a\u666f\u7684\u8ba1\u6570<br \/>\n    long sum &#061; baseCount.get();<br \/>\n    \/\/ \u5982\u679c\u5206\u6bb5\u8ba1\u6570\u6570\u7ec4\u4e0d\u4e3a null&#xff0c;\u8bf4\u660e\u5b58\u5728\u7ade\u4e89\u573a\u666f&#xff0c;\u9700\u8981\u904d\u5386\u7d2f\u52a0\u6bcf\u4e2a Cell \u7684\u8ba1\u6570\u503c<br \/>\n    if (as !&#061; null) {<br \/>\n        for (int i &#061; 0; i &lt; as.length; &#043;&#043;i) {<br \/>\n            \/\/ \u53d6\u51fa\u5f53\u524d Cell&#xff0c;\u82e5 Cell \u4e0d\u4e3a null&#xff0c;\u5c06\u5176\u8ba1\u6570\u503c\u7d2f\u52a0\u5230\u603b\u548c<br \/>\n            if ((a &#061; as[i]) !&#061; null) {<br \/>\n                sum &#043;&#061; a.value;<br \/>\n            }<br \/>\n        }<br \/>\n    }<br \/>\n    \/\/ \u8fd4\u56de\u7d2f\u52a0\u540e\u7684\u603b\u8ba1\u6570&#xff0c;\u4ee3\u8868 ConcurrentHashMap \u4e2d\u5143\u7d20\u7684\u5927\u81f4\u6570\u91cf&#xff08;\u9ad8\u5e76\u53d1\u4e0b\u53ef\u80fd\u6709\u5ef6\u8fdf &#xff09;<br \/>\n    return sum;<br \/>\n}<\/p>\n<li>size\u00a0\u65b9\u6cd5\u5165\u53e3&#xff1a;<br \/>\n\u8c03\u7528\u00a0sumCount\u00a0\u65b9\u6cd5&#xff0c;\u83b7\u53d6\u00a0ConcurrentHashMap\u00a0\u4e2d\u5143\u7d20\u6570\u91cf\u7684\u603b\u8ba1\u6570\u3002<\/li>\n<li>\u8c03\u7528\u00a0sumCount&#xff1a;<br \/>\n\u8fdb\u5165\u00a0sumCount\u00a0\u65b9\u6cd5&#xff0c;\u5f00\u59cb\u8ba1\u7b97\u603b\u8ba1\u6570\u3002<\/li>\n<li>\u7d2f\u52a0\u57fa\u7840\u8ba1\u6570&#xff1a;<br \/>\n\u5148\u5c06\u00a0baseCount&#xff08;\u65e0\u7ade\u4e89\u573a\u666f\u7684\u8ba1\u6570 &#xff09;\u7684\u503c\u7d2f\u52a0\u5230\u603b\u548c\u00a0sum\u00a0\u4e2d\u3002<\/li>\n<li>\u904d\u5386\u5206\u6bb5\u8ba1\u6570\u6570\u7ec4&#xff1a;<br \/>\n\u82e5\u00a0counterCells&#xff08;\u5206\u6bb5\u8ba1\u6570\u6570\u7ec4&#xff0c;\u6709\u7ade\u4e89\u573a\u666f\u4f7f\u7528 &#xff09;\u4e0d\u4e3a\u00a0null&#xff0c;\u904d\u5386\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e2a\u00a0CounterCell&#xff0c;\u5c06\u975e\u00a0null\u00a0Cell \u7684\u8ba1\u6570\u503c\u7d2f\u52a0\u5230\u00a0sum\u3002<\/li>\n<li>\u8fd4\u56de\u603b\u8ba1\u6570&#xff1a;<br \/>\nsumCount\u00a0\u8fd4\u56de\u7d2f\u52a0\u540e\u7684\u603b\u8ba1\u6570&#xff0c;\u4ee3\u8868\u96c6\u5408\u5143\u7d20\u7684\u5927\u81f4\u6570\u91cf\u3002<\/li>\n<li>\u5904\u7406\u00a0size\u00a0\u8fd4\u56de\u503c&#xff1a;<br \/>\n\u5bf9\u00a0sumCount\u00a0\u8fd4\u56de\u7684\u603b\u8ba1\u6570\u505a\u8fb9\u754c\u5904\u7406&#xff0c;\u8f6c\u6362\u4e3a\u00a0int\u00a0\u7c7b\u578b\u8fd4\u56de&#xff0c;\u8d85\u8fc7\u00a0Integer.MAX_VALUE\u00a0\u5219\u8fd4\u56de\u6700\u5927\u503c&#xff0c;\u5c0f\u4e8e 0 \u5219\u8fd4\u56de 0 \u3002<\/li>\n<\/p>\n<h4 style=\"background-color:transparent\">transfer\u6d41\u7a0b<\/h4>\n<li>\n<p>\u591a\u7ebf\u7a0b\u534f\u4f5c\u6269\u5bb9&#xff1a;<br \/>\n\u901a\u8fc7\u00a0transferIndex\u00a0\u5206\u914d\u8fc1\u79fb\u8303\u56f4&#xff0c;\u5141\u8bb8\u591a\u7ebf\u7a0b\u540c\u65f6\u8fc1\u79fb\u4e0d\u540c\u6876&#xff0c;\u63d0\u5347\u6269\u5bb9\u6548\u7387\u3002<\/p>\n<\/li>\n<li>\n<p>\u6e10\u8fdb\u5f0f\u8fc1\u79fb&#xff1a;<br \/>\n\u6bcf\u6b21\u8fc1\u79fb\u4e00\u4e2a\u6876&#xff0c;\u907f\u514d\u957f\u65f6\u95f4\u6301\u6709\u9501&#xff0c;\u4fdd\u8bc1\u9ad8\u5e76\u53d1\u4e0b\u7684\u54cd\u5e94\u6027\u3002<\/p>\n<\/li>\n<li>\n<p>\u81ea\u52a8\u91cd\u5b9a\u5411&#xff1a;<br \/>\nForwardingNode\u00a0\u81ea\u52a8\u91cd\u5b9a\u5411\u8bbf\u95ee\u5230\u65b0\u6570\u7ec4&#xff0c;\u4fdd\u8bc1\u6269\u5bb9\u65f6\u8bfb\u5199\u64cd\u4f5c\u7684\u6b63\u786e\u6027\u3002<\/p>\n<\/li>\n<li>\n<p>\u94fe\u8868\u4e0e\u7ea2\u9ed1\u6811\u62c6\u5206&#xff1a;<br \/>\n\u9002\u914d\u4e0d\u540c\u8282\u70b9\u7c7b\u578b&#xff08;\u94fe\u8868 \/ \u7ea2\u9ed1\u6811 &#xff09;&#xff0c;\u4fdd\u8bc1\u62c6\u5206\u548c\u8fc1\u79fb\u7684\u6548\u7387\u3002<\/p>\n<\/li>\n<p>\/\/ \u6807\u8bb0\u8282\u70b9\u6b63\u5728\u8fc1\u79fb&#xff08;hash \u503c&#xff09;<br \/>\nprivate static final int MOVED &#061; -1;<br \/>\n\/\/ CPU \u6838\u5fc3\u6570<br \/>\nprivate static final int NCPU &#061; Runtime.getRuntime().availableProcessors();<br \/>\n\/\/ \u6700\u5c0f\u8fc1\u79fb\u6b65\u957f<br \/>\nprivate static final int MIN_TRANSFER_STRIDE &#061; 16; <\/p>\n<p>private final void transfer(Node&lt;K,V&gt;[] tab, Node&lt;K,V&gt;[] nextTab) {<br \/>\n    int n &#061; tab.length, stride;<br \/>\n    \/\/ \u8ba1\u7b97\u6bcf\u4e2a\u7ebf\u7a0b\u7684\u8fc1\u79fb\u6b65\u957f&#xff08;stride&#xff09;<br \/>\n    if ((stride &#061; (NCPU &gt; 1) ? (n &gt;&gt;&gt; 3) \/ NCPU : n) &lt; MIN_TRANSFER_STRIDE)<br \/>\n        stride &#061; MIN_TRANSFER_STRIDE; \/\/ \u4fdd\u8bc1\u6b65\u957f\u4e0d\u5c0f\u4e8e 16<br \/>\n    \/\/ \u521d\u59cb\u5316 nextTab&#xff08;\u6269\u5bb9\u540e\u7684\u65b0\u6570\u7ec4&#xff09;<br \/>\n    if (nextTab &#061;&#061; null) {<br \/>\n        try {<br \/>\n            nextTab &#061; (Node&lt;K,V&gt;[])new Node&lt;?,?&gt;[n &lt;&lt; 1]; \/\/ \u65b0\u5bb9\u91cf\u4e3a\u539f\u5bb9\u91cf\u7684 2 \u500d<br \/>\n        } catch (Throwable ex) {<br \/>\n            sizeCtl &#061; Integer.MAX_VALUE;<br \/>\n            return;<br \/>\n        }<br \/>\n        nextTable &#061; nextTab; \/\/ \u8bb0\u5f55\u65b0\u6570\u7ec4<br \/>\n        transferIndex &#061; n; \/\/ \u521d\u59cb\u5316\u8fc1\u79fb\u7d22\u5f15<br \/>\n    }<br \/>\n    int nextn &#061; nextTab.length;<br \/>\n    \/\/ \u521b\u5efa ForwardingNode&#xff0c;\u6807\u8bb0\u5f53\u524d\u6876\u6b63\u5728\u8fc1\u79fb<br \/>\n    ForwardingNode&lt;K,V&gt; fwd &#061; new ForwardingNode&lt;K,V&gt;(nextTab);<br \/>\n    boolean advance &#061; true; \/\/ \u6807\u8bb0\u662f\u5426\u7ee7\u7eed\u8fc1\u79fb\u4e0b\u4e00\u4e2a\u6876<br \/>\n    boolean finishing &#061; false; \/\/ \u6807\u8bb0\u662f\u5426\u5b8c\u6210\u6240\u6709\u8fc1\u79fb<br \/>\n    \/\/ \u5faa\u73af\u8fc1\u79fb\u6bcf\u4e2a\u6876<br \/>\n    for (int i &#061; 0, bound &#061; 0;;) {<br \/>\n        Node&lt;K,V&gt; f; int fh;<br \/>\n        \/\/ \u786e\u5b9a\u5f53\u524d\u7ebf\u7a0b\u7684\u8fc1\u79fb\u8303\u56f4<br \/>\n        while (advance) {<br \/>\n            int nextIndex, nextBound;<br \/>\n            if (&#8211;transferIndex &gt;&#061; 0) {<br \/>\n                nextIndex &#061; transferIndex;<br \/>\n                nextBound &#061; (nextIndex &gt; stride ? nextIndex &#8211; stride : 0);<br \/>\n                bound &#061; nextBound;<br \/>\n                i &#061; nextIndex;<br \/>\n                advance &#061; false;<br \/>\n            } else {<br \/>\n                advance &#061; false;<br \/>\n                if (finishing) { \/\/ \u6240\u6709\u6876\u8fc1\u79fb\u5b8c\u6210<br \/>\n                    nextTable &#061; null;<br \/>\n                    table &#061; nextTab; \/\/ \u66ff\u6362\u4e3a\u65b0\u6570\u7ec4<br \/>\n                    sizeCtl &#061; (n &lt;&lt; 1) &#8211; (n &gt;&gt;&gt; 1); \/\/ \u66f4\u65b0\u6269\u5bb9\u9608\u503c<br \/>\n                    return;<br \/>\n                }<br \/>\n            }<br \/>\n        }<br \/>\n        \/\/ \u68c0\u67e5\u5f53\u524d\u6876\u662f\u5426\u8d8a\u754c<br \/>\n        if (i &lt; 0 || i &gt;&#061; n || i &#043; n &gt;&#061; nextn)<br \/>\n            continue;<br \/>\n        \/\/ \u83b7\u53d6\u5f53\u524d\u6876\u7684\u5934\u8282\u70b9<br \/>\n        if ((f &#061; tabAt(tab, i)) &#061;&#061; null) {<br \/>\n            advance &#061; casTabAt(tab, i, null, fwd); \/\/ \u6807\u8bb0\u6876\u4e3a\u8fc1\u79fb\u4e2d<br \/>\n        }<br \/>\n        \/\/ \u5904\u7406\u6b63\u5728\u8fc1\u79fb\u7684\u6876&#xff08;\u8df3\u8fc7&#xff09;<br \/>\n        else if ((fh &#061; f.hash) &#061;&#061; MOVED) {<br \/>\n            advance &#061; true;<br \/>\n        }<br \/>\n        \/\/ \u8fc1\u79fb\u5f53\u524d\u6876\u7684\u8282\u70b9<br \/>\n        else {<br \/>\n            synchronized (f) { \/\/ \u52a0\u9501\u5f53\u524d\u6876\u7684\u5934\u8282\u70b9<br \/>\n                if (tabAt(tab, i) &#061;&#061; f) { \/\/ \u4e8c\u6b21\u68c0\u67e5&#xff0c;\u9632\u6b62\u5e76\u53d1\u4fee\u6539<br \/>\n                    Node&lt;K,V&gt; ln, hn;<br \/>\n                    if (fh &gt;&#061; 0) { \/\/ \u5934\u8282\u70b9\u662f\u666e\u901a\u94fe\u8868\u8282\u70b9<br \/>\n                        int runBit &#061; fh &amp; n; \/\/ \u8ba1\u7b97\u8fc1\u79fb\u540e\u7684\u4f4d\u7f6e<br \/>\n                        Node&lt;K,V&gt; lastRun &#061; f;<br \/>\n                        Node&lt;K,V&gt; p &#061; f.next;<br \/>\n                        \/\/ \u627e\u5230\u6700\u540e\u4e00\u6bb5\u8fde\u7eed\u76f8\u540c runBit \u7684\u8282\u70b9<br \/>\n                        while (p !&#061; null) {<br \/>\n                            int b &#061; p.hash &amp; n;<br \/>\n                            if (b !&#061; runBit) {<br \/>\n                                runBit &#061; b;<br \/>\n                                lastRun &#061; p;<br \/>\n                            }<br \/>\n                            p &#061; p.next;<br \/>\n                        }<br \/>\n                        \/\/ \u5c06\u6700\u540e\u4e00\u6bb5\u8282\u70b9\u76f4\u63a5\u653e\u5165\u65b0\u6876<br \/>\n                        if (runBit &#061;&#061; (fh &amp; n)) {<br \/>\n                            ln &#061; lastRun;<br \/>\n                            hn &#061; null;<br \/>\n                        } else {<br \/>\n                            \/\/ \u62c6\u5206\u94fe\u8868\u4e3a\u4e24\u90e8\u5206<br \/>\n                            ln &#061; null;<br \/>\n                            hn &#061; lastRun;<br \/>\n                            for (p &#061; f; p !&#061; lastRun; p &#061; p.next) {<br \/>\n                                int b &#061; p.hash &amp; n;<br \/>\n                                Node&lt;K,V&gt; newNode &#061; new Node&lt;K,V&gt;(p.hash, p.key, p.val, null);<br \/>\n                                if (b &#061;&#061; runBit)<br \/>\n                                    ln &#061; (ln &#061;&#061; null) ? newNode : ln.next &#061; newNode;<br \/>\n                                else<br \/>\n                                    hn &#061; (hn &#061;&#061; null) ? newNode : hn.next &#061; newNode;<br \/>\n                            }<br \/>\n                        }<br \/>\n                        \/\/ \u5c06\u62c6\u5206\u540e\u7684\u94fe\u8868\u653e\u5165\u65b0\u6876<br \/>\n                        setTabAt(nextTab, i, ln);<br \/>\n                        setTabAt(nextTab, i &#043; n, hn);<br \/>\n                        \/\/ \u6807\u8bb0\u5f53\u524d\u6876\u5df2\u8fc1\u79fb<br \/>\n                        setTabAt(tab, i, fwd);<br \/>\n                        advance &#061; true;<br \/>\n                    } else if (f instanceof TreeBin) { \/\/ \u5934\u8282\u70b9\u662f\u7ea2\u9ed1\u6811<br \/>\n                        TreeBin&lt;K,V&gt; t &#061; (TreeBin&lt;K,V&gt;)f;<br \/>\n                        TreeNode&lt;K,V&gt; lo &#061; null, loTail &#061; null;<br \/>\n                        TreeNode&lt;K,V&gt; hi &#061; null, hiTail &#061; null;<br \/>\n                        int lc &#061; 0, hc &#061; 0;<br \/>\n                        \/\/ \u904d\u5386\u7ea2\u9ed1\u6811\u8282\u70b9&#xff0c;\u62c6\u5206\u5230\u65b0\u6876<br \/>\n                        for (Node&lt;K,V&gt; e &#061; t.first; e !&#061; null; e &#061; e.next) {<br \/>\n                            int h &#061; e.hash;<br \/>\n                            TreeNode&lt;K,V&gt; p &#061; new TreeNode&lt;K,V&gt;(h, e.key, e.val, null, null);<br \/>\n                            if ((h &amp; n) &#061;&#061; 0) {<br \/>\n                                if (loTail &#061;&#061; null) lo &#061; p;<br \/>\n                                else loTail.next &#061; p;<br \/>\n                                loTail &#061; p;<br \/>\n                                lc&#043;&#043;;<br \/>\n                            } else {<br \/>\n                                if (hiTail &#061;&#061; null) hi &#061; p;<br \/>\n                                else hiTail.next &#061; p;<br \/>\n                                hiTail &#061; p;<br \/>\n                                hc&#043;&#043;;<br \/>\n                            }<br \/>\n                        }<br \/>\n                        \/\/ \u5224\u65ad\u662f\u5426\u9700\u8981\u8f6c\u6362\u4e3a\u94fe\u8868<br \/>\n                        ln &#061; (lc &lt;&#061; 6) ? untreeify(lo) : (hc !&#061; 0) ? new TreeBin&lt;K,V&gt;(lo) : t;<br \/>\n                        hn &#061; (hc &lt;&#061; 6) ? untreeify(hi) : (lc !&#061; 0) ? new TreeBin&lt;K,V&gt;(hi) : t;<br \/>\n                        \/\/ \u653e\u5165\u65b0\u6876<br \/>\n                        setTabAt(nextTab, i, ln);<br \/>\n                        setTabAt(nextTab, i &#043; n, hn);<br \/>\n                        \/\/ \u6807\u8bb0\u5f53\u524d\u6876\u5df2\u8fc1\u79fb<br \/>\n                        setTabAt(tab, i, fwd);<br \/>\n                        advance &#061; true;<br \/>\n                    }<br \/>\n                }<br \/>\n            }<br \/>\n        }<br \/>\n    }<br \/>\n}<\/p>\n<p>\/\/ \u8f85\u52a9\u65b9\u6cd5&#xff1a;CAS \u8bbe\u7f6e\u6876\u7684\u8282\u70b9<br \/>\nprivate void setTabAt(Node&lt;K,V&gt;[] tab, int i, Node&lt;K,V&gt; v) {<br \/>\n    U.putObjectVolatile(tab, ((long)i &lt;&lt; ASHIFT) &#043; ABASE, v);<br \/>\n}<\/p>\n<li>\n<p>\u8ba1\u7b97\u8fc1\u79fb\u6b65\u957f&#xff1a;<br \/>\n\u6839\u636e CPU \u6838\u5fc3\u6570\u548c\u539f\u6570\u7ec4\u957f\u5ea6&#xff0c;\u786e\u5b9a\u6bcf\u4e2a\u7ebf\u7a0b\u7684\u8fc1\u79fb\u6876\u6570\u91cf\u3002<\/p>\n<\/li>\n<li>\n<p>\u521d\u59cb\u5316\u65b0\u6570\u7ec4&#xff1a;<br \/>\n\u521b\u5efa\u6269\u5bb9\u540e\u7684\u65b0\u6570\u7ec4&#xff08;\u5bb9\u91cf\u7ffb\u500d&#xff09;&#xff0c;\u6807\u8bb0\u8fc1\u79fb\u72b6\u6001\u3002<\/p>\n<\/li>\n<li>\n<p>\u521b\u5efa ForwardingNode&#xff1a;<br \/>\n\u6807\u8bb0\u6876\u8fc1\u79fb\u72b6\u6001&#xff0c;\u7528\u4e8e\u91cd\u5b9a\u5411\u8bbf\u95ee\u3002<\/p>\n<\/li>\n<li>\n<p>\u5206\u914d\u8fc1\u79fb\u8303\u56f4&#xff1a;<br \/>\n\u901a\u8fc7\u00a0transferIndex\u00a0\u5206\u914d\u7ebf\u7a0b\u7684\u8fc1\u79fb\u6876\u8303\u56f4&#xff0c;\u907f\u514d\u7ade\u4e89\u3002<\/p>\n<\/li>\n<li>\n<p>\u5904\u7406\u7a7a\u6876&#xff1a;<br \/>\n\u6807\u8bb0\u4e3a\u7a7a\u6876\u4e3a\u8fc1\u79fb\u4e2d&#xff08;fwd\u00a0&#xff09;\u3002<\/p>\n<\/li>\n<li>\n<p>\u5904\u7406\u8fc1\u79fb\u4e2d\u6876&#xff1a;<br \/>\n\u8df3\u8fc7\u5df2\u6807\u8bb0\u4e3a\u00a0MOVED\u00a0\u7684\u6876\u3002<\/p>\n<\/li>\n<li>\n<p>\u62c6\u5206\u94fe\u8868 \/ \u7ea2\u9ed1\u6811&#xff1a;<br \/>\n\u52a0\u9501\u540e\u62c6\u5206\u8282\u70b9&#xff0c;\u5206\u522b\u653e\u5165\u65b0\u6570\u7ec4\u7684\u539f\u4f4d\u7f6e\u548c\u6269\u5bb9\u4f4d\u7f6e\u3002<\/p>\n<\/li>\n<li>\n<p>\u6807\u8bb0\u8fc1\u79fb\u5b8c\u6210&#xff1a;<br \/>\n\u66ff\u6362\u539f\u6570\u7ec4\u4e3a\u65b0\u6570\u7ec4&#xff0c;\u66f4\u65b0\u6269\u5bb9\u9608\u503c\u3002<\/p>\n<\/li>\n<h3>jdk7<\/h3>\n<p>\u7ef4\u62a4\u4e00\u4e2a\u00a0segment\u00a0\u6570\u7ec4&#xff0c;\u6bcf\u4e2a\u00a0segment\u00a0\u5bf9\u5e94\u4e00\u628a\u9501<\/p>\n<ul>\n<li>\u4f18\u70b9&#xff1a;\u591a\u4e2a\u7ebf\u7a0b\u8bbf\u95ee\u4e0d\u540c\u00a0segment\u00a0\u65f6\u65e0\u51b2\u7a81&#xff0c;\u548c JDK8 \u4e2d\u7c7b\u4f3c&#xff08;JDK8 \u867d\u5e95\u5c42\u5b9e\u73b0\u4e0d\u540c&#xff0c;\u4f46\u4e5f\u6709\u5206\u6563\u7ade\u4e89\u7684\u601d\u60f3 &#xff09;<\/li>\n<li>\u7f3a\u70b9&#xff1a;Segments\u00a0\u6570\u7ec4\u9ed8\u8ba4\u5927\u5c0f\u4e3a 16&#xff0c;\u5bb9\u91cf\u521d\u59cb\u5316\u540e\u4e0d\u53ef\u6539\u53d8&#xff0c;\u4e14\u4e0d\u662f\u61d2\u60f0\u521d\u59cb\u5316&#xff08;\u521b\u5efa\u65f6\u5c31\u5206\u914d\u597d\u8d44\u6e90&#xff0c;\u672a\u5ef6\u8fdf\u5230\u9996\u6b21\u4f7f\u7528 &#xff09;<\/li>\n<\/ul>\n<h4>\u6784\u9020\u5668\u5206\u6790<\/h4>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"680\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/08\/20250814061550-689d7f1620a9c.png\" width=\"384\" \/><\/p>\n<p>\/\/ \u6700\u5927 Segment \u6570\u91cf&#xff08;\u5fc5\u987b\u662f 2 \u7684\u5e42&#xff0c;\u4e14\u4e0d\u8d85\u8fc7 1&lt;&lt;16&#xff09;<br \/>\nstatic final int MAX_SEGMENTS &#061; 1 &lt;&lt; 16;<br \/>\n\/\/ \u6bcf\u4e2a Segment \u4e2d\u54c8\u5e0c\u8868\u7684\u6700\u5c0f\u5bb9\u91cf&#xff08;\u5fc5\u987b\u662f 2 \u7684\u5e42&#xff0c;\u9ed8\u8ba4 2&#xff09;<br \/>\nstatic final int MIN_SEGMENT_TABLE_CAPACITY &#061; 2; <\/p>\n<p>public ConcurrentHashMap(int initialCapacity, float loadFactor, int concurrencyLevel) {<br \/>\n    \/\/ 1. \u53c2\u6570\u5408\u6cd5\u6027\u6821\u9a8c<br \/>\n    if (!(loadFactor &gt; 0) || initialCapacity &lt; 0 || concurrencyLevel &lt;&#061; 0) {<br \/>\n        throw new IllegalArgumentException();<br \/>\n    }<br \/>\n    \/\/ 2. \u9650\u5236 concurrencyLevel \u7684\u6700\u5927\u503c<br \/>\n    if (concurrencyLevel &gt; MAX_SEGMENTS) {<br \/>\n        concurrencyLevel &#061; MAX_SEGMENTS;<br \/>\n    }<br \/>\n    \/\/ 3. \u8ba1\u7b97 Segment \u6570\u7ec4\u7684\u5927\u5c0f&#xff08;ssize&#xff09;&#xff1a;\u5fc5\u987b\u662f 2 \u7684\u5e42<br \/>\n    int sshift &#061; 0; \/\/ \u5de6\u79fb\u6b21\u6570<br \/>\n    int ssize &#061; 1;  \/\/ Segment \u6570\u7ec4\u7684\u521d\u59cb\u5927\u5c0f&#xff08;\u81f3\u5c11\u4e3a 1&#xff09;<br \/>\n    while (ssize &lt; concurrencyLevel) {<br \/>\n        &#043;&#043;sshift;<br \/>\n        ssize &lt;&lt;&#061; 1; \/\/ \u5de6\u79fb 1 \u4f4d&#xff08;\u4e58\u4ee5 2&#xff09;&#xff0c;\u76f4\u5230 ssize &gt;&#061; concurrencyLevel<br \/>\n    }<br \/>\n    \/\/ 4. \u8ba1\u7b97 segmentShift \u548c segmentMask<br \/>\n    this.segmentShift &#061; 32 &#8211; sshift; \/\/ \u7528\u4e8e\u8ba1\u7b97 Segment \u4e0b\u6807<br \/>\n    this.segmentMask &#061; ssize &#8211; 1;    \/\/ \u7528\u4e8e\u8ba1\u7b97 Segment \u4e0b\u6807\u7684\u63a9\u7801<br \/>\n    \/\/ 5. \u9650\u5236 initialCapacity \u7684\u6700\u5927\u503c<br \/>\n    if (initialCapacity &gt; MAXIMUM_CAPACITY) {<br \/>\n        initialCapacity &#061; MAXIMUM_CAPACITY;<br \/>\n    }<br \/>\n    \/\/ 6. \u8ba1\u7b97\u6bcf\u4e2a Segment \u7684\u5bb9\u91cf&#xff08;cap&#xff09;<br \/>\n    int c &#061; initialCapacity \/ ssize; \/\/ \u5e73\u5747\u6bcf\u4e2a Segment \u7684\u5bb9\u91cf<br \/>\n    if (c * ssize &lt; initialCapacity) {<br \/>\n        &#043;&#043;c; \/\/ \u5411\u4e0a\u53d6\u6574<br \/>\n    }<br \/>\n    int cap &#061; MIN_SEGMENT_TABLE_CAPACITY; \/\/ \u6bcf\u4e2a Segment \u7684\u6700\u5c0f\u5bb9\u91cf<br \/>\n    while (cap &lt; c) {<br \/>\n        cap &lt;&lt;&#061; 1; \/\/ \u5de6\u79fb 1 \u4f4d&#xff08;\u4e58\u4ee5 2&#xff09;&#xff0c;\u76f4\u5230 cap &gt;&#061; c<br \/>\n    }<br \/>\n    \/\/ 7. \u521b\u5efa\u7b2c\u4e00\u4e2a Segment&#xff08;s0&#xff09;<br \/>\n    Segment&lt;K,V&gt; s0 &#061; new Segment&lt;K,V&gt;(loadFactor, (int)(cap * loadFactor),<br \/>\n                                       (HashEntry&lt;K,V&gt;[])new HashEntry[cap]);<br \/>\n    \/\/ 8. \u521b\u5efa Segment \u6570\u7ec4&#xff08;ss&#xff09;<br \/>\n    Segment&lt;K,V&gt;[] ss &#061; (Segment&lt;K,V&gt;[])new Segment[ssize];<br \/>\n    \/\/ 9. \u521d\u59cb\u5316 Segment \u6570\u7ec4\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20&#xff08;\u6709\u5e8f\u5199&#xff0c;\u907f\u514d\u6307\u4ee4\u91cd\u6392&#xff09;<br \/>\n    UNSAFE.putOrderedObject(ss, SBASE, s0);<br \/>\n    this.segments &#061; ss; \/\/ \u8d4b\u503c\u7ed9\u5168\u5c40\u53d8\u91cf<br \/>\n}<\/p>\n<li>\n<p>\u53c2\u6570\u6821\u9a8c&#xff1a;<br \/>\n\u786e\u4fdd\u00a0loadFactor\u3001initialCapacity\u3001concurrencyLevel\u00a0\u5408\u6cd5\u3002<\/p>\n<\/li>\n<li>\n<p>\u9650\u5236\u5e76\u53d1\u7ea7\u522b&#xff1a;<br \/>\nconcurrencyLevel\u00a0\u8d85\u8fc7\u6700\u5927\u503c\u65f6&#xff0c;\u622a\u65ad\u4e3a\u00a0MAX_SEGMENTS\u3002<\/p>\n<\/li>\n<li>\n<p>\u8ba1\u7b97 Segment \u6570\u7ec4\u5927\u5c0f&#xff1a;<br \/>\n\u627e\u5230\u5927\u4e8e\u7b49\u4e8e\u00a0concurrencyLevel\u00a0\u7684\u6700\u5c0f\u00a02 \u7684\u5e42&#xff08;ssize\u00a0&#xff09;\u3002<\/p>\n<\/li>\n<li>\n<p>\u8ba1\u7b97\u54c8\u5e0c\u504f\u79fb\u91cf&#xff1a;<br \/>\n\u901a\u8fc7\u00a0segmentShift\u00a0\u548c\u00a0segmentMask\u00a0\u5b9a\u4f4d\u00a0Segment\u3002<\/p>\n<\/li>\n<li>\n<p>\u9650\u5236\u521d\u59cb\u5bb9\u91cf&#xff1a;<br \/>\ninitialCapacity\u00a0\u8d85\u8fc7\u6700\u5927\u503c\u65f6&#xff0c;\u622a\u65ad\u4e3a\u00a0MAXIMUM_CAPACITY\u3002<\/p>\n<\/li>\n<li>\n<p>\u8ba1\u7b97 Segment \u5bb9\u91cf&#xff1a;<br \/>\n\u6bcf\u4e2a\u00a0Segment\u00a0\u7684\u5bb9\u91cf&#xff08;cap\u00a0&#xff09;\u662f\u00a02 \u7684\u5e42&#xff0c;\u81f3\u5c11\u4e3a\u00a0MIN_SEGMENT_TABLE_CAPACITY\u3002<\/p>\n<\/li>\n<li>\n<p>\u521b\u5efa\u7b2c\u4e00\u4e2a Segment&#xff1a;<br \/>\n\u521d\u59cb\u5316\u7b2c\u4e00\u4e2a\u00a0Segment&#xff0c;\u5305\u542b\u54c8\u5e0c\u8868\u548c\u6269\u5bb9\u9608\u503c\u3002<\/p>\n<\/li>\n<li>\n<p>\u521b\u5efa Segment \u6570\u7ec4&#xff1a;<br \/>\n\u521d\u59cb\u5316\u00a0Segment\u00a0\u6570\u7ec4&#xff0c;\u5927\u5c0f\u4e3a\u00a0ssize\u3002<\/p>\n<\/li>\n<li>\n<p>\u6709\u5e8f\u5199\u5165\u7b2c\u4e00\u4e2a Segment&#xff1a;<br \/>\n\u901a\u8fc7\u00a0UNSAFE\u00a0\u786e\u4fdd\u7ebf\u7a0b\u5b89\u5168&#xff0c;\u907f\u514d\u6307\u4ee4\u91cd\u6392\u3002<\/p>\n<\/li>\n<p>\u8ba1\u7b97 segmentShift \u548c segmentMask<\/p>\n<p>this.segmentShift &#061; 32 &#8211; sshift;<br \/>\nthis.segmentMask &#061; ssize &#8211; 1;<\/p>\n<ul>\n<li>\u4f5c\u7528&#xff1a;\n<ul>\n<li>segmentShift&#xff1a;\u7528\u4e8e\u8ba1\u7b97\u00a0key\u00a0\u7684\u54c8\u5e0c\u503c\u5728\u00a0Segment\u00a0\u7ef4\u5ea6\u7684\u504f\u79fb\u91cf\u3002<\/li>\n<li>segmentMask&#xff1a;\u7528\u4e8e\u8ba1\u7b97\u00a0Segment\u00a0\u6570\u7ec4\u7684\u4e0b\u6807&#xff08;\u63a9\u7801&#xff09;\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\u7ec6\u8282&#xff1a;\u7ed3\u5408\u00a0key\u00a0\u7684\u54c8\u5e0c\u503c&#xff0c;\u901a\u8fc7\u00a0(hash &gt;&gt;&gt; segmentShift) &amp; segmentMask\u00a0\u5b9a\u4f4d\u5230\u5177\u4f53\u7684\u00a0Segment\u3002<\/li>\n<\/ul>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"483\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/08\/20250814061550-689d7f16aa9bd.png\" width=\"1065\" \/><\/p>\n<h4><\/h4>\n<h4>put\u6d41\u7a0b<\/h4>\n<p>public V put(K key, V value) {<br \/>\n    \/\/ 1. \u6821\u9a8c value \u4e0d\u4e3a null&#xff08;JDK 7 \u4e0d\u5141\u8bb8 value \u4e3a null&#xff09;<br \/>\n    if (value &#061;&#061; null) {<br \/>\n        throw new NullPointerException();<br \/>\n    }<br \/>\n    \/\/ 2. \u8ba1\u7b97 key \u7684\u54c8\u5e0c\u503c<br \/>\n    int hash &#061; hash(key);<br \/>\n    \/\/ 3. \u8ba1\u7b97 key \u5bf9\u5e94\u7684 Segment \u4e0b\u6807<br \/>\n    int j &#061; (hash &gt;&gt;&gt; segmentShift) &amp; segmentMask;<br \/>\n    \/\/ 4. \u83b7\u53d6\u6216\u521b\u5efa\u5bf9\u5e94\u7684 Segment<br \/>\n    Segment&lt;K,V&gt; s;<br \/>\n    if ((s &#061; (Segment&lt;K,V&gt;)UNSAFE.getObject(<br \/>\n            segments, (j &lt;&lt; SSHIFT) &#043; SBASE)) &#061;&#061; null) {<br \/>\n        \/\/ \u82e5 Segment \u4e3a null&#xff0c;\u901a\u8fc7 ensureSegment \u5b89\u5168\u521b\u5efa<br \/>\n        s &#061; ensureSegment(j);<br \/>\n    }<br \/>\n    \/\/ 5. \u8c03\u7528 Segment \u7684 put \u65b9\u6cd5\u63d2\u5165\u6570\u636e<br \/>\n    return s.put(key, hash, value, false);<br \/>\n}<\/p>\n<li>\n<p>\u6821\u9a8c\u00a0value&#xff1a;<br \/>\nvalue\u00a0\u4e3a\u00a0null\u00a0\u65f6\u629b\u51fa\u5f02\u5e38\u3002<\/p>\n<\/li>\n<li>\n<p>\u8ba1\u7b97\u54c8\u5e0c\u503c&#xff1a;<br \/>\n\u4e8c\u6b21\u54c8\u5e0c\u4f18\u5316&#xff0c;\u51cf\u5c11\u51b2\u7a81\u3002<\/p>\n<\/li>\n<li>\n<p>\u5b9a\u4f4d\u00a0Segment&#xff1a;<br \/>\n\u901a\u8fc7\u00a0segmentShift\u00a0\u548c\u00a0segmentMask\u00a0\u8ba1\u7b97\u00a0Segment\u00a0\u4e0b\u6807\u3002<\/p>\n<\/li>\n<li>\n<p>\u83b7\u53d6\u6216\u521b\u5efa\u00a0Segment&#xff1a;<br \/>\n\u7528\u00a0UNSAFE\u00a0\u76f4\u63a5\u8bbf\u95ee\u00a0Segment\u00a0\u6570\u7ec4&#xff1b;\u82e5\u4e3a\u00a0null&#xff0c;\u901a\u8fc7\u00a0ensureSegment\u00a0\u5b89\u5168\u521b\u5efa\u3002<\/p>\n<\/li>\n<li>\n<p>\u8c03\u7528\u00a0Segment\u00a0\u7684\u00a0put&#xff1a;<br \/>\n\u8fdb\u5165\u00a0Segment\u00a0\u5185\u90e8\u7684\u9501\u903b\u8f91&#xff0c;\u63d2\u5165\u952e\u503c\u5bf9\u3002<\/p>\n<\/li>\n<p>segment\u7ee7\u627f\u4e86\u53ef\u91cd\u5165\u9501&#xff08;ReentrantLock&#xff09;&#xff0c;\u5b83\u7684put\u65b9\u6cd5\u4e3a<\/p>\n<p>final V put(K key, int hash, V value, boolean onlyIfAbsent) {<br \/>\n    \/\/ 1. \u5c1d\u8bd5\u52a0\u9501&#xff08;\u975e\u963b\u585e\u9501&#xff09;<br \/>\n    HashEntry&lt;K,V&gt; node &#061; tryLock() ? null :<br \/>\n        \/\/ \u52a0\u9501\u5931\u8d25&#xff0c;\u8fdb\u5165\u626b\u63cf\u5e76\u52a0\u9501\u6d41\u7a0b&#xff08;\u6700\u591a\u5c1d\u8bd5 64 \u6b21&#xff09;<br \/>\n        scanAndLockForPut(key, hash, value);<\/p>\n<p>    \/\/ 2. \u52a0\u9501\u6210\u529f\u540e&#xff0c;\u6267\u884c\u63d2\u5165\u903b\u8f91<br \/>\n    V oldValue;<br \/>\n    try {<br \/>\n        HashEntry&lt;K,V&gt;[] tab &#061; table; \/\/ \u5f53\u524d Segment \u7684\u54c8\u5e0c\u8868<br \/>\n        int index &#061; (tab.length &#8211; 1) &amp; hash; \/\/ \u8ba1\u7b97\u6876\u4e0b\u6807<br \/>\n        HashEntry&lt;K,V&gt; first &#061; entryAt(tab, index); \/\/ \u83b7\u53d6\u6876\u7684\u5934\u8282\u70b9<\/p>\n<p>        \/\/ 3. \u904d\u5386\u6876\u7684\u94fe\u8868&#xff0c;\u67e5\u627e\u6216\u63d2\u5165\u8282\u70b9<br \/>\n        for (HashEntry&lt;K,V&gt; e &#061; first;;) {<br \/>\n            if (e !&#061; null) { \/\/ \u6876\u4e0d\u4e3a\u7a7a<br \/>\n                K k;<br \/>\n                \/\/ 3.1 \u627e\u5230\u76f8\u540c key \u7684\u8282\u70b9&#xff0c;\u66f4\u65b0\u503c<br \/>\n                if ((k &#061; e.key) &#061;&#061; key || (e.hash &#061;&#061; hash &amp;&amp; key.equals(k))) {<br \/>\n                    oldValue &#061; e.value;<br \/>\n                    if (!onlyIfAbsent) { \/\/ \u662f\u5426\u4ec5\u5728\u4e0d\u5b58\u5728\u65f6\u63d2\u5165<br \/>\n                        e.value &#061; value;<br \/>\n                        &#043;&#043;modCount; \/\/ \u8bb0\u5f55\u7ed3\u6784\u4fee\u6539\u6b21\u6570<br \/>\n                    }<br \/>\n                    break;<br \/>\n                }<br \/>\n                e &#061; e.next; \/\/ \u904d\u5386\u4e0b\u4e00\u4e2a\u8282\u70b9<br \/>\n            } else { \/\/ \u6876\u4e3a\u7a7a&#xff0c;\u63d2\u5165\u65b0\u8282\u70b9<br \/>\n                if (node !&#061; null) { \/\/ \u590d\u7528 scanAndLockForPut \u521b\u5efa\u7684\u8282\u70b9<br \/>\n                    node.setNext(first);<br \/>\n                } else { \/\/ \u521b\u5efa\u65b0\u8282\u70b9<br \/>\n                    node &#061; new HashEntry&lt;K,V&gt;(hash, key, value, first);<br \/>\n                }<br \/>\n                int c &#061; count &#043; 1; \/\/ \u5f53\u524d Segment \u7684\u5143\u7d20\u6570\u91cf<br \/>\n                \/\/ \u68c0\u67e5\u662f\u5426\u9700\u8981\u6269\u5bb9&#xff08;\u8d85\u8fc7\u8d1f\u8f7d\u56e0\u5b50&#xff09;<br \/>\n                if (c &gt; threshold &amp;&amp; tab.length &lt; MAXIMUM_CAPACITY) {<br \/>\n                    \/\/ \u6269\u5bb9\u5e76\u8fc1\u79fb\u6570\u636e<br \/>\n                    rehash(node);<br \/>\n                } else {<br \/>\n                    \/\/ \u63d2\u5165\u65b0\u8282\u70b9\u5230\u6876\u7684\u5934\u90e8<br \/>\n                    setEntryAt(tab, index, node);<br \/>\n                }<br \/>\n                &#043;&#043;modCount;<br \/>\n                count &#061; c; \/\/ \u66f4\u65b0\u5143\u7d20\u6570\u91cf<br \/>\n                oldValue &#061; null;<br \/>\n                break;<br \/>\n            }<br \/>\n        }<br \/>\n    } finally {<br \/>\n        \/\/ 4. \u91ca\u653e\u9501<br \/>\n        unlock();<br \/>\n    }<br \/>\n    return oldValue;<br \/>\n}<\/p>\n<li>\n<p>\u5c1d\u8bd5\u52a0\u9501&#xff1a;<br \/>\n\u975e\u963b\u585e\u52a0\u9501\u5931\u8d25\u540e&#xff0c;\u8fdb\u5165\u00a0scanAndLockForPut\u00a0\u6d41\u7a0b&#xff08;\u6700\u591a\u5c1d\u8bd5 64 \u6b21 &#xff09;\u3002<\/p>\n<\/li>\n<li>\n<p>\u904d\u5386\u94fe\u8868\u67e5\u627e\u8282\u70b9&#xff1a;<br \/>\n\u627e\u5230\u76f8\u540c\u00a0key\u00a0\u5219\u66f4\u65b0\u503c&#xff0c;\u5426\u5219\u51c6\u5907\u63d2\u5165\u65b0\u8282\u70b9\u3002<\/p>\n<\/li>\n<li>\n<p>\u63d2\u5165\u65b0\u8282\u70b9&#xff1a;<br \/>\n\u6876\u4e3a\u7a7a\u65f6\u63d2\u5165\u65b0\u8282\u70b9&#xff0c;\u68c0\u67e5\u662f\u5426\u9700\u8981\u6269\u5bb9&#xff08;rehash\u00a0&#xff09;\u3002<\/p>\n<\/li>\n<li>\n<p>\u6269\u5bb9\u4e0e\u8fc1\u79fb&#xff1a;<br \/>\n\u8d85\u8fc7\u8d1f\u8f7d\u56e0\u5b50\u65f6&#xff0c;\u6269\u5bb9\u4e3a\u539f\u5bb9\u91cf\u7684 2 \u500d&#xff0c;\u62c6\u5206\u5e76\u8fc1\u79fb\u94fe\u8868\u3002<\/p>\n<\/li>\n<li>\n<p>\u91ca\u653e\u9501&#xff1a;<br \/>\n\u64cd\u4f5c\u5b8c\u6210\u540e\u91ca\u653e\u9501&#xff0c;\u4fdd\u8bc1\u7ebf\u7a0b\u5b89\u5168<\/p>\n<\/li>\n<h4>rehash\u6d41\u7a0b<\/h4>\n<p>\u627e\u5230\u94fe\u8868\u4e2d\u8fde\u7eed\u76f8\u540c\u4e0b\u6807\u7684\u6700\u957f\u5b50\u94fe\u8868&#xff08;lastRun\u00a0&#xff09;&#xff0c;\u76f4\u63a5\u8fc1\u79fb&#xff0c;\u51cf\u5c11\u8282\u70b9\u590d\u5236\u6b21\u6570&#xff0c;\u63d0\u5347\u6269\u5bb9\u6548\u7387\u3002<\/p>\n<p>private void rehash(HashEntry&lt;K,V&gt; node) {<br \/>\n    \/\/ 1. \u4fdd\u5b58\u65e7\u54c8\u5e0c\u8868\u6570\u636e<br \/>\n    HashEntry&lt;K,V&gt;[] oldTable &#061; table;<br \/>\n    int oldCapacity &#061; oldTable.length;<br \/>\n    \/\/ 2. \u8ba1\u7b97\u65b0\u5bb9\u91cf&#xff08;\u6269\u5bb9\u4e3a\u539f\u5bb9\u91cf\u7684 2 \u500d&#xff09;<br \/>\n    int newCapacity &#061; oldCapacity &lt;&lt; 1;<br \/>\n    \/\/ 3. \u66f4\u65b0\u6269\u5bb9\u9608\u503c&#xff08;\u65b0\u5bb9\u91cf * \u8d1f\u8f7d\u56e0\u5b50&#xff09;<br \/>\n    threshold &#061; (int)(newCapacity * loadFactor);<br \/>\n    \/\/ 4. \u521b\u5efa\u65b0\u54c8\u5e0c\u8868<br \/>\n    HashEntry&lt;K,V&gt;[] newTable &#061; (HashEntry&lt;K,V&gt;[]) new HashEntry[newCapacity];<br \/>\n    int sizeMask &#061; newCapacity &#8211; 1; \/\/ \u65b0\u7684\u63a9\u7801&#xff08;\u7528\u4e8e\u8ba1\u7b97\u6876\u4e0b\u6807&#xff09;<\/p>\n<p>    \/\/ 5. \u904d\u5386\u65e7\u54c8\u5e0c\u8868&#xff0c;\u8fc1\u79fb\u6570\u636e\u5230\u65b0\u54c8\u5e0c\u8868<br \/>\n    for (int i &#061; 0; i &lt; oldCapacity; &#043;&#043;i) {<br \/>\n        HashEntry&lt;K,V&gt; e &#061; oldTable[i]; \/\/ \u5f53\u524d\u6876\u7684\u5934\u8282\u70b9<br \/>\n        if (e !&#061; null) { \/\/ \u6876\u4e0d\u4e3a\u7a7a<br \/>\n            HashEntry&lt;K,V&gt; next &#061; e.next; \/\/ \u4e0b\u4e00\u4e2a\u8282\u70b9<br \/>\n            int idx &#061; e.hash &amp; sizeMask; \/\/ \u8ba1\u7b97\u65b0\u6876\u4e0b\u6807<\/p>\n<p>            \/\/ 5.1 \u6876\u4e2d\u53ea\u6709\u4e00\u4e2a\u8282\u70b9&#xff08;\u94fe\u8868\u957f\u5ea6\u4e3a 1&#xff09;<br \/>\n            if (next &#061;&#061; null) {<br \/>\n                newTable[idx] &#061; e;<br \/>\n            } else {<br \/>\n                \/\/ 5.2 \u6876\u4e2d\u6709\u591a\u4e2a\u8282\u70b9&#xff0c;\u590d\u7528\u8fde\u7eed\u76f8\u540c\u4e0b\u6807\u7684\u8282\u70b9<br \/>\n                HashEntry&lt;K,V&gt; lastRun &#061; e; \/\/ \u8bb0\u5f55\u8fde\u7eed\u76f8\u540c\u4e0b\u6807\u7684\u6700\u540e\u4e00\u4e2a\u8282\u70b9<br \/>\n                int lastIdx &#061; idx; \/\/ \u8bb0\u5f55\u5f53\u524d\u4e0b\u6807<\/p>\n<p>                \/\/ \u904d\u5386\u94fe\u8868&#xff0c;\u627e\u5230\u8fde\u7eed\u76f8\u540c\u4e0b\u6807\u7684\u6700\u957f\u5b50\u94fe\u8868<br \/>\n                for (HashEntry&lt;K,V&gt; last &#061; next; last !&#061; null; last &#061; last.next) {<br \/>\n                    int k &#061; last.hash &amp; sizeMask;<br \/>\n                    if (k !&#061; lastIdx) {<br \/>\n                        lastIdx &#061; k;<br \/>\n                        lastRun &#061; last;<br \/>\n                    }<br \/>\n                }<\/p>\n<p>                \/\/ \u5c06\u8fde\u7eed\u76f8\u540c\u4e0b\u6807\u7684\u5b50\u94fe\u8868\u76f4\u63a5\u653e\u5165\u65b0\u6876<br \/>\n                newTable[lastIdx] &#061; lastRun;<\/p>\n<p>                \/\/ 5.3 \u904d\u5386\u5269\u4f59\u8282\u70b9&#xff0c;\u62c6\u5206\u5230\u65b0\u6876<br \/>\n                for (HashEntry&lt;K,V&gt; p &#061; e; p !&#061; lastRun; p &#061; p.next) {<br \/>\n                    V v &#061; p.value;<br \/>\n                    int h &#061; p.hash;<br \/>\n                    int k &#061; h &amp; sizeMask;<br \/>\n                    \/\/ \u63d2\u5165\u5230\u65b0\u6876\u7684\u5934\u90e8&#xff08;\u5934\u63d2\u6cd5&#xff09;<br \/>\n                    HashEntry&lt;K,V&gt; n &#061; newTable[k];<br \/>\n                    newTable[k] &#061; new HashEntry&lt;K,V&gt;(h, p.key, v, n);<br \/>\n                }<br \/>\n            }<br \/>\n        }<br \/>\n    }<\/p>\n<p>    \/\/ 6. \u5904\u7406\u5f53\u524d\u63d2\u5165\u7684\u65b0\u8282\u70b9&#xff08;\u6269\u5bb9\u65f6\u88ab\u6682\u65f6\u4fdd\u5b58\u7684\u8282\u70b9&#xff09;<br \/>\n    int nodeIndex &#061; node.hash &amp; sizeMask; \/\/ \u8ba1\u7b97\u65b0\u4e0b\u6807<br \/>\n    node.setNext(newTable[nodeIndex]); \/\/ \u63d2\u5165\u5230\u65b0\u6876\u7684\u5934\u90e8<br \/>\n    newTable[nodeIndex] &#061; node; \/\/ \u66f4\u65b0\u65b0\u6876\u7684\u5934\u8282\u70b9<\/p>\n<p>    \/\/ 7. \u66ff\u6362\u4e3a\u65b0\u54c8\u5e0c\u8868<br \/>\n    table &#061; newTable;<br \/>\n}<\/p>\n<li>\n<p>\u4fdd\u5b58\u65e7\u8868\u6570\u636e&#xff1a;<br \/>\n\u8bb0\u5f55\u65e7\u54c8\u5e0c\u8868\u7684\u5f15\u7528\u548c\u5bb9\u91cf\u3002<\/p>\n<\/li>\n<li>\n<p>\u8ba1\u7b97\u65b0\u5bb9\u91cf&#xff1a;<br \/>\n\u6269\u5bb9\u4e3a\u539f\u5bb9\u91cf\u7684 2 \u500d&#xff08;oldCapacity &lt;&lt; 1\u00a0&#xff09;\u3002<\/p>\n<\/li>\n<li>\n<p>\u66f4\u65b0\u6269\u5bb9\u9608\u503c&#xff1a;<br \/>\n\u65b0\u5bb9\u91cf\u4e58\u4ee5\u8d1f\u8f7d\u56e0\u5b50&#xff0c;\u4f5c\u4e3a\u4e0b\u4e00\u6b21\u6269\u5bb9\u7684\u89e6\u53d1\u6761\u4ef6\u3002<\/p>\n<\/li>\n<li>\n<p>\u521b\u5efa\u65b0\u54c8\u5e0c\u8868&#xff1a;<br \/>\n\u521d\u59cb\u5316\u65b0\u54c8\u5e0c\u8868&#xff0c;\u5bb9\u91cf\u4e3a\u00a0newCapacity\u3002<\/p>\n<\/li>\n<li>\n<p>\u8fc1\u79fb\u65e7\u8868\u6570\u636e&#xff1a;<\/p>\n<ul>\n<li>\u904d\u5386\u65e7\u54c8\u5e0c\u8868\u7684\u6bcf\u4e2a\u6876&#xff0c;\u62c6\u5206\u94fe\u8868\u5e76\u8fc1\u79fb\u8282\u70b9\u3002<\/li>\n<li>\u590d\u7528\u8fde\u7eed\u76f8\u540c\u4e0b\u6807\u7684\u5b50\u94fe\u8868&#xff0c;\u51cf\u5c11\u8282\u70b9\u590d\u5236\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u63d2\u5165\u65b0\u8282\u70b9&#xff1a;<br \/>\n\u5c06\u6269\u5bb9\u65f6\u6682\u65f6\u4fdd\u5b58\u7684\u65b0\u8282\u70b9\u63d2\u5165\u5230\u65b0\u54c8\u5e0c\u8868\u3002<\/p>\n<\/li>\n<li>\n<p>\u66ff\u6362\u54c8\u5e0c\u8868&#xff1a;<br \/>\n\u5c06\u00a0Segment\u00a0\u7684\u54c8\u5e0c\u8868\u66ff\u6362\u4e3a\u65b0\u54c8\u5e0c\u8868&#xff0c;\u5b8c\u6210\u6269\u5bb9\u3002<\/p>\n<\/li>\n<h4>get\u6d41\u7a0b<\/h4>\n<p>\u6bcf\u4e2a\u00a0Segment\u00a0\u72ec\u7acb\u5b58\u50a8\u6570\u636e&#xff0c;get\u00a0\u65f6\u53ea\u9700\u8bbf\u95ee\u5bf9\u5e94\u00a0Segment&#xff0c;\u65e0\u9700\u52a0\u9501\u3002<\/p>\n<p>\u901a\u8fc7\u00a0UNSAFE.getObjectVolatile\u00a0\u4fdd\u8bc1\u00a0Segment\u00a0\u548c\u00a0HashEntry\u00a0\u7684\u53ef\u89c1\u6027&#xff0c;\u907f\u514d\u810f\u8bfb\u3002<\/p>\n<ul>\n<li>\u6269\u5bb9\u8fc7\u7a0b\u4e2d&#xff1a;\n<ul>\n<li>\u82e5\u00a0get\u00a0\u5148\u53d1\u751f&#xff08;\u76f8\u5bf9\u4e8e\u6269\u5bb9\u64cd\u4f5c &#xff09;&#xff0c;\u4ece\u65e7\u8868\u53d6\u5185\u5bb9<\/li>\n<li>\u82e5\u00a0get\u00a0\u540e\u53d1\u751f&#xff08;\u76f8\u5bf9\u4e8e\u6269\u5bb9\u64cd\u4f5c &#xff09;&#xff0c;\u4ece\u65b0\u8868\u53d6\u5185\u5bb9<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>public V get(Object key) {<br \/>\n    \/\/ 1. \u8ba1\u7b97 key \u7684\u54c8\u5e0c\u503c<br \/>\n    int h &#061; hash(key);<br \/>\n    \/\/ 2. \u8ba1\u7b97 key \u5bf9\u5e94\u7684 Segment \u504f\u79fb\u91cf<br \/>\n    long u &#061; (((h &gt;&gt;&gt; segmentShift) &amp; segmentMask) &lt;&lt; SSHIFT) &#043; SBASE;<br \/>\n    \/\/ 3. \u83b7\u53d6\u5bf9\u5e94\u7684 Segment<br \/>\n    Segment&lt;K,V&gt; s &#061; (Segment&lt;K,V&gt;) UNSAFE.getObjectVolatile(segments, u);<br \/>\n    HashEntry&lt;K,V&gt;[] tab;<br \/>\n    \/\/ 4. \u68c0\u67e5 Segment \u548c\u54c8\u5e0c\u8868\u662f\u5426\u4e3a\u7a7a<br \/>\n    if (s !&#061; null &amp;&amp; (tab &#061; s.table) !&#061; null) {<br \/>\n        \/\/ 5. \u8ba1\u7b97\u6876\u4e0b\u6807&#xff0c;\u5e76\u83b7\u53d6\u6876\u7684\u5934\u8282\u70b9<br \/>\n        long t &#061; (((long)(tab.length &#8211; 1) &amp; h) &lt;&lt; TSHIFT) &#043; TBASE;<br \/>\n        for (HashEntry&lt;K,V&gt; e &#061; (HashEntry&lt;K,V&gt;) UNSAFE.getObjectVolatile(tab, t);<br \/>\n             e !&#061; null;<br \/>\n             e &#061; e.next) { \/\/ \u904d\u5386\u94fe\u8868<br \/>\n            K k;<br \/>\n            \/\/ 6. \u627e\u5230\u76f8\u540c key \u7684\u8282\u70b9&#xff0c;\u8fd4\u56de\u5bf9\u5e94\u503c<br \/>\n            if ((k &#061; e.key) &#061;&#061; key || (e.hash &#061;&#061; h &amp;&amp; key.equals(k))) {<br \/>\n                return e.value;<br \/>\n            }<br \/>\n        }<br \/>\n    }<br \/>\n    return null; \/\/ \u672a\u627e\u5230\u5bf9\u5e94 key&#xff0c;\u8fd4\u56de null<br \/>\n}<\/p>\n<li>\n<p>\u8ba1\u7b97\u54c8\u5e0c\u503c&#xff1a;<br \/>\n\u4e8c\u6b21\u54c8\u5e0c\u4f18\u5316&#xff0c;\u51cf\u5c11\u54c8\u5e0c\u51b2\u7a81\u3002<\/p>\n<\/li>\n<li>\n<p>\u5b9a\u4f4d Segment&#xff1a;<br \/>\n\u901a\u8fc7\u54c8\u5e0c\u503c\u548c\u00a0Segment\u00a0\u6570\u7ec4\u53c2\u6570&#xff0c;\u8ba1\u7b97\u5185\u5b58\u504f\u79fb\u91cf&#xff0c;\u83b7\u53d6\u5bf9\u5e94\u00a0Segment\u3002<\/p>\n<\/li>\n<li>\n<p>\u68c0\u67e5\u975e\u7a7a&#xff1a;<br \/>\n\u786e\u4fdd\u00a0Segment\u00a0\u548c\u54c8\u5e0c\u8868\u5df2\u521d\u59cb\u5316&#xff0c;\u907f\u514d\u7a7a\u6307\u9488\u3002<\/p>\n<\/li>\n<li>\n<p>\u5b9a\u4f4d\u6876\u5e76\u904d\u5386\u94fe\u8868&#xff1a;<br \/>\n\u8ba1\u7b97\u6876\u4e0b\u6807&#xff0c;\u83b7\u53d6\u5934\u8282\u70b9&#xff0c;\u904d\u5386\u94fe\u8868\u67e5\u627e\u5339\u914d\u7684\u00a0key\u3002<\/p>\n<\/li>\n<li>\n<p>\u5339\u914d\u8282\u70b9\u5e76\u8fd4\u56de\u503c&#xff1a;<br \/>\n\u627e\u5230\u00a0key\u00a0\u548c\u00a0hash\u00a0\u90fd\u5339\u914d\u7684\u8282\u70b9&#xff0c;\u8fd4\u56de\u5bf9\u5e94\u503c&#xff1b;\u5426\u5219\u8fd4\u56de\u00a0null\u3002<\/p>\n<\/li>\n<h4>size\u8ba1\u7b97\u6d41\u7a0b<\/h4>\n<ul>\n<li>\u521d\u6b65\u8ba1\u7b97&#xff1a;\u8ba1\u7b97\u5143\u7d20\u4e2a\u6570\u524d&#xff0c;\u5148\u4e0d\u52a0\u9501\u8ba1\u7b97\u4e24\u6b21&#xff0c;\u82e5\u524d\u540e\u7ed3\u679c\u4e00\u81f4&#xff0c;\u8ba4\u4e3a\u4e2a\u6570\u6b63\u786e\u5e76\u8fd4\u56de<\/li>\n<li>\u91cd\u8bd5\u4e0e\u52a0\u9501&#xff1a;\u82e5\u4e24\u6b21\u7ed3\u679c\u4e0d\u4e00\u81f4&#xff0c;\u8fdb\u884c\u91cd\u8bd5&#xff1b;\u91cd\u8bd5\u6b21\u6570\u8d85 3 \u6b21\u65f6&#xff0c;\u9501\u4f4f\u6240\u6709\u00a0segment&#xff0c;\u91cd\u65b0\u8ba1\u7b97\u4e2a\u6570\u540e\u8fd4\u56de<\/li>\n<li>\u5148\u5c1d\u8bd5\u65e0\u9501\u8ba1\u7b97&#xff0c;\u5229\u7528\u00a0modCount\u00a0\u68c0\u6d4b\u5e76\u53d1\u4fee\u6539\u3002\u82e5\u4e24\u6b21\u8ba1\u7b97\u7684\u00a0modCount\u00a0\u4e4b\u548c\u4e00\u81f4&#xff0c;\u8ba4\u4e3a\u7ed3\u679c\u7a33\u5b9a&#xff0c;\u907f\u514d\u52a0\u9501\u5f00\u9500\u3002<\/li>\n<\/ul>\n<p>\/\/ \u91cd\u8bd5\u6b21\u6570\u9608\u503c&#xff1a;\u8d85\u8fc7\u8be5\u6b21\u6570\u5219\u52a0\u9501\u6240\u6709 Segment<br \/>\nstatic final int RETRIES_BEFORE_LOCK &#061; 2; <\/p>\n<p>public int size() {<br \/>\n    \/\/ 1. \u521d\u59cb\u5316\u53d8\u91cf<br \/>\n    final Segment&lt;K,V&gt;[] segments &#061; this.segments;<br \/>\n    int size;           \/\/ \u6700\u7ec8\u8fd4\u56de\u7684\u5143\u7d20\u6570\u91cf<br \/>\n    boolean overflow;   \/\/ \u662f\u5426\u6ea2\u51fa 32 \u4f4d\u6574\u6570<br \/>\n    long sum;           \/\/ \u6240\u6709 Segment \u7684 modCount \u4e4b\u548c<br \/>\n    long last &#061; 0L;     \/\/ \u4e0a\u4e00\u6b21\u8ba1\u7b97\u7684 sum<br \/>\n    int retries &#061; -1;   \/\/ \u91cd\u8bd5\u6b21\u6570&#xff08;\u521d\u59cb\u4e3a -1&#xff0c;\u7b2c\u4e00\u6b21\u5faa\u73af\u4e0d\u89c6\u4e3a\u91cd\u8bd5&#xff09;<\/p>\n<p>    try {<br \/>\n        for (;;) { \/\/ \u65e0\u9650\u5faa\u73af&#xff0c;\u76f4\u5230\u83b7\u53d6\u7a33\u5b9a\u7ed3\u679c<br \/>\n            \/\/ 2. \u68c0\u67e5\u662f\u5426\u9700\u8981\u52a0\u9501\u6240\u6709 Segment<br \/>\n            if (retries&#043;&#043; &#061;&#061; RETRIES_BEFORE_LOCK) {<br \/>\n                \/\/ \u904d\u5386\u6240\u6709 Segment&#xff0c;\u52a0\u9501&#xff08;\u786e\u4fdd Segment \u5df2\u521b\u5efa&#xff09;<br \/>\n                for (int j &#061; 0; j &lt; segments.length; &#043;&#043;j) {<br \/>\n                    ensureSegment(j).lock(); \/\/ \u52a0\u9501&#xff0c;force creation<br \/>\n                }<br \/>\n            }<\/p>\n<p>            \/\/ 3. \u91cd\u7f6e\u8ba1\u7b97\u53c2\u6570<br \/>\n            sum &#061; 0L;<br \/>\n            size &#061; 0;<br \/>\n            overflow &#061; false;<\/p>\n<p>            \/\/ 4. \u904d\u5386\u6240\u6709 Segment&#xff0c;\u7d2f\u52a0\u5143\u7d20\u6570\u91cf<br \/>\n            for (int j &#061; 0; j &lt; segments.length; &#043;&#043;j) {<br \/>\n                Segment&lt;K,V&gt; seg &#061; segmentAt(segments, j);<br \/>\n                if (seg !&#061; null) {<br \/>\n                    sum &#043;&#061; seg.modCount; \/\/ \u7d2f\u52a0 modCount&#xff08;\u8bb0\u5f55\u7ed3\u6784\u53d8\u5316&#xff09;<br \/>\n                    int c &#061; seg.count;   \/\/ \u5f53\u524d Segment \u7684\u5143\u7d20\u6570\u91cf<br \/>\n                    if (c &lt; 0 || (size &#043;&#061; c) &lt; 0) {<br \/>\n                        overflow &#061; true; \/\/ \u6ea2\u51fa\u5224\u65ad<br \/>\n                    }<br \/>\n                }<br \/>\n            }<\/p>\n<p>            \/\/ 5. \u68c0\u67e5\u4e24\u6b21\u8ba1\u7b97\u7ed3\u679c\u662f\u5426\u4e00\u81f4<br \/>\n            if (sum &#061;&#061; last) { \/\/ \u82e5 sum \u4e0e\u4e0a\u4e00\u6b21\u76f8\u540c&#xff0c;\u8bf4\u660e\u65e0\u7ed3\u6784\u53d8\u5316<br \/>\n                break;<br \/>\n            }<br \/>\n            last &#061; sum; \/\/ \u8bb0\u5f55\u5f53\u524d sum&#xff0c;\u7528\u4e8e\u4e0b\u6b21\u5bf9\u6bd4<br \/>\n        }<br \/>\n    } finally {<br \/>\n        \/\/ 6. \u91ca\u653e\u6240\u6709 Segment \u7684\u9501&#xff08;\u4ec5\u5f53\u52a0\u9501\u8fc7\u624d\u91ca\u653e&#xff09;<br \/>\n        if (retries &gt; RETRIES_BEFORE_LOCK) {<br \/>\n            for (int j &#061; 0; j &lt; segments.length; &#043;&#043;j) {<br \/>\n                segmentAt(segments, j).unlock();<br \/>\n            }<br \/>\n        }<br \/>\n    }<\/p>\n<p>    \/\/ 7. \u5904\u7406\u6ea2\u51fa\u6216\u8fd4\u56de\u7ed3\u679c<br \/>\n    return overflow ? Integer.MAX_VALUE : size;<br \/>\n}<\/p>\n<p>\/\/ \u8f85\u52a9\u65b9\u6cd5&#xff1a;\u83b7\u53d6\u6307\u5b9a\u4f4d\u7f6e\u7684 Segment&#xff08;\u786e\u4fdd\u53ef\u89c1\u6027&#xff09;<br \/>\nprivate Segment&lt;K,V&gt; segmentAt(Segment&lt;K,V&gt;[] segments, int j) {<br \/>\n    return (Segment&lt;K,V&gt;) UNSAFE.getObjectVolatile(segments,<br \/>\n        ((long)j &lt;&lt; SSHIFT) &#043; SBASE);<br \/>\n}<\/p>\n<p>\/\/ \u8f85\u52a9\u65b9\u6cd5&#xff1a;\u786e\u4fdd Segment \u5df2\u521b\u5efa&#xff08;\u5ef6\u8fdf\u521d\u59cb\u5316&#xff09;<br \/>\nprivate Segment&lt;K,V&gt; ensureSegment(int j) {<br \/>\n    \/\/ &#8230; \u903b\u8f91\u4e0e\u4e4b\u524d\u89e3\u6790\u4e00\u81f4&#xff0c;\u786e\u4fdd Segment \u521b\u5efa &#8230;<br \/>\n}<\/p>\n<li>\n<p>\u521d\u59cb\u5316\u53d8\u91cf&#xff1a;<br \/>\n\u5b9a\u4e49\u7ed3\u679c\u3001\u6ea2\u51fa\u6807\u8bb0\u3001modCount\u00a0\u7d2f\u52a0\u548c\u7b49\u53d8\u91cf\u3002<\/p>\n<\/li>\n<li>\n<p>\u68c0\u67e5\u52a0\u9501\u6761\u4ef6&#xff1a;<br \/>\n\u91cd\u8bd5\u6b21\u6570\u8d85\u8fc7\u9608\u503c\u65f6&#xff0c;\u52a0\u9501\u6240\u6709\u00a0Segment&#xff0c;\u786e\u4fdd\u7ed3\u679c\u51c6\u786e\u3002<\/p>\n<\/li>\n<li>\n<p>\u91cd\u7f6e\u8ba1\u7b97\u53c2\u6570&#xff1a;<br \/>\n\u6bcf\u6b21\u8ba1\u7b97\u524d\u6e05\u7a7a\u4e34\u65f6\u53d8\u91cf&#xff0c;\u907f\u514d\u5e72\u6270\u3002<\/p>\n<\/li>\n<li>\n<p>\u904d\u5386\u7d2f\u52a0\u6570\u91cf&#xff1a;<br \/>\n\u904d\u5386\u6240\u6709\u00a0Segment&#xff0c;\u7d2f\u52a0\u00a0modCount\u00a0\u548c\u00a0count&#xff0c;\u68c0\u6d4b\u6ea2\u51fa\u3002<\/p>\n<\/li>\n<li>\n<p>\u68c0\u67e5\u7ed3\u679c\u7a33\u5b9a\u6027&#xff1a;<br \/>\n\u82e5\u00a0modCount\u00a0\u4e4b\u548c\u7a33\u5b9a&#xff08;\u4e24\u6b21\u8ba1\u7b97\u76f8\u540c &#xff09;&#xff0c;\u8ba4\u4e3a\u7ed3\u679c\u51c6\u786e&#xff0c;\u8df3\u51fa\u5faa\u73af\u3002<\/p>\n<\/li>\n<li>\n<p>\u91ca\u653e\u9501&#xff1a;<br \/>\n\u52a0\u9501\u540e\u9700\u91ca\u653e&#xff0c;\u907f\u514d\u6027\u80fd\u635f\u8017\u3002<\/p>\n<\/li>\n<li>\n<p>\u8fd4\u56de\u7ed3\u679c&#xff1a;<br \/>\n\u6839\u636e\u6ea2\u51fa\u60c5\u51b5\u8fd4\u56de\u6700\u7ec8\u7ed3\u679c\u3002<\/p>\n<\/li>\n<\/p>\n<h3 style=\"background-color:transparent\">\u603b\u7ed3<\/h3>\n<table>\n<tr>JDK 7 ConcurrentHashMapJDK 8 ConcurrentHashMap<\/tr>\n<tbody>\n<tr>\n<td>\u5e95\u5c42\u6570\u636e\u7ed3\u6784<\/td>\n<td>\u7531Segment\u6570\u7ec4\u548cHashEntry\u6570\u7ec4\u7ec4\u6210&#xff0c;\u6bcf\u4e2aSegment\u7c7b\u4f3c\u4e8e\u4e00\u4e2a\u5c0f\u578b\u7684HashMap&#xff0c;Segment\u7ee7\u627f\u81eaReentrantLock&#xff0c;\u662f\u53ef\u91cd\u5165\u9501&#xff0c;HashEntry\u7ec4\u6210\u94fe\u8868\u6765\u5904\u7406\u54c8\u5e0c\u51b2\u7a81<\/td>\n<td>\u7531Node\u6570\u7ec4\u3001\u94fe\u8868\u548c\u7ea2\u9ed1\u6811\u7ec4\u6210&#xff0c;\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u662f\u4e00\u4e2aNode&#xff0c;\u5982\u679c\u94fe\u8868\u957f\u5ea6\u8d85\u8fc7\u9608\u503c&#xff08;\u9ed8\u8ba4 8&#xff09;&#xff0c;\u4f1a\u5c06\u94fe\u8868\u8f6c\u6362\u4e3a\u7ea2\u9ed1\u6811\u4ee5\u63d0\u5347\u67e5\u8be2\u6548\u7387<\/td>\n<\/tr>\n<tr>\n<td>\u9501\u673a\u5236<\/td>\n<td>\u5206\u6bb5\u9501&#xff0c;\u6bcf\u4e2aSegment\u5bf9\u5e94\u4e00\u628a\u9501&#xff0c;\u591a\u7ebf\u7a0b\u8bbf\u95ee\u4e0d\u540cSegment\u65f6\u4e0d\u4f1a\u4ea7\u751f\u51b2\u7a81&#xff0c;\u63d0\u9ad8\u4e86\u5e76\u53d1\u5ea6&#xff0c;\u4f46\u9501\u7c92\u5ea6\u76f8\u5bf9\u8f83\u7c97\u3002\u5f53\u8fdb\u884c\u5199\u64cd\u4f5c\u65f6&#xff0c;\u9700\u8981\u9501\u5b9a\u5bf9\u5e94\u7684Segment<\/td>\n<td>\u9501\u7c92\u5ea6\u66f4\u7ec6&#xff0c;\u5f15\u5165\u4e86CAS\u64cd\u4f5c\u548cSynchronized\u5173\u952e\u5b57\u3002\u5728\u94fe\u8868\u8282\u70b9\u5c42\u9762&#xff0c;\u5bf9\u4e8e\u63d2\u5165\u3001\u66f4\u65b0\u7b49\u64cd\u4f5c&#xff0c;\u5148\u5c1d\u8bd5\u4f7f\u7528CAS\u64cd\u4f5c&#xff0c;\u5931\u8d25\u540e\u518d\u4f7f\u7528synchronized\u540c\u6b65\u4ee3\u7801\u5757\u9501\u5b9a\u5355\u4e2a\u94fe\u8868\u8282\u70b9\u6216\u7ea2\u9ed1\u6811<\/td>\n<\/tr>\n<tr>\n<td>\u521d\u59cb\u5316\u65b9\u5f0f<\/td>\n<td>\u4e0d\u662f\u61d2\u60f0\u521d\u59cb\u5316&#xff0c;\u5728\u521b\u5efaConcurrentHashMap\u65f6&#xff0c;\u4f1a\u6839\u636e\u6307\u5b9a\u7684concurrencyLevel\u521d\u59cb\u5316Segment\u6570\u7ec4&#xff0c;\u6bcf\u4e2aSegment\u5185\u90e8\u7684HashEntry\u6570\u7ec4\u4e5f\u4f1a\u8fdb\u884c\u4e00\u5b9a\u7684\u521d\u59cb\u5316<\/td>\n<td>\u91c7\u7528\u61d2\u60f0\u521d\u59cb\u5316&#xff0c;\u5728\u9996\u6b21\u63d2\u5165\u5143\u7d20\u65f6\u624d\u4f1a\u521d\u59cb\u5316\u5e95\u5c42\u7684\u6570\u7ec4<\/td>\n<\/tr>\n<tr>\n<td>\u6269\u5bb9\u673a\u5236<\/td>\n<td>\u5f53\u67d0\u4e2aSegment\u4e2d\u7684\u5143\u7d20\u6570\u91cf\u8d85\u8fc7\u5176\u8d1f\u8f7d\u56e0\u5b50\u5bf9\u5e94\u7684\u9608\u503c\u65f6&#xff0c;\u8be5Segment\u8fdb\u884c\u6269\u5bb9&#xff0c;\u6269\u5bb9\u65f6\u4f1a\u521b\u5efa\u4e00\u4e2a\u65b0\u7684HashEntry\u6570\u7ec4&#xff0c;\u7136\u540e\u5c06\u65e7\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u91cd\u65b0\u54c8\u5e0c\u5e76\u8fc1\u79fb\u5230\u65b0\u6570\u7ec4\u4e2d&#xff0c;\u91c7\u7528\u5934\u63d2\u6cd5&#xff0c;\u5728\u591a\u7ebf\u7a0b\u73af\u5883\u4e0b\u53ef\u80fd\u4f1a\u51fa\u73b0\u94fe\u8868\u6210\u73af\u95ee\u9898<\/td>\n<td>\u5f53\u6574\u4e2aConcurrentHashMap\u7684\u5143\u7d20\u6570\u91cf\u8d85\u8fc7\u8d1f\u8f7d\u56e0\u5b50\u5bf9\u5e94\u7684\u9608\u503c\u65f6&#xff0c;\u4f1a\u89e6\u53d1\u6269\u5bb9\u3002\u6269\u5bb9\u65f6\u91c7\u7528\u590d\u5236\u8fc1\u79fb\u7684\u65b9\u5f0f&#xff0c;\u5c06\u65e7\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u590d\u5236\u5230\u65b0\u6570\u7ec4\u4e2d&#xff0c;\u5e76\u4e14\u91c7\u7528\u5c3e\u63d2\u6cd5&#xff0c;\u907f\u514d\u4e86\u94fe\u8868\u6210\u73af\u95ee\u9898\u3002\u540c\u65f6&#xff0c;\u6269\u5bb9\u652f\u6301\u591a\u7ebf\u7a0b\u534f\u4f5c&#xff0c;\u901a\u8fc7ForwardingNode\u6807\u8bb0\u8282\u70b9&#xff0c;\u8ba9\u5176\u4ed6\u7ebf\u7a0b\u77e5\u9053\u8be5\u8282\u70b9\u6b63\u5728\u8fc1\u79fb<\/td>\n<\/tr>\n<tr>\n<td>\u8bfb\u64cd\u4f5c<\/td>\n<td>\u672a\u52a0\u9501&#xff0c;\u901a\u8fc7UNSAFE\u65b9\u6cd5\u4fdd\u8bc1\u53ef\u89c1\u6027\u3002\u5728\u6269\u5bb9\u8fc7\u7a0b\u4e2d&#xff0c;\u5982\u679cget\u64cd\u4f5c\u5148\u4e8e\u6269\u5bb9\u53d1\u751f&#xff0c;\u5219\u4ece\u65e7\u8868\u53d6\u5185\u5bb9&#xff1b;\u5982\u679cget\u64cd\u4f5c\u540e\u4e8e\u6269\u5bb9\u53d1\u751f&#xff0c;\u5219\u4ece\u65b0\u8868\u53d6\u5185\u5bb9<\/td>\n<td>\u8bfb\u64cd\u4f5c\u5927\u90e8\u5206\u60c5\u51b5\u4e0b\u65e0\u9501&#xff0c;\u901a\u8fc7volatile\u5173\u952e\u5b57\u4fdd\u8bc1\u53ef\u89c1\u6027\u3002\u5728\u94fe\u8868\u4e0a\u67e5\u627e\u65f6\u76f4\u63a5\u904d\u5386&#xff0c;\u5728\u7ea2\u9ed1\u6811\u4e0a\u67e5\u627e\u65f6\u6309\u7167\u7ea2\u9ed1\u6811\u7684\u67e5\u627e\u7b97\u6cd5\u8fdb\u884c<\/td>\n<\/tr>\n<tr>\n<td>\u5199\u64cd\u4f5c<\/td>\n<td>\u5bf9\u9700\u8981\u64cd\u4f5c\u7684Segment\u52a0\u9501&#xff0c;\u7136\u540e\u8fdb\u884c\u63d2\u5165\u3001\u66f4\u65b0\u7b49\u64cd\u4f5c\u3002\u5982\u679c\u5728\u4e00\u4e2aSegment\u5185\u94fe\u8868\u957f\u5ea6\u8fc7\u957f&#xff0c;\u4e0d\u4f1a\u8f6c\u6362\u4e3a\u7ea2\u9ed1\u6811<\/td>\n<td>\u5148\u901a\u8fc7\u54c8\u5e0c\u503c\u786e\u5b9a\u8981\u64cd\u4f5c\u7684\u6570\u7ec4\u7d22\u5f15\u4f4d\u7f6e&#xff0c;\u82e5\u8be5\u4f4d\u7f6e\u4e3a\u7a7a&#xff0c;\u4f7f\u7528CAS\u64cd\u4f5c\u63d2\u5165\u65b0\u8282\u70b9&#xff1b;\u82e5\u4e0d\u4e3a\u7a7a&#xff0c;\u5224\u65ad\u662f\u94fe\u8868\u8fd8\u662f\u7ea2\u9ed1\u6811&#xff0c;\u7136\u540e\u91c7\u7528\u4e0d\u540c\u7684\u65b9\u5f0f\u63d2\u5165&#xff0c;\u5f53\u94fe\u8868\u957f\u5ea6\u8fbe\u5230\u9608\u503c&#xff08;\u9ed8\u8ba4 8&#xff09;\u4e14\u6570\u7ec4\u957f\u5ea6\u5927\u4e8e 64 \u65f6&#xff0c;\u4f1a\u5c06\u94fe\u8868\u8f6c\u6362\u4e3a\u7ea2\u9ed1\u6811<\/td>\n<\/tr>\n<tr>\n<td>\u8ba1\u7b97\u5143\u7d20\u4e2a\u6570&#xff08;size&#xff09;<\/td>\n<td>\u5148\u4e0d\u52a0\u9501\u8ba1\u7b97\u4e24\u6b21modCount\u4e4b\u548c\u4ee5\u53ca\u6bcf\u4e2aSegment\u7684\u5143\u7d20\u6570\u91cf\u603b\u548c&#xff0c;\u5982\u679c\u524d\u540e\u4e24\u6b21\u8ba1\u7b97\u7ed3\u679c\u4e00\u6837&#xff0c;\u8ba4\u4e3a\u4e2a\u6570\u6b63\u786e\u8fd4\u56de&#xff1b;\u5982\u679c\u4e0d\u4e00\u6837&#xff0c;\u8fdb\u884c\u91cd\u8bd5&#xff0c;\u91cd\u8bd5\u6b21\u6570\u8d85\u8fc7 3 \u6b21&#xff0c;\u5c06\u6240\u6709Segment\u9501\u4f4f&#xff0c;\u91cd\u65b0\u8ba1\u7b97\u4e2a\u6570\u8fd4\u56de<\/td>\n<td>\u901a\u8fc7\u7d2f\u52a0baseCount\u548ccounterCells\u6570\u7ec4\u4e2d\u6240\u6709CounterCell\u7684value\u503c\u6765\u8ba1\u7b97\u5143\u7d20\u4e2a\u6570\u3002\u5728\u591a\u7ebf\u7a0b\u5e76\u53d1\u4fee\u6539\u7684\u60c5\u51b5\u4e0b&#xff0c;\u7531\u4e8ecounterCells\u7684\u5b58\u5728&#xff0c;\u80fd\u8f83\u597d\u5730\u5904\u7406\u5e76\u53d1\u8ba1\u6570\u95ee\u9898<\/td>\n<\/tr>\n<tr>\n<td>\u6027\u80fd\u7279\u70b9<\/td>\n<td>\u5728\u591a\u7ebf\u7a0b\u8bfb\u591a\u5199\u5c11\u7684\u573a\u666f\u4e0b&#xff0c;\u7531\u4e8e\u5206\u6bb5\u9501\u673a\u5236&#xff0c;\u5e76\u53d1\u6027\u80fd\u8f83\u597d&#xff0c;\u4f46\u5199\u64cd\u4f5c\u65f6\u9501\u7c92\u5ea6\u76f8\u5bf9\u8f83\u7c97&#xff0c;\u53ef\u80fd\u5b58\u5728\u7ade\u4e89\u3002\u521d\u59cb\u5316\u65f6\u4f1a\u5360\u7528\u4e00\u5b9a\u5185\u5b58<\/td>\n<td>\u5728\u591a\u7ebf\u7a0b\u8bfb\u5199\u573a\u666f\u4e0b\u6027\u80fd\u90fd\u6709\u8f83\u5927\u63d0\u5347&#xff0c;\u9501\u7c92\u5ea6\u66f4\u7ec6&#xff0c;CAS\u548cSynchronized\u7ed3\u5408\u51cf\u5c11\u4e86\u9501\u7ade\u4e89&#xff0c;\u5e76\u4e14\u91c7\u7528\u4e86\u7ea2\u9ed1\u6811\u4f18\u5316\u67e5\u8be2&#xff0c;\u5185\u5b58\u4f7f\u7528\u66f4\u9ad8\u6548&#xff0c;\u521d\u59cb\u5316\u65f6\u6309\u9700\u5206\u914d\u5185\u5b58<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<table>\n<tr>\n<table>\n<tr>Java 7 ConcurrentHashMapJava 8 ConcurrentHashMap\u4f18\u52bf\u8bf4\u660e<\/tr>\n<tbody>\n<tr>\n<td>\u9501\u673a\u5236<\/td>\n<td>\u6bb5\u7ea7\u522b\u7684\u9501&#xff0c;\u6bcf\u4e2aSegment\u5bf9\u5e94\u4e00\u628a\u9501<\/td>\n<td>\u66f4\u7ec6\u7c92\u5ea6\u7684\u9501&#xff08;Synchronized\u00a0&#xff09;\u7ed3\u5408\u65e0\u9501\u64cd\u4f5c&#xff08;CAS\u00a0&#xff09;<\/td>\n<td>Java 8 \u51cf\u5c11\u4e86\u9501\u7ade\u4e89&#xff0c;\u5141\u8bb8\u66f4\u591a\u7ebf\u7a0b\u540c\u65f6\u8bbf\u95ee&#xff0c;\u63d0\u9ad8\u4e86\u5e76\u53d1\u6027\u3002\u4f8b\u5982&#xff0c;\u5728\u9ad8\u5e76\u53d1\u5199\u5165\u573a\u666f\u4e0b&#xff0c;Java 7 \u53ef\u80fd\u56e0\u9501\u51b2\u7a81\u5bfc\u81f4\u7ebf\u7a0b\u963b\u585e&#xff0c;Java 8 \u80fd\u8ba9\u66f4\u591a\u7ebf\u7a0b\u5e76\u884c\u6267\u884c\u5199\u5165\u64cd\u4f5c<\/td>\n<\/tr>\n<tr>\n<td>get\u64cd\u4f5c<\/td>\n<td>\u672a\u52a0\u9501&#xff0c;\u901a\u8fc7UNSAFE\u65b9\u6cd5\u4fdd\u8bc1\u53ef\u89c1\u6027<\/td>\n<td>\u65e0\u9501\u64cd\u4f5c&#xff0c;\u5229\u7528volatile\u5173\u952e\u5b57\u4fdd\u8bc1\u53ef\u89c1\u6027<\/td>\n<td>Java 8 \u7684get\u64cd\u4f5c\u5728\u4fdd\u8bc1\u53ef\u89c1\u6027\u7684\u540c\u65f6&#xff0c;\u51cf\u5c11\u4e86UNSAFE\u5e26\u6765\u7684\u6f5c\u5728\u98ce\u9669\u548c\u590d\u6742\u6027&#xff0c;\u6027\u80fd\u66f4\u4f18\u3002\u5e76\u4e14volatile\u8bed\u4e49\u66f4\u7b26\u5408 Java \u5185\u5b58\u6a21\u578b\u89c4\u8303&#xff0c;\u53ef\u8bfb\u6027\u548c\u7ef4\u62a4\u6027\u66f4\u597d<\/td>\n<\/tr>\n<tr>\n<td>put\u64cd\u4f5c<\/td>\n<td>\u5bf9Segment\u52a0\u9501&#xff0c;\u91c7\u7528\u5934\u63d2\u6cd5<\/td>\n<td>\u4f7f\u7528CAS\u00a0\u4f18\u5148\u5c1d\u8bd5\u63d2\u5165&#xff0c;\u5931\u8d25\u540e\u4f7f\u7528\u7ec6\u7c92\u5ea6\u7684synchronized\u9501&#xff0c;\u91c7\u7528\u5c3e\u63d2\u6cd5<\/td>\n<td>Java 8 \u7684put\u64cd\u4f5c\u51cf\u5c11\u4e86\u9501\u7684\u4f7f\u7528\u9891\u7387&#xff0c;CAS\u64cd\u4f5c\u907f\u514d\u4e86\u4e0d\u5fc5\u8981\u7684\u9501\u7ade\u4e89&#xff0c;\u5c3e\u63d2\u6cd5\u907f\u514d\u4e86\u591a\u7ebf\u7a0b\u6269\u5bb9\u65f6\u94fe\u8868\u6210\u73af\u95ee\u9898&#xff0c;\u63d0\u9ad8\u4e86\u63d2\u5165\u548c\u66f4\u65b0\u7684\u6027\u80fd\u548c\u7a33\u5b9a\u6027<\/td>\n<\/tr>\n<tr>\n<td>\u6269\u5bb9\u673a\u5236<\/td>\n<td>\u67d0\u4e2aSegment\u5143\u7d20\u6570\u91cf\u8d85\u9608\u503c\u65f6&#xff0c;\u8be5Segment\u5355\u72ec\u6269\u5bb9&#xff0c;\u91c7\u7528\u5934\u63d2\u6cd5\u8fc1\u79fb\u6570\u636e<\/td>\n<td>\u6574\u4e2aConcurrentHashMap\u5143\u7d20\u6570\u91cf\u8d85\u9608\u503c\u65f6\u89e6\u53d1\u6269\u5bb9&#xff0c;\u591a\u7ebf\u7a0b\u534f\u4f5c\u6269\u5bb9&#xff0c;\u9010\u6b65\u8fc1\u79fb\u8282\u70b9&#xff0c;\u91c7\u7528\u5c3e\u63d2\u6cd5<\/td>\n<td>Java 8 \u7684\u6269\u5bb9\u673a\u5236\u51cf\u5c11\u4e86\u6269\u5bb9\u5bf9\u6574\u4f53\u6027\u80fd\u7684\u5f71\u54cd&#xff0c;\u591a\u7ebf\u7a0b\u534f\u4f5c\u52a0\u901f\u4e86\u6269\u5bb9\u8fc7\u7a0b&#xff0c;\u4e14\u5c3e\u63d2\u6cd5\u4fdd\u8bc1\u4e86\u6570\u636e\u7ed3\u6784\u7684\u6b63\u786e\u6027&#xff0c;\u907f\u514d\u4e86\u94fe\u8868\u6210\u73af<\/td>\n<\/tr>\n<tr>\n<td>\u67e5\u627e\u673a\u5236<\/td>\n<td>\u94fe\u8868\u7ed3\u6784&#xff0c;\u67e5\u627e\u65f6\u95f4\u590d\u6742\u5ea6\u5e73\u5747\u4e3a O (n)<\/td>\n<td>\u94fe\u8868\u957f\u5ea6\u8d85\u8fc7\u9608\u503c&#xff08;\u9ed8\u8ba4 8&#xff09;\u65f6\u8f6c\u6362\u4e3a\u7ea2\u9ed1\u6811&#xff0c;\u67e5\u627e\u65f6\u95f4\u590d\u6742\u5ea6\u5e73\u5747\u4e3a O (log n)<\/td>\n<td>Java 8 \u5728\u6570\u636e\u91cf\u8f83\u5927\u4e14\u54c8\u5e0c\u51b2\u7a81\u8f83\u591a\u65f6&#xff0c;\u7ea2\u9ed1\u6811\u7ed3\u6784\u964d\u4f4e\u4e86\u67e5\u627e\u65f6\u95f4\u590d\u6742\u5ea6&#xff0c;\u63d0\u9ad8\u4e86\u67e5\u627e\u6548\u7387<\/td>\n<\/tr>\n<tr>\n<td>\u5185\u5b58\u4f7f\u7528<\/td>\n<td>\u4e0d\u662f\u61d2\u60f0\u521d\u59cb\u5316&#xff0c;\u521b\u5efa\u65f6\u4f1a\u521d\u59cb\u5316Segment\u6570\u7ec4\u53ca\u5185\u90e8\u90e8\u5206\u8d44\u6e90<\/td>\n<td>\u61d2\u60f0\u521d\u59cb\u5316&#xff0c;\u9996\u6b21\u63d2\u5165\u5143\u7d20\u65f6\u624d\u521d\u59cb\u5316\u5e95\u5c42\u6570\u7ec4<\/td>\n<td>Java 8 \u907f\u514d\u4e86\u4e0d\u5fc5\u8981\u7684\u5185\u5b58\u5360\u7528&#xff0c;\u5728\u5185\u5b58\u4f7f\u7528\u4e0a\u66f4\u52a0\u9ad8\u6548&#xff0c;\u9002\u7528\u4e8e\u5185\u5b58\u654f\u611f\u7684\u5e94\u7528\u573a\u666f<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/tr>\n<\/table>\n<\/p>\n<\/p>\n<\/p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb261\u6b21\u3002\u672c\u6587\u8be6\u7ec6\u89e3\u6790\u4e86Java\u4e2dConcurrentHashMap\u5728JDK7\u548cJDK8\u7684\u5b9e\u73b0\u539f\u7406\u4e0e\u4f7f\u7528\u65b9\u5f0f\u3002JDK7\u91c7\u7528\u5206\u6bb5\u9501\u673a\u5236\uff0c\u901a\u8fc7Segment\u6570\u7ec4\u5b9e\u73b0\u5e76\u53d1\u63a7\u5236\uff0c\u6bcf\u4e2aSegment\u76f8\u5f53\u4e8e\u4e00\u4e2a\u5c0f\u578bHashMap\uff1bJDK8\u5219\u4f18\u5316\u4e3a\u66f4\u7ec6\u7c92\u5ea6\u7684\u9501+CAS\u64cd\u4f5c\uff0c\u5f15\u5165\u7ea2\u9ed1\u6811\u63d0\u5347\u67e5\u8be2\u6548\u7387\u3002\u6587\u7ae0\u91cd\u70b9\u5206\u6790\u4e86put\/get\/\u6269\u5bb9\u7b49\u6838\u5fc3\u6d41\u7a0b\uff0c\u5bf9\u6bd4\u4e86\u4e24\u7248\u672c\u5728\u9501\u673a\u5236\u3001\u6570\u636e\u7ed3\u6784\u3001\u6269\u5bb9\u65b9\u5f0f\u7b49\u65b9\u9762\u7684\u5dee\u5f02\uff0c\u5e76\u63d0\u4f9b\u4e86\u6b63\u786e\u4f7f\u7528\u5efa\u8bae\uff1a\u907f\u514d\u76f4\u63a5\u4f7f\u7528get+put\u975e\u539f\u5b50\u64cd\u4f5c\uff0c\u63a8\u8350\u4f7f\u7528computeIfAbsent\u7b49\u539f\u5b50\u65b9\u6cd5\uff1bJDK8\u901a\u8fc7LongAdde<\/p>\n","protected":false},"author":2,"featured_media":56183,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[5825,371,305],"topic":[],"class_list":["post-56188","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-server","tag-juc","tag-371","tag-305"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>JUC\u5b66\u4e60\u7b14\u8bb0-ConcurrentHashMap - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/56188.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"JUC\u5b66\u4e60\u7b14\u8bb0-ConcurrentHashMap - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb261\u6b21\u3002\u672c\u6587\u8be6\u7ec6\u89e3\u6790\u4e86Java\u4e2dConcurrentHashMap\u5728JDK7\u548cJDK8\u7684\u5b9e\u73b0\u539f\u7406\u4e0e\u4f7f\u7528\u65b9\u5f0f\u3002JDK7\u91c7\u7528\u5206\u6bb5\u9501\u673a\u5236\uff0c\u901a\u8fc7Segment\u6570\u7ec4\u5b9e\u73b0\u5e76\u53d1\u63a7\u5236\uff0c\u6bcf\u4e2aSegment\u76f8\u5f53\u4e8e\u4e00\u4e2a\u5c0f\u578bHashMap\uff1bJDK8\u5219\u4f18\u5316\u4e3a\u66f4\u7ec6\u7c92\u5ea6\u7684\u9501+CAS\u64cd\u4f5c\uff0c\u5f15\u5165\u7ea2\u9ed1\u6811\u63d0\u5347\u67e5\u8be2\u6548\u7387\u3002\u6587\u7ae0\u91cd\u70b9\u5206\u6790\u4e86put\/get\/\u6269\u5bb9\u7b49\u6838\u5fc3\u6d41\u7a0b\uff0c\u5bf9\u6bd4\u4e86\u4e24\u7248\u672c\u5728\u9501\u673a\u5236\u3001\u6570\u636e\u7ed3\u6784\u3001\u6269\u5bb9\u65b9\u5f0f\u7b49\u65b9\u9762\u7684\u5dee\u5f02\uff0c\u5e76\u63d0\u4f9b\u4e86\u6b63\u786e\u4f7f\u7528\u5efa\u8bae\uff1a\u907f\u514d\u76f4\u63a5\u4f7f\u7528get+put\u975e\u539f\u5b50\u64cd\u4f5c\uff0c\u63a8\u8350\u4f7f\u7528computeIfAbsent\u7b49\u539f\u5b50\u65b9\u6cd5\uff1bJDK8\u901a\u8fc7LongAdde\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/56188.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2025-08-14T06:15:52+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/08\/20250814061547-689d7f1377d6f.png\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"20 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/56188.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/56188.html\",\"name\":\"JUC\u5b66\u4e60\u7b14\u8bb0-ConcurrentHashMap - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2025-08-14T06:15:52+00:00\",\"dateModified\":\"2025-08-14T06:15:52+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/56188.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/56188.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/56188.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"JUC\u5b66\u4e60\u7b14\u8bb0-----ConcurrentHashMap\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"JUC\u5b66\u4e60\u7b14\u8bb0-ConcurrentHashMap - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/56188.html","og_locale":"zh_CN","og_type":"article","og_title":"JUC\u5b66\u4e60\u7b14\u8bb0-ConcurrentHashMap - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb261\u6b21\u3002\u672c\u6587\u8be6\u7ec6\u89e3\u6790\u4e86Java\u4e2dConcurrentHashMap\u5728JDK7\u548cJDK8\u7684\u5b9e\u73b0\u539f\u7406\u4e0e\u4f7f\u7528\u65b9\u5f0f\u3002JDK7\u91c7\u7528\u5206\u6bb5\u9501\u673a\u5236\uff0c\u901a\u8fc7Segment\u6570\u7ec4\u5b9e\u73b0\u5e76\u53d1\u63a7\u5236\uff0c\u6bcf\u4e2aSegment\u76f8\u5f53\u4e8e\u4e00\u4e2a\u5c0f\u578bHashMap\uff1bJDK8\u5219\u4f18\u5316\u4e3a\u66f4\u7ec6\u7c92\u5ea6\u7684\u9501+CAS\u64cd\u4f5c\uff0c\u5f15\u5165\u7ea2\u9ed1\u6811\u63d0\u5347\u67e5\u8be2\u6548\u7387\u3002\u6587\u7ae0\u91cd\u70b9\u5206\u6790\u4e86put\/get\/\u6269\u5bb9\u7b49\u6838\u5fc3\u6d41\u7a0b\uff0c\u5bf9\u6bd4\u4e86\u4e24\u7248\u672c\u5728\u9501\u673a\u5236\u3001\u6570\u636e\u7ed3\u6784\u3001\u6269\u5bb9\u65b9\u5f0f\u7b49\u65b9\u9762\u7684\u5dee\u5f02\uff0c\u5e76\u63d0\u4f9b\u4e86\u6b63\u786e\u4f7f\u7528\u5efa\u8bae\uff1a\u907f\u514d\u76f4\u63a5\u4f7f\u7528get+put\u975e\u539f\u5b50\u64cd\u4f5c\uff0c\u63a8\u8350\u4f7f\u7528computeIfAbsent\u7b49\u539f\u5b50\u65b9\u6cd5\uff1bJDK8\u901a\u8fc7LongAdde","og_url":"https:\/\/www.wsisp.com\/helps\/56188.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2025-08-14T06:15:52+00:00","og_image":[{"url":"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/08\/20250814061547-689d7f1377d6f.png"}],"author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"20 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/56188.html","url":"https:\/\/www.wsisp.com\/helps\/56188.html","name":"JUC\u5b66\u4e60\u7b14\u8bb0-ConcurrentHashMap - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2025-08-14T06:15:52+00:00","dateModified":"2025-08-14T06:15:52+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/56188.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/56188.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/56188.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"JUC\u5b66\u4e60\u7b14\u8bb0-----ConcurrentHashMap"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/56188","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=56188"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/56188\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media\/56183"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=56188"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=56188"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=56188"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=56188"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}