{"id":1597,"date":"2017-07-06T14:53:48","date_gmt":"2017-07-06T06:53:48","guid":{"rendered":"http:\/\/www.xttblog.com\/?p=1597"},"modified":"2017-07-06T14:53:48","modified_gmt":"2017-07-06T06:53:48","slug":"java-concurrenthashmap-%e6%95%99%e7%a8%8b","status":"publish","type":"post","link":"https:\/\/www.xttblog.com\/?p=1597","title":{"rendered":"java ConcurrentHashMap \u6559\u7a0b"},"content":{"rendered":"<p>\n\tConcurrentHashMap\u5177\u4f53\u662f\u600e\u4e48\u5b9e\u73b0\u7ebf\u7a0b\u5b89\u5168\u7684\u5462,\u80af\u5b9a\u4e0d\u53ef\u80fd\u662f\u6bcf\u4e2a\u65b9\u6cd5\u52a0synchronized,\u90a3\u6837\u5c31\u53d8\u6210\u4e86HashTable\u3002\n<\/p>\n<p>\n\t\u96c6\u5408\u662f\u7f16\u7a0b\u4e2d\u6700\u5e38\u7528\u7684\u6570\u636e\u7ed3\u6784\u3002\u800c\u8c08\u5230\u5e76\u53d1\uff0c\u51e0\u4e4e\u603b\u662f\u79bb\u4e0d\u5f00\u96c6\u5408\u8fd9\u7c7b\u9ad8\u7ea7\u6570\u636e\u7ed3\u6784\u7684\u652f\u6301\u3002\u6bd4\u5982\u4e24\u4e2a\u7ebf\u7a0b\u9700\u8981\u540c\u65f6\u8bbf\u95ee\u4e00\u4e2a\u4e2d\u95f4\u4e34\u754c\u533a\uff08Queue\uff09\uff0c\u6bd4\u5982\u5e38\u4f1a\u7528\u7f13\u5b58\u4f5c\u4e3a\u5916\u90e8\u6587\u4ef6\u7684\u526f\u672c\uff08HashMap\uff09\u3002\u8fd9\u7bc7\u6587\u7ae0\u4e3b\u8981\u5206\u6790jdk1.5\u76843\u79cd\u5e76\u53d1\u96c6\u5408\u7c7b\u578b\uff08concurrent\uff0ccopyonright\uff0cqueue\uff09\u4e2d\u7684ConcurrentHashMap\uff0c\u8ba9\u6211\u4eec\u4ece\u539f\u7406\u4e0a\u7ec6\u81f4\u7684\u4e86\u89e3\u5b83\u4eec\uff0c\u80fd\u591f\u8ba9\u6211\u4eec\u5728\u6df1\u5ea6\u9879\u76ee\u5f00\u53d1\u4e2d\u83b7\u76ca\u975e\u6d45\u3002\n<\/p>\n<p>\n\t\u901a\u8fc7\u5206\u6790Hashtable\u5c31\u77e5\u9053\uff0csynchronized\u662f\u9488\u5bf9\u6574\u5f20Hash\u8868\u7684\uff0c\u5373\u6bcf\u6b21\u9501\u4f4f\u6574\u5f20\u8868\u8ba9\u7ebf\u7a0b\u72ec\u5360\uff0cConcurrentHashMap\u5141\u8bb8\u591a\u4e2a\u4fee\u6539\u64cd\u4f5c\u5e76\u53d1\u8fdb\u884c\uff0c\u5176\u5173\u952e\u5728\u4e8e\u4f7f\u7528\u4e86\u9501\u5206\u79bb\u6280\u672f\u3002\u5b83\u4f7f\u7528\u4e86\u591a\u4e2a\u9501\u6765\u63a7\u5236\u5bf9hash\u8868\u7684\u4e0d\u540c\u90e8\u5206\u8fdb\u884c\u7684\u4fee\u6539\u3002ConcurrentHashMap\u5185\u90e8\u4f7f\u7528\u6bb5(Segment)\u6765\u8868\u793a\u8fd9\u4e9b\u4e0d\u540c\u7684\u90e8\u5206\uff0c\u6bcf\u4e2a\u6bb5\u5176\u5b9e\u5c31\u662f\u4e00\u4e2a\u5c0f\u7684hash table\uff0c\u5b83\u4eec\u6709\u81ea\u5df1\u7684\u9501\u3002\u53ea\u8981\u591a\u4e2a\u4fee\u6539\u64cd\u4f5c\u53d1\u751f\u5728\u4e0d\u540c\u7684\u6bb5\u4e0a\uff0c\u5b83\u4eec\u5c31\u53ef\u4ee5\u5e76\u53d1\u8fdb\u884c\u3002<br \/>\n\t\u6709\u4e9b\u65b9\u6cd5\u9700\u8981\u8de8\u6bb5\uff0c\u6bd4\u5982size()\u548ccontainsValue()\uff0c\u5b83\u4eec\u53ef\u80fd\u9700\u8981\u9501\u5b9a\u6574\u4e2a\u8868\u800c\u800c\u4e0d\u4ec5\u4ec5\u662f\u67d0\u4e2a\u6bb5\uff0c\u8fd9\u9700\u8981\u6309\u987a\u5e8f\u9501\u5b9a\u6240\u6709\u6bb5\uff0c\u64cd\u4f5c\u5b8c\u6bd5\u540e\uff0c\u53c8\u6309\u987a\u5e8f\u91ca\u653e\u6240\u6709\u6bb5\u7684\u9501\u3002\u8fd9\u91cc&ldquo;\u6309\u987a\u5e8f&rdquo;\u662f\u5f88\u91cd\u8981\u7684\uff0c\u5426\u5219\u6781\u6709\u53ef\u80fd\u51fa\u73b0\u6b7b\u9501\uff0c\u5728ConcurrentHashMap\u5185\u90e8\uff0c\u6bb5\u6570\u7ec4\u662ffinal\u7684\uff0c\u5e76\u4e14\u5176\u6210\u5458\u53d8\u91cf\u5b9e\u9645\u4e0a\u4e5f\u662ffinal\u7684\uff0c\u4f46\u662f\uff0c\u4ec5\u4ec5\u662f\u5c06\u6570\u7ec4\u58f0\u660e\u4e3afinal\u7684\u5e76\u4e0d\u4fdd\u8bc1\u6570\u7ec4\u6210\u5458\u4e5f\u662ffinal\u7684\uff0c\u8fd9\u9700\u8981\u5b9e\u73b0\u4e0a\u7684\u4fdd\u8bc1\u3002\u8fd9\u53ef\u4ee5\u786e\u4fdd\u4e0d\u4f1a\u51fa\u73b0\u6b7b\u9501\uff0c\u56e0\u4e3a\u83b7\u5f97\u9501\u7684\u987a\u5e8f\u662f\u56fa\u5b9a\u7684\u3002\n<\/p>\n<p>\n\t\u5f53\u8d44\u6e90\u5728\u591a\u7ebf\u7a0b\u4e0b\u5171\u4eab\u65f6\u4f1a\u4ea7\u751f\u4e00\u4e9b\u903b\u8f91\u95ee\u9898\uff0c\u8fd9\u4e2a\u65f6\u5019\u7c7b\u6216\u8005\u65b9\u6cd5\u4f1a\u4ea7\u751f\u4e0d\u7b26\u5408\u6b63\u5e38\u903b\u8f91\u7684\u7ed3\u679c\uff0c\u5219\u4e0d\u662f\u7ebf\u7a0b\u5b89\u5168\u7684\u3002\u7eb5\u89c2jdk\u7684\u7248\u672c\u66f4\u65b0\uff0c\u53ef\u4ee5\u770b\u5230jdk\u7684\u5f00\u53d1\u4eba\u5458\u5728\u9ad8\u5e76\u53d1\u548c\u591a\u7ebf\u7a0b\u4e0b\u4e86\u5f88\u5927\u7684\u529f\u592b\uff0c\u5c3d\u53ef\u80fd\u7684\u901a\u8fc7jdk\u539f\u751fAPI\u6765\u7ed9\u5f00\u53d1\u4eba\u5458\u5e26\u6765\u6700\u65b9\u4fbf\u6700\u8f7b\u677e\u7684\u9ad8\u5e76\u53d1\u6570\u636e\u6a21\u578b\uff0c\u751a\u81f3\u60f3\u5b8c\u5168\u4e3a\u5f00\u53d1\u4eba\u5458\u89e3\u51b3\u5e76\u53d1\u95ee\u9898\uff0c\u53ef\u4ee5\u770b\u5f97\u51fa\u6765jdk\u7684\u5f00\u53d1\u4eba\u5458\u786e\u5b9e\u5f88\u7528\u5fc3\u3002\u4f46\u662f\u5728\u5927\u91cf\u4e1a\u52a1\u6570\u636e\u7684\u903b\u8f91\u4ee3\u7801\u7684\u60c5\u51b5\u4e0b\u9ad8\u5e76\u53d1\u8fd8\u662f\u4e0d\u53ef\u907f\u514d\uff0c\u4e5f\u4e0d\u53ef\u80fd\u5b8c\u5168\u901a\u8fc7jdk\u539f\u751f\u7684\u5e76\u53d1API\u53bb\u89e3\u51b3\u8fd9\u4e9b\u5e76\u53d1\u95ee\u9898\uff0c\u5f00\u53d1\u4eba\u5458\u4e0d\u5f97\u4e0d\u81ea\u5df1\u53bb\u7a7a\u503c\u5728\u9ad8\u5e76\u53d1\u73af\u5883\u4e0b\u7684\u6570\u636e\u9ad8\u53ef\u7528\u6027\u548c\u4e00\u81f4\u6027\u3002\n<\/p>\n<p>\n\t\u524d\u9762\u8bf4\u4e86jdk\u539f\u751f\u7684API\u5df2\u7ecf\u6709\u4e86\u5f88\u591a\u7684\u9ad8\u5e76\u53d1\u4ea7\u54c1\uff0c\u5728java.util.concurrent\u5305\u4e0b\u6709\u5f88\u591a\u89e3\u51b3\u9ad8\u5e76\u53d1\uff0c\u9ad8\u541e\u5410\u91cf\uff0c\u591a\u7ebf\u7a0b\u95ee\u9898\u7684API\u3002\u6bd4\u5982\u7ebf\u7a0b\u6c60ThreadPoolExecutor\uff0c\u7ebf\u7a0b\u6c60\u5de5\u5382Executors\uff0cFuture\u6a21\u5f0f\u4e0b\u7684\u63a5\u53e3Future\uff0c\u963b\u585e\u961f\u5217BlockingQueue\u7b49\u7b49\u3002\n<\/p>\n<h2>\n\t\u6570\u636e\u7684\u53ef\u89c1\u6027<br \/>\n<\/h2>\n<p>\n\tconcurrentHashMap\u76f8\u4fe1\u7528\u7684\u4eba\u4e5f\u5f88\u591a\uff0c\u56e0\u4e3a\u5728\u6570\u636e\u5b89\u5168\u6027\u4e0a\u786e\u5b9e\u6bd4HashMap\u597d\u7528\uff0c\u5728\u6027\u80fd\u4e0a\u6bd4hashtable\u4e5f\u597d\u7528\u3002\u5927\u5bb6\u90fd\u77e5\u9053\u7ebf\u7a0b\u5728\u64cd\u4f5c\u4e00\u4e2a\u53d8\u91cf\u7684\u65f6\u5019\uff0c\u6bd4\u5982i++\uff0cjvm\u6267\u884c\u7684\u65f6\u5019\u9700\u8981\u7ecf\u8fc7\u4e24\u4e2a\u5185\u5b58\uff0c\u4e3b\u5185\u5b58\u548c\u5de5\u4f5c\u5185\u5b58\u3002\u90a3\u4e48\u5728\u7ebf\u7a0bA\u5bf9i\u8fdb\u884c\u52a01\u7684\u65f6\u5019\uff0c\u5b83\u9700\u8981\u53bb\u4e3b\u5185\u5b58\u62ff\u5230\u53d8\u91cf\u503c\uff0c\u8fd9\u4e2a\u65f6\u5019\u5de5\u4f5c\u5185\u5b58\u4e2d\u4fbf\u6709\u4e86\u4e00\u4e2a\u53d8\u91cf\u6570\u636e\u7684\u526f\u672c\uff0c\u6267\u884c\u5b8c\u8fd9\u4e9b\u4e4b\u540e\uff0c\u518d\u53bb\u5bf9\u53d8\u91cf\u771f\u6b63\u7684\u52a01\uff0c\u4f46\u662f\u6b64\u65f6\u7ebf\u7a0bB\u4e5f\u8981\u64cd\u4f5c\u53d8\u91cf\uff0c\u5e76\u4e14\u903b\u8f91\u4e0a\u4e5f\u662f\u6ca1\u6709\u7ef4\u62a4\u591a\u7ebf\u7a0b\u8bbf\u95ee\u7684\u9650\u5236\uff0c\u5219\u5f88\u6709\u53ef\u80fd\u5728\u7ebf\u7a0bA\u5728\u4ece\u4e3b\u5185\u5b58\u83b7\u53d6\u6570\u636e\u5e76\u5728\u4fee\u6539\u7684\u65f6\u5019\u7ebf\u7a0bB\u53bb\u4e3b\u5185\u5b58\u62ff\u6570\u636e\uff0c\u4f46\u662f\u8fd9\u4e2a\u65f6\u5019\u4e3b\u5185\u5b58\u7684\u6570\u636e\u8fd8\u6ca1\u6709\u66f4\u65b0\uff0cA\u7ebf\u7a0b\u8fd8\u6ca1\u6709\u6765\u5f97\u53ca\u8bb2\u52a01\u540e\u7684\u53d8\u91cf\u56de\u586b\u5230\u4e3b\u5185\u5b58\uff0c\u8fd9\u4e2a\u65f6\u5019\u53d8\u91cf\u5728\u8fd9\u4e24\u4e2a\u7ebf\u7a0b\u64cd\u4f5c\u7684\u60c5\u51b5\u4e0b\u5c31\u4f1a\u53d1\u751f\u903b\u8f91\u9519\u8bef\u3002\n<\/p>\n<h2>\n\t\u539f\u5b50\u6027<br \/>\n<\/h2>\n<p>\n\t\u539f\u5b50\u6027\u5c31\u662f\u5f53\u67d0\u4e00\u4e2a\u7ebf\u7a0bA\u4fee\u6539i\u7684\u503c\u7684\u65f6\u5019\uff0c\u4ece\u53d6\u51fai\u5230\u5c06\u65b0\u7684i\u7684\u503c\u5199\u7ed9i\u4e4b\u95f4\u7ebf\u7a0bB\u4e0d\u80fd\u5bf9i\u8fdb\u884c\u4efb\u4f55\u64cd\u4f5c\u3002\u4e5f\u5c31\u662f\u8bf4\u4fdd\u8bc1\u67d0\u4e2a\u7ebf\u7a0b\u5bf9i\u7684\u64cd\u4f5c\u662f\u539f\u5b50\u6027\u7684\uff0c\u8fd9\u6837\u5c31\u53ef\u4ee5\u907f\u514d\u6570\u636e\u810f\u8bfb\u3002\n<\/p>\n<h2>\n\tvolatile\u7684\u4f5c\u7528<br \/>\n<\/h2>\n<p>\n\tVolatile\u4fdd\u8bc1\u4e86\u6570\u636e\u5728\u591a\u7ebf\u7a0b\u4e4b\u95f4\u7684\u53ef\u89c1\u6027\uff0c\u6bcf\u4e2a\u7ebf\u7a0b\u5728\u83b7\u53d6volatile\u4fee\u9970\u7684\u53d8\u91cf\u65f6\u5019\u90fd\u56de\u53bb\u4e3b\u5185\u5b58\u83b7\u53d6\uff0c\u6240\u4ee5\u5f53\u7ebf\u7a0bA\u4fee\u6539\u4e86\u88abvolatile\u4fee\u9970\u7684\u6570\u636e\u540e\u5176\u4ed6\u7ebf\u7a0b\u770b\u5230\u7684\u4e00\u5b9a\u662f\u4fee\u6539\u8fc7\u540e\u6700\u65b0\u7684\u6570\u636e\uff0c\u4e5f\u662f\u56e0\u4e3avolatile\u4fee\u9970\u7684\u53d8\u91cf\u6570\u636e\u6bcf\u6b21\u90fd\u8981\u53bb\u4e3b\u5185\u5b58\u83b7\u53d6\uff0c\u5728\u6027\u80fd\u4e0a\u4f1a\u6709\u4e9b\u727a\u7272\u3002\n<\/p>\n<h2>\n\t\u63aa\u65bd<br \/>\n<\/h2>\n<p>\n\tHashMap\u5728\u591a\u7ebf\u7a0b\u7684\u573a\u666f\u4e0b\u662f\u4e0d\u5b89\u5168\u7684\uff0chashtable\u867d\u7136\u662f\u5728\u6570\u636e\u8868\u4e0a\u52a0\u9501\uff0c\u7eb5\u7136\u6570\u636e\u5b89\u5168\u4e86\uff0c\u4f46\u662f\u6027\u80fd\u65b9\u9762\u786e\u5b9e\u4e0d\u5982HashMap\u3002\u90a3\u4e48\u6765\u770b\u770bconcurrentHashMap\u662f\u5982\u4f55\u89e3\u51b3\u8fd9\u4e9b\u95ee\u9898\u7684\u3002\n<\/p>\n<p>\n\tconcurrentHashMap\u7531\u591a\u4e2asegment\u7ec4\u6210\uff0c\u6bcf\u4e00\u4e2asegment\u90fd\u5305\u542b\u4e86\u4e00\u4e2aHashEntry\u6570\u7ec4\u7684hashtable\uff0c \u6bcf\u4e00\u4e2asegment\u5305\u542b\u4e86\u5bf9\u81ea\u5df1\u7684hashtable\u7684\u64cd\u4f5c\uff0c\u6bd4\u5982get\uff0cput\uff0creplace\u7b49\u64cd\u4f5c(\u8fd9\u4e9b\u64cd\u4f5c\u4e0eHashMap\u903b\u8f91\u90fd\u662f\u4e00\u6837\u7684\uff0c\u4e0d\u540c\u7684\u662fconcurrentHashMap\u5728\u6267\u884c\u8fd9\u4e9b\u64cd\u4f5c\u7684\u65f6\u5019\u52a0\u5165\u4e86\u91cd\u5165\u9501ReentrantLock)\uff0c\u8fd9\u4e9b\u64cd\u4f5c\u53d1\u751f\u7684\u65f6\u5019\uff0c\u5bf9\u81ea\u5df1\u7684hashtable\u8fdb\u884c\u9501\u5b9a\u3002\u7531\u4e8e\u6bcf\u4e00\u4e2asegment\u5199\u64cd\u4f5c\u53ea\u9501\u5b9a\u81ea\u5df1\u7684hashtable\uff0c\u6240\u4ee5\u53ef\u80fd\u5b58\u5728\u591a\u4e2a\u7ebf\u7a0b\u540c\u65f6\u5199\u7684\u60c5\u51b5\uff0c\u6027\u80fd\u65e0\u7591\u597d\u4e8e\u53ea\u6709\u4e00\u4e2ahashtable\u9501\u5b9a\u7684\u60c5\u51b5\u3002\u901a\u4fd7\u7684\u8bb2\u5c31\u662fconcurrentHashMap\u7531\u591a\u4e2ahashtable\u7ec4\u6210\u3002\n<\/p>\n<h2>\n\tconcurrentHashMap \u6e90\u7801<br \/>\n<\/h2>\n<p>\n\t\u770b\u4e0bconcurrentHashMap\u7684remove\u64cd\u4f5c\n<\/p>\n<pre class=\"brush:java;\">\r\nV remove(Object key, int hash, Object value) {\r\n\tlock();\/\/\u91cd\u5165\u9501\r\n\ttry {\r\n\t\tint c = count - 1;\r\n\t\tHashEntry&lt;K,V&gt;[] tab = table;\r\n\t\tint index = hash &amp; (tab.length - 1);\r\n\t\tHashEntry&lt;K,V&gt; first = tab[index];\r\n\t\tHashEntry&lt;K,V&gt; e = first;\r\n\t\twhile (e != null &amp;&amp; (e.hash != hash || !key.equals(e.key)))\r\n\t\t\te = e.next;\r\n\r\n\t\tV oldValue = null;\r\n\t\tif (e != null) {\r\n\t\t\tV v = e.value;\r\n\t\t\tif (value == null || value.equals(v)) {\r\n\t\t\t\toldValue = v;\r\n\t\t\t\t\/\/ All entries following removed node can stay\r\n\t\t\t\t\/\/ in list, but all preceding ones need to be\r\n\t\t\t\t\/\/ cloned.\r\n\t\t\t\t++modCount;\r\n\t\t\t\tHashEntry&lt;K,V&gt; newFirst = e.next;\r\n\t\t\t\tfor (HashEntry&lt;K,V&gt; p = first; p != e; p = p.next)\r\n\t\t\t\t\tnewFirst = new HashEntry&lt;K,V&gt;(p.key, p.hash,\r\n\t\t\t\t\t\t\t\t\t\t\t\t  newFirst, p.value);\r\n\t\t\t\ttab[index] = newFirst;\r\n\t\t\t\tcount = c; \/\/ write-volatile\r\n\t\t\t}\r\n\t\t}\r\n\t\treturn oldValue;\r\n\t} finally {\r\n\t\tunlock();\/\/\u91ca\u653e\u9501\r\n\t}\r\n}<\/pre>\n<p>\n\tCount\u662f\u88abvolatile\u6240\u4fee\u9970\uff0c\u4fdd\u8bc1\u4e86count\u7684\u53ef\u89c1\u6027\uff0c\u907f\u514d\u64cd\u4f5c\u6570\u636e\u7684\u65f6\u5019\u4ea7\u751f\u903b\u8f91\u9519\u8bef\u3002segment\u4e2d\u7684remove\u64cd\u4f5c\u548cHashMap\u5927\u81f4\u4e00\u6837\uff0cHashMap\u6ca1\u6709lock()\u548cunlock()\u64cd\u4f5c\u3002\n<\/p>\n<p>\n\t\u770b\u4e0bconcurrentHashMap\u7684get\u6e90\u7801\n<\/p>\n<pre class=\"brush:java;\">\r\nV get(Object key, int hash) {\r\n\tif (count != 0) { \/\/ read-volatile\r\n\t\tHashEntry&lt;K,V&gt; e = getFirst(hash);\r\n\t\u3000\u3000\/\/\u5982\u679c\u6ca1\u6709\u627e\u5230\u5219\u76f4\u63a5\u8fd4\u56denull\r\n\t\twhile (e != null) {\r\n\t\t\tif (e.hash == hash &amp;&amp; key.equals(e.key)) {\r\n\t\u3000\u3000\u3000\u3000\u3000\u3000\/\/\u7531\u4e8e\u6ca1\u6709\u52a0\u9501\uff0c\u5728get\u7684\u8fc7\u7a0b\u4e2d\uff0c\u53ef\u80fd\u4f1a\u6709\u66f4\u65b0\uff0c\u62ff\u5230\u7684key\u5bf9\u5e94\u7684value\u53ef\u80fd\u4e3anull\uff0c\u9700\u8981\u5355\u72ec\u5224\u65ad\u4e00\u904d\r\n\t\t\t\tV v = e.value;\r\n\t\u3000\u3000\u3000\u3000\u3000\u3000\/\/\u5982\u679cvalue\u4e3a\u4e0d\u4e3anull\uff0c\u5219\u8fd4\u56de\u83b7\u53d6\u5230\u7684value\r\n\t\t\t\tif (v != null)\r\n\t\t\t\t\treturn v;\r\n\t\t\t\treturn readValueUnderLock(e); \/\/ recheck\r\n\t\t\t}\r\n\t\t\te = e.next;\r\n\t\t}\r\n\t}\r\n\treturn null;\r\n}<\/pre>\n<p>\n\t\u5173\u4e8econcurrentHashMap\u7684get\u7684\u76f8\u5173\u8bf4\u660e\u5df2\u7ecf\u5728\u4e0a\u9762\u4ee3\u7801\u4e2d\u7ed9\u51fa\u4e86\u6ce8\u91ca\uff0c\u8fd9\u91cc\u5c31\u4e0d\u591a\u8bf4\u4e86\u3002\n<\/p>\n<p>\n\t\u770b\u4e0bconcurrentHashMap\u4e2d\u7684put\n<\/p>\n<pre class=\"brush:java;\">\r\npublic V put(K key, V value) {\r\n\tif (value == null)\r\n\t\tthrow new NullPointerException();\r\n\tint hash = hash(key.hashCode());\r\n\treturn segmentFor(hash).put(key, hash, value, false);\r\n}<\/pre>\n<p>\n\t\u53ef\u4ee5\u770b\u5230concurrentHashMap\u4e0d\u5141\u8bb8key\u6216\u8005value\u4e3anull\n<\/p>\n<p>\n\t\u63a5\u4e0b\u6765\u770b\u4e0bsegment\u7684put\n<\/p>\n<pre class=\"brush:java;\">\r\nV put(K key, int hash, V value, boolean onlyIfAbsent) {\r\n\tlock();\r\n\ttry {\r\n\t\tint c = count;\r\n\t\tif (c++ &gt; threshold) \/\/ ensure capacity\r\n\t\t\trehash();\r\n\t\tHashEntry&lt;K,V&gt;[] tab = table;\r\n\t\tint index = hash &amp; (tab.length - 1);\r\n\t\tHashEntry&lt;K,V&gt; first = tab[index];\r\n\t\tHashEntry&lt;K,V&gt; e = first;\r\n\t\twhile (e != null &amp;&amp; (e.hash != hash || !key.equals(e.key)))\r\n\t\t\te = e.next;\r\n\r\n\t\tV oldValue;\r\n\t\tif (e != null) {\r\n\t\t\toldValue = e.value;\r\n\t\t\tif (!onlyIfAbsent)\r\n\t\t\t\te.value = value;\r\n\t\t}\r\n\t\telse {\r\n\t\t\toldValue = null;\r\n\t\t\t++modCount;\r\n\t\t\ttab[index] = new HashEntry&lt;K,V&gt;(key, hash, first, value);\r\n\t\t\tcount = c; \/\/ write-volatile\r\n\t\t}\r\n\t\treturn oldValue;\r\n\t} finally {\r\n\t\tunlock();\r\n\t}\r\n}<\/pre>\n<p>\n\t\u540c\u6837\u4e5f\u662f\u52a0\u5165\u4e86\u91cd\u5165\u9501\uff0c\u5176\u4ed6\u7684\u57fa\u672c\u548cHashMap\u903b\u8f91\u5dee\u4e0d\u591a\u3002\u503c\u5f97\u4e00\u63d0\u7684\u662fjdk8\u4e2d\u6dfb\u52a0\u7684\u4e2d\u7684putval\uff0c\u8fd9\u91cc\u5c31\u4e0d\u591a\u8bf4\u4e86\u3002\n<\/p>\n<h2>\n\t\u603b\u7ed3<br \/>\n<\/h2>\n<p>\n\tConcurrentHashmap\u5c06\u6570\u636e\u7ed3\u6784\u5206\u4e3a\u4e86\u591a\u4e2aSegment\uff0c\u4e5f\u662f\u4f7f\u7528\u91cd\u5165\u9501\u6765\u89e3\u51b3\u9ad8\u5e76\u53d1\uff0c\u8bb2\u4ed6\u5206\u4e3a\u591a\u4e2asegment\u662f\u4e3a\u4e86\u51cf\u5c0f\u9501\u7684\u529b\u5ea6\uff0c\u6dfb\u52a0\u7684\u65f6\u5019\u52a0\u4e86\u9501\uff0c\u7d22\u5f15\u7684\u65f6\u5019\u6ca1\u6709\u52a0\u9501\uff0c\u4f7f\u7528volatile\u4fee\u9970count\u662f\u4e3a\u4e86\u4fdd\u6301count\u7684\u53ef\u89c1\u6027\uff0c\u90fd\u662fjdk\u4e3a\u4e86\u89e3\u51b3\u5e76\u53d1\u548c\u591a\u7ebf\u7a0b\u64cd\u4f5c\u7684\u5e38\u7528\u624b\u6bb5\u3002<\/p>\n<!-- <audio autoplay=\"autoplay\" height=\"100\" width=\"100\" hidden=\"true\"><source src=\"https:\/\/c.coka.la\/orAvPR.mp3\" type=\"audio\/mp3\"\/><\/audio><img decoding=\"async\" src=\"https:\/\/images.weserv.nl\/?url=https:\/\/ws3.sinaimg.cn\/large\/8fa5dcfcgy1g4em3rzar5j20ib089ae0.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/> --><p><!-- <img decoding=\"async\" src=\"https:\/\/raw.githubusercontent.com\/xmt1139057136\/xttblog\/master\/8fa5dcfcgy1g4em3rzar5j20ib089ae0.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/>--><img decoding=\"async\" src=\"https:\/\/saas.51autoparts.cn\/api\/admin\/sys-file\/oss\/file?fileName=84db51f8561e448f9a4fda2eff0811f9.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/><\/p><p>\u6700\u540e\uff0c\u6b22\u8fce\u5173\u6ce8\u6211\u7684\u4e2a\u4eba\u5fae\u4fe1\u516c\u4f17\u53f7\uff1a\u4e1a\u4f59\u8349\uff08yyucao\uff09\uff01<b>\u53ef\u52a0\u4f5c\u8005\u5fae\u4fe1\u53f7\uff1a<font color=\"red\">xttblog2<\/font>\u3002\u5907\u6ce8\uff1a\u201c1\u201d\uff0c\u6dfb\u52a0\u535a\u4e3b\u5fae\u4fe1\u62c9\u4f60\u8fdb\u5fae\u4fe1\u7fa4\u3002\u5907\u6ce8\u9519\u8bef\u4e0d\u4f1a\u540c\u610f\u597d\u53cb\u7533\u8bf7\u3002<\/b>\u518d\u6b21\u611f\u8c22\u60a8\u7684\u5173\u6ce8\uff01\u540e\u7eed\u6709\u7cbe\u5f69\u5185\u5bb9\u4f1a\u7b2c\u4e00\u65f6\u95f4\u53d1\u7ed9\u60a8\uff01\u539f\u521b\u6587\u7ae0\u6295\u7a3f\u8bf7\u53d1\u9001\u81f3532009913@qq.com\u90ae\u7bb1\u3002\u5546\u52a1\u5408\u4f5c\u4e5f\u53ef\u6dfb\u52a0\u4f5c\u8005\u5fae\u4fe1\u8fdb\u884c\u8054\u7cfb\uff01<\/p><p>\u672c\u6587\u539f\u6587\u51fa\u5904\uff1a<a href=\"https:\/\/www.xttblog.com\">\u4e1a\u4f59\u8349<\/a>\uff1a &raquo; <a href=\"https:\/\/www.xttblog.com\/?p=1597\">java ConcurrentHashMap \u6559\u7a0b<\/a><\/p><!-- <div class=\"bd-reward-stl\"><button id=\"bdRewardBtn\"><span><\/span><\/button><\/div> -->","protected":false},"excerpt":{"rendered":"<p>ConcurrentHashMap\u5177\u4f53\u662f\u600e\u4e48\u5b9e\u73b0\u7ebf\u7a0b\u5b89\u5168\u7684\u5462,\u80af\u5b9a\u4e0d\u53ef\u80fd\u662f\u6bcf\u4e2a\u65b9\u6cd5\u52a0synchronized, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[1164,1161,1334,712,1335],"class_list":["post-1597","post","type-post","status-publish","format-standard","hentry","category-java","tag-concurrenthashmap","tag-hashmap","tag-jdk8","tag-reentrantlock","tag-volatile"],"_links":{"self":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/1597","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1597"}],"version-history":[{"count":1,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/1597\/revisions"}],"predecessor-version":[{"id":1598,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/1597\/revisions\/1598"}],"wp:attachment":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1597"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1597"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1597"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}