{"id":2086,"date":"2017-08-05T16:41:10","date_gmt":"2017-08-05T08:41:10","guid":{"rendered":"https:\/\/ai.zhousir.top\/?p=2086"},"modified":"2017-08-06T16:55:13","modified_gmt":"2017-08-06T08:55:13","slug":"concurrenthashmap%e4%b8%8ecas","status":"publish","type":"post","link":"https:\/\/ai.zhousir.top\/?p=2086","title":{"rendered":"ConcurrentHashMap\u4e0eCAS"},"content":{"rendered":"<h2>\u4e00\u3001CAS:Compare and Swap,\u6bd4\u8f83\u5e76\u4ea4\u6362\u3002<\/h2>\n<h3>1. \u60b2\u89c2\u9501\u548c\u4e50\u89c2\u9501<\/h3>\n<p>java.util.concurrent\u5305\u4e2d\u501f\u52a9CAS\u5b9e\u73b0\u4e86\u533a\u522b\u4e8esynchronouse\u540c\u6b65\u9501\u7684\u4e00\u79cd\u4e50\u89c2\u9501\uff0c\u4e50\u89c2\u9501\u7528\u5230\u7684\u673a\u5236\u5c31\u662fCAS.<\/p>\n<p>\u5728JDK 5\u4e4b\u524dJava\u8bed\u8a00\u662f\u9760synchronized\u5173\u952e\u5b57\u4fdd\u8bc1\u540c\u6b65\u7684\uff0c\u8fd9\u4f1a\u5bfc\u81f4\u4ee5\u4e0b\u95ee\u9898\uff1a<\/p>\n<p>\uff081\uff09\u5728\u591a\u7ebf\u7a0b\u7ade\u4e89\u4e0b\uff0c\u52a0\u9501\u3001\u91ca\u653e\u9501\u4f1a\u5bfc\u81f4\u6bd4\u8f83\u591a\u7684\u4e0a\u4e0b\u6587\u5207\u6362\u548c\u8c03\u5ea6\u5ef6\u65f6\uff0c\u5f15\u8d77\u6027\u80fd\u95ee\u9898\u3002<\/p>\n<p>\uff082\uff09\u4e00\u4e2a\u7ebf\u7a0b\u6301\u6709\u9501\u4f1a\u5bfc\u81f4\u5176\u5b83\u6240\u6709\u9700\u8981\u6b64\u9501\u7684\u7ebf\u7a0b\u6302\u8d77\u3002<\/p>\n<p>\uff083\uff09\u5982\u679c\u4e00\u4e2a\u4f18\u5148\u7ea7\u9ad8\u7684\u7ebf\u7a0b\u7b49\u5f85\u4e00\u4e2a\u4f18\u5148\u7ea7\u4f4e\u7684\u7ebf\u7a0b\u91ca\u653e\u9501\u4f1a\u5bfc\u81f4\u4f18\u5148\u7ea7\u5012\u7f6e\uff0c\u5f15\u8d77\u6027\u80fd\u98ce\u9669\u3002<\/p>\n<p>volatile\u662f\u4e0d\u9519\u7684\u673a\u5236\uff0c\u4f46\u662fvolatile\u4e0d\u80fd\u4fdd\u8bc1\u539f\u5b50\u6027\u3002\u56e0\u6b64\u5bf9\u4e8e\u540c\u6b65\u6700\u7ec8\u8fd8\u662f\u8981\u56de\u5230\u9501\u673a\u5236\u4e0a\u6765\u3002<\/p>\n<p>\u72ec\u5360\u9501\u662f\u4e00\u79cd\u60b2\u89c2\u9501\uff0csynchronized\u5c31\u662f\u4e00\u79cd\u72ec\u5360\u9501\uff0c\u4f1a\u5bfc\u81f4\u5176\u5b83\u6240\u6709\u9700\u8981\u9501\u7684\u7ebf\u7a0b\u6302\u8d77\uff0c\u7b49\u5f85\u6301\u6709\u9501\u7684\u7ebf\u7a0b\u91ca\u653e\u9501\u3002\u800c\u53e6\u4e00\u4e2a\u66f4\u52a0\u6709\u6548\u7684\u9501\u5c31\u662f\u4e50\u89c2\u9501\u3002\u6240\u8c13\u4e50\u89c2\u9501\u5c31\u662f\uff0c\u6bcf\u6b21\u4e0d\u52a0\u9501\u800c\u662f\u5047\u8bbe\u6ca1\u6709\u51b2\u7a81\u800c\u53bb\u5b8c\u6210\u67d0\u9879\u64cd\u4f5c\uff0c\u5982\u679c\u56e0\u4e3a\u51b2\u7a81\u5931\u8d25\u5c31\u91cd\u8bd5\uff0c\u76f4\u5230\u6210\u529f\u4e3a\u6b62\u3002\u4e50\u89c2\u9501\u7528\u5230\u7684\u673a\u5236\u5c31\u662fCAS\uff0cCompare and Swap\u3002<\/p>\n<h3>2. CAS\u7684\u7406\u89e3<\/h3>\n<p>\u6211\u4eec\u90fd\u77e5\u9053\uff0c\u5728java\u8bed\u8a00\u4e4b\u524d\uff0c\u5e76\u53d1\u5c31\u5df2\u7ecf\u5e7f\u6cdb\u5b58\u5728\u5e76\u5728\u670d\u52a1\u5668\u9886\u57df\u5f97\u5230\u4e86\u5927\u91cf\u7684\u5e94\u7528\u3002\u6240\u4ee5\u786c\u4ef6\u5382\u5546\u8001\u65e9\u5c31\u5728\u82af\u7247\u4e2d\u52a0\u5165\u4e86\u5927\u91cf\u76f4\u81f3\u5e76\u53d1\u64cd\u4f5c\u7684\u539f\u8bed\uff0c\u4ece\u800c\u5728\u786c\u4ef6\u5c42\u9762\u63d0\u5347\u6548\u7387\u3002\u5728intel\u7684CPU\u4e2d\uff0c\u4f7f\u7528cmpxchg\u6307\u4ee4\u3002<\/p>\n<p>\u5728Java\u53d1\u5c55\u521d\u671f\uff0cjava\u8bed\u8a00\u662f\u4e0d\u80fd\u591f\u5229\u7528\u786c\u4ef6\u63d0\u4f9b\u7684\u8fd9\u4e9b\u4fbf\u5229\u6765\u63d0\u5347\u7cfb\u7edf\u7684\u6027\u80fd\u7684\u3002\u800c\u968f\u7740java\u4e0d\u65ad\u7684\u53d1\u5c55,Java\u672c\u5730\u65b9\u6cd5(JNI)\u7684\u51fa\u73b0\uff0c\u4f7f\u5f97java\u7a0b\u5e8f\u8d8a\u8fc7JVM\u76f4\u63a5\u8c03\u7528\u672c\u5730\u65b9\u6cd5\u63d0\u4f9b\u4e86\u4e00\u79cd\u4fbf\u6377\u7684\u65b9\u5f0f\uff0c\u56e0\u800cjava\u5728\u5e76\u53d1\u7684\u624b\u6bb5\u4e0a\u4e5f\u591a\u4e86\u8d77\u6765\u3002\u800c\u5728Doug Lea\u63d0\u4f9b\u7684cucurenct\u5305\u4e2d\uff0cCAS\u7406\u8bba\u662f\u5b83\u5b9e\u73b0\u6574\u4e2ajava\u5305\u7684\u57fa\u77f3\u3002<\/p>\n<p>CAS \u64cd\u4f5c\u5305\u542b\u4e09\u4e2a\u64cd\u4f5c\u6570 \u2014\u2014 \u5185\u5b58\u4f4d\u7f6e\uff08V\uff09\u3001\u9884\u671f\u539f\u503c\uff08A\uff09\u548c\u65b0\u503c(B)\u3002 \u5982\u679c\u5185\u5b58\u4f4d\u7f6e\u7684\u503c\u4e0e\u9884\u671f\u539f\u503c\u76f8\u5339\u914d\uff0c\u90a3\u4e48\u5904\u7406\u5668\u4f1a\u81ea\u52a8\u5c06\u8be5\u4f4d\u7f6e\u503c\u66f4\u65b0\u4e3a\u65b0\u503c \u3002\u5426\u5219\uff0c\u5904\u7406\u5668\u4e0d\u505a\u4efb\u4f55\u64cd\u4f5c\u3002\u65e0\u8bba\u54ea\u79cd\u60c5\u51b5\uff0c\u5b83\u90fd\u4f1a\u5728 CAS \u6307\u4ee4\u4e4b\u524d\u8fd4\u56de\u8be5 \u4f4d\u7f6e\u7684\u503c\u3002\uff08\u5728 CAS \u7684\u4e00\u4e9b\u7279\u6b8a\u60c5\u51b5\u4e0b\u5c06\u4ec5\u8fd4\u56de CAS \u662f\u5426\u6210\u529f\uff0c\u800c\u4e0d\u63d0\u53d6\u5f53\u524d \u503c\u3002\uff09CAS \u6709\u6548\u5730\u8bf4\u660e\u4e86\u201c\u6211\u8ba4\u4e3a\u4f4d\u7f6e V \u5e94\u8be5\u5305\u542b\u503c A\uff1b\u5982\u679c\u5305\u542b\u8be5\u503c\uff0c\u5219\u5c06 B \u653e\u5230\u8fd9\u4e2a\u4f4d\u7f6e\uff1b\u5426\u5219\uff0c\u4e0d\u8981\u66f4\u6539\u8be5\u4f4d\u7f6e\uff0c\u53ea\u544a\u8bc9\u6211\u8fd9\u4e2a\u4f4d\u7f6e\u73b0\u5728\u7684\u503c\u5373\u53ef\u3002\u201d<\/p>\n<p>\u901a\u5e38\u5c06 CAS \u7528\u4e8e\u540c\u6b65\u7684\u65b9\u5f0f\u662f\u4ece\u5730\u5740 V \u8bfb\u53d6\u503c A\uff0c\u6267\u884c\u591a\u6b65\u8ba1\u7b97\u6765\u83b7\u5f97\u65b0 \u503c B\uff0c\u7136\u540e\u4f7f\u7528 CAS \u5c06 V \u7684\u503c\u4ece A \u6539\u4e3a B\u3002\u5982\u679c V \u5904\u7684\u503c\u5c1a\u672a\u540c\u65f6\u66f4\u6539\uff0c\u5219 CAS \u64cd\u4f5c\u6210\u529f\u3002<\/p>\n<p>\u7c7b\u4f3c\u4e8e CAS \u7684\u6307\u4ee4\u5141\u8bb8\u7b97\u6cd5\u6267\u884c\u8bfb-\u4fee\u6539-\u5199\u64cd\u4f5c\uff0c\u800c\u65e0\u9700\u5bb3\u6015\u5176\u4ed6\u7ebf\u7a0b\u540c\u65f6 \u4fee\u6539\u53d8\u91cf\uff0c\u56e0\u4e3a\u5982\u679c\u5176\u4ed6\u7ebf\u7a0b\u4fee\u6539\u53d8\u91cf\uff0c\u90a3\u4e48 CAS \u4f1a\u68c0\u6d4b\u5b83\uff08\u5e76\u5931\u8d25\uff09\uff0c\u7b97\u6cd5 \u53ef\u4ee5\u5bf9\u8be5\u64cd\u4f5c\u91cd\u65b0\u8ba1\u7b97\u3002<\/p>\n<h3>3. CAS\u5e94\u7528<\/h3>\n<p>CAS\u67093\u4e2a\u64cd\u4f5c\u6570\uff0c\u5185\u5b58\u503cV\uff0c\u65e7\u7684\u9884\u671f\u503cA\uff0c\u8981\u4fee\u6539\u7684\u65b0\u503cB\u3002\u5f53\u4e14\u4ec5\u5f53\u9884\u671f\u503cA\u548c\u5185\u5b58\u503cV\u76f8\u540c\u65f6\uff0c\u5c06\u5185\u5b58\u503cV\u4fee\u6539\u4e3aB\uff0c\u5426\u5219\u4ec0\u4e48\u90fd\u4e0d\u505a\u3002<\/p>\n<p>\u975e\u963b\u585e\u7b97\u6cd5 \uff08nonblocking algorithms\uff09<\/p>\n<p>\u4e00\u4e2a\u7ebf\u7a0b\u7684\u5931\u8d25\u6216\u8005\u6302\u8d77\u4e0d\u5e94\u8be5\u5f71\u54cd\u5176\u4ed6\u7ebf\u7a0b\u7684\u5931\u8d25\u6216\u6302\u8d77\u7684\u7b97\u6cd5\u3002<\/p>\n<p>\u73b0\u4ee3\u7684CPU\u63d0\u4f9b\u4e86\u7279\u6b8a\u7684\u6307\u4ee4\uff0c\u53ef\u4ee5\u81ea\u52a8\u66f4\u65b0\u5171\u4eab\u6570\u636e\uff0c\u800c\u4e14\u80fd\u591f\u68c0\u6d4b\u5230\u5176\u4ed6\u7ebf\u7a0b\u7684\u5e72\u6270\uff0c\u800c compareAndSet() \u5c31\u7528\u8fd9\u4e9b\u4ee3\u66ff\u4e86\u9501\u5b9a\u3002<\/p>\n<p>\u62ff\u51faAtomicInteger\u6765\u7814\u7a76\u5728\u6ca1\u6709\u9501\u7684\u60c5\u51b5\u4e0b\u662f\u5982\u4f55\u505a\u5230\u6570\u636e\u6b63\u786e\u6027\u7684\u3002<\/p>\n<p>[java]private volatile int value;[\/java]<\/p>\n<p>\u9996\u5148\u6beb\u65e0\u4ee5\u4e3a\uff0c\u5728\u6ca1\u6709\u9501\u7684\u673a\u5236\u4e0b\u53ef\u80fd\u9700\u8981\u501f\u52a9volatile\u539f\u8bed\uff0c\u4fdd\u8bc1\u7ebf\u7a0b\u95f4\u7684\u6570\u636e\u662f\u53ef\u89c1\u7684\uff08\u5171\u4eab\u7684\uff09\u3002<\/p>\n<p>\u8fd9\u6837\u624d\u83b7\u53d6\u53d8\u91cf\u7684\u503c\u7684\u65f6\u5019\u624d\u80fd\u76f4\u63a5\u8bfb\u53d6\u3002<\/p>\n<p>[java]public final int get() {<br \/>\n        return value;<br \/>\n    }<br \/>\n[\/java]<\/p>\n<p>\u7136\u540e\u6765\u770b\u770b++i\u662f\u600e\u4e48\u505a\u5230\u7684\u3002<\/p>\n<p>[java]public final int incrementAndGet() {<br \/>\n    for (;;) {<br \/>\n        int current = get();<br \/>\n        int next = current + 1;<br \/>\n        if (compareAndSet(current, next))<br \/>\n            return next;<br \/>\n    }<br \/>\n}[\/java]<\/p>\n<p>\u5728\u8fd9\u91cc\u91c7\u7528\u4e86CAS\u64cd\u4f5c\uff0c\u6bcf\u6b21\u4ece\u5185\u5b58\u4e2d\u8bfb\u53d6\u6570\u636e\u7136\u540e\u5c06\u6b64\u6570\u636e\u548c+1\u540e\u7684\u7ed3\u679c\u8fdb\u884cCAS\u64cd\u4f5c\uff0c\u5982\u679c\u6210\u529f\u5c31\u8fd4\u56de\u7ed3\u679c\uff0c\u5426\u5219\u91cd\u8bd5\u76f4\u5230\u6210\u529f\u4e3a\u6b62\u3002<\/p>\n<p>\u800ccompareAndSet\u5229\u7528JNI\u6765\u5b8c\u6210CPU\u6307\u4ee4\u7684\u64cd\u4f5c\u3002<\/p>\n<p>[java]public final boolean compareAndSet(int expect, int update) {<br \/>\n    return unsafe.compareAndSwapInt(this, valueOffset, expect, update);<br \/>\n    }[\/java]<\/p>\n<p>\u6574\u4f53\u7684\u8fc7\u7a0b\u5c31\u662f\u8fd9\u6837\u5b50\u7684\uff0c\u5229\u7528CPU\u7684CAS\u6307\u4ee4\uff0c\u540c\u65f6\u501f\u52a9JNI\u6765\u5b8c\u6210Java\u7684\u975e\u963b\u585e\u7b97\u6cd5\u3002\u5176\u5b83\u539f\u5b50\u64cd\u4f5c\u90fd\u662f\u5229\u7528\u7c7b\u4f3c\u7684\u7279\u6027\u5b8c\u6210\u7684\u3002<\/p>\n<h2>\u4e8c\u3001ConcurrentHashMap<\/h2>\n<p>\u5e76\u53d1\u7f16\u7a0b\u5b9e\u8df5\u4e2d\uff0cConcurrentHashMap\u662f\u4e00\u4e2a\u7ecf\u5e38\u88ab\u4f7f\u7528\u7684\u6570\u636e\u7ed3\u6784\uff0c\u76f8\u6bd4\u4e8eHashtable\u4ee5\u53caCollections.synchronizedMap()\uff0cConcurrentHashMap\u5728\u7ebf\u7a0b\u5b89\u5168\u7684\u57fa\u7840\u4e0a\u63d0\u4f9b\u4e86\u66f4\u597d\u7684\u5199\u5e76\u53d1\u80fd\u529b\uff0c\u4f46\u540c\u65f6\u964d\u4f4e\u4e86\u5bf9\u8bfb\u4e00\u81f4\u6027\u7684\u8981\u6c42.<\/p>\n<h3>1. \u8bbe\u8ba1\u539f\u7406:<\/h3>\n<p>JDK7\u4e2d\u5b9e\u73b0<br \/>\nConcurrentHashMap\u91c7\u7528\u4e86\u5206\u6bb5\u9501\u7684\u8bbe\u8ba1\uff0c\u53ea\u6709\u5728\u540c\u4e00\u4e2a\u5206\u6bb5\u5185\u624d\u5b58\u5728\u7ade\u6001\u5173\u7cfb\uff0c\u4e0d\u540c\u7684\u5206\u6bb5\u9501\u4e4b\u95f4\u6ca1\u6709\u9501\u7ade\u4e89\u3002\u76f8\u6bd4\u4e8e\u5bf9\u6574\u4e2aMap\u52a0\u9501\u7684\u8bbe\u8ba1\uff0c\u5206\u6bb5\u9501\u5927\u5927\u7684\u63d0\u9ad8\u4e86\u9ad8\u5e76\u53d1\u73af\u5883\u4e0b\u7684\u5904\u7406\u80fd\u529b\u3002<\/p>\n<p>JDK8\u4e2d\u7684\u5b9e\u73b0<br \/>\n\u5b83\u6452\u5f03\u4e86Segment\uff08\u9501\u6bb5\uff09\u7684\u6982\u5ff5\uff0c\u800c\u662f\u542f\u7528\u4e86\u4e00\u79cd\u5168\u65b0\u7684\u65b9\u5f0f\u5b9e\u73b0,\u5229\u7528CAS\u7b97\u6cd5\u3002\u5b83\u6cbf\u7528\u4e86\u4e0e\u5b83\u540c\u65f6\u671f\u7684HashMap\u7248\u672c\u7684\u601d\u60f3\uff0c\u5e95\u5c42\u4f9d\u7136\u7531\u201c\u6570\u7ec4\u201d+\u94fe\u8868+\u7ea2\u9ed1\u6811\u7684\u65b9\u5f0f\u601d\u60f3(JDK7\u4e0eJDK8\u4e2dHashMap\u7684\u5b9e\u73b0)\uff0c\u4f46\u662f\u4e3a\u4e86\u505a\u5230\u5e76\u53d1\uff0c\u53c8\u589e\u52a0\u4e86\u5f88\u591a\u8f85\u52a9\u7684\u7c7b\uff0c\u4f8b\u5982TreeBin\uff0cTraverser\u7b49\u5bf9\u8c61\u5185\u90e8\u7c7b\u3002<\/p>\n<h3>2. \u5b9e\u4f8b-\u5e76\u53d1\u60c5\u51b5\u4e0b\u5b57\u7b26\u4e32\u7edf\u8ba1-\u5e94\u7528\u4e8e\u63a5\u53e3\u9650\u6d41<\/h3>\n<p>\u8bf4\u5230\u5e76\u53d1\u7684\u5b57\u7b26\u4e32\u7edf\u8ba1\uff0c\u7acb\u5373\u8ba9\u4eba\u8054\u60f3\u5230\u7684\u6570\u636e\u7ed3\u6784\u4fbf\u662fConcurrentHashpMap&lt;String,Long&gt; urlCounter;<br \/>\n\u5982\u679c\u4f60\u521a\u521a\u63a5\u89e6\u5e76\u53d1\u53ef\u80fd\u4f1a\u5199\u51fa\u5982\u4ee3\u7801\u6e05\u53551\u7684\u4ee3\u7801<\/p>\n<p>\u4ee3\u7801\u6e05\u53551\uff1a<\/p>\n<p>[java]<br \/>\npublic class CounterDemo1 {<\/p>\n<p>    private final Map&lt;String, Long&gt; urlCounter = new ConcurrentHashMap&lt;&gt;();<\/p>\n<p>    \/\/\u63a5\u53e3\u8c03\u7528\u6b21\u6570+1<br \/>\n    public long increase(String url) {<br \/>\n        Long oldValue = urlCounter.get(url);<br \/>\n        Long newValue = (oldValue == null) ? 1L : oldValue + 1;<br \/>\n        urlCounter.put(url, newValue);<br \/>\n        return newValue;<br \/>\n    }<\/p>\n<p>    \/\/\u83b7\u53d6\u8c03\u7528\u6b21\u6570<br \/>\n    public Long getCount(String url){<br \/>\n        return urlCounter.get(url);<br \/>\n    }<\/p>\n<p>    public static void main(String[] args) {<br \/>\n        ExecutorService executor = Executors.newFixedThreadPool(10);<br \/>\n        final CounterDemo1 counterDemo = new CounterDemo1();<br \/>\n        int callTime = 100000;<br \/>\n        final String url = &quot;http:\/\/localhost:8080\/hello&quot;;<br \/>\n        CountDownLatch countDownLatch = new CountDownLatch(callTime);<br \/>\n        \/\/\u6a21\u62df\u5e76\u53d1\u60c5\u51b5\u4e0b\u7684\u63a5\u53e3\u8c03\u7528\u7edf\u8ba1<br \/>\n        for(int i=0;i&lt;callTime;i++){<br \/>\n            executor.execute(new Runnable() {<br \/>\n                @Override<br \/>\n                public void run() {<br \/>\n                    counterDemo.increase(url);<br \/>\n                    countDownLatch.countDown();<br \/>\n                }<br \/>\n            });<br \/>\n        }<br \/>\n        try {<br \/>\n            countDownLatch.await();<br \/>\n        } catch (InterruptedException e) {<br \/>\n            e.printStackTrace();<br \/>\n        }<br \/>\n        executor.shutdown();<br \/>\n        \/\/\u7b49\u5f85\u6240\u6709\u7ebf\u7a0b\u7edf\u8ba1\u5b8c\u6210\u540e\u8f93\u51fa\u8c03\u7528\u6b21\u6570<br \/>\n        System.out.println(&quot;\u8c03\u7528\u6b21\u6570\uff1a&quot;+counterDemo.getCount(url));<br \/>\n    }<br \/>\n}<\/p>\n<p>console output\uff1a<br \/>\n\u8c03\u7528\u6b21\u6570\uff1a96526[\/java]<\/p>\n<p>\u90fd\u8bf4concurrentHashMap\u662f\u4e2a\u7ebf\u7a0b\u5b89\u5168\u7684\u5e76\u53d1\u5bb9\u5668\uff0c\u6240\u4ee5\u6ca1\u6709\u663e\u793a\u52a0\u540c\u6b65\uff0c\u5b9e\u9645\u6548\u679c\u5462\u5e76\u4e0d\u5982\u6240\u613f\u3002<\/p>\n<p>\u95ee\u9898\u5c31\u51fa\u5728increase\u65b9\u6cd5\uff0cconcurrentHashMap\u80fd\u4fdd\u8bc1\u7684\u662f\u6bcf\u4e00\u4e2a\u64cd\u4f5c\uff08put\uff0cget,delete\u2026\uff09\u672c\u8eab\u662f\u7ebf\u7a0b\u5b89\u5168\u7684\uff0c\u4f46\u662f\u6211\u4eec\u7684increase\u65b9\u6cd5\uff0c\u5bf9concurrentHashMap\u7684\u64cd\u4f5c\u662f\u4e00\u4e2a\u7ec4\u5408\uff0c\u5148get\u518dput\uff0c\u6240\u4ee5\u591a\u4e2a\u7ebf\u7a0b\u7684\u64cd\u4f5c\u51fa\u73b0\u4e86\u8986\u76d6\u3002\u5982\u679c\u5bf9\u6574\u4e2aincrease\u65b9\u6cd5\u52a0\u9501\uff0c\u90a3\u4e48\u53c8\u8fdd\u80cc\u4e86\u6211\u4eec\u4f7f\u7528\u5e76\u53d1\u5bb9\u5668\u7684\u521d\u8877\uff0c\u56e0\u4e3a\u9501\u7684\u5f00\u9500\u5f88\u5927\u3002\u6211\u4eec\u6709\u6ca1\u6709\u65b9\u6cd5\u6539\u5584\u7edf\u8ba1\u65b9\u6cd5\u5462\uff1f<br \/>\n\u4ee3\u7801\u6e05\u53552\u7f57\u5217\u4e86concurrentHashMap\u7236\u63a5\u53e3concurrentMap\u7684\u4e00\u4e2a\u975e\u5e38\u6709\u7528\u4f46\u662f\u53c8\u5e38\u5e38\u88ab\u5ffd\u7565\u7684\u65b9\u6cd5\u3002<\/p>\n<p>\u4ee3\u7801\u6e05\u53552\uff1a<\/p>\n<p>[java]\/**<br \/>\n * Replaces the entry for a key only if currently mapped to a given value.<br \/>\n * This is equivalent to<br \/>\n *<br \/>\n * if (map.containsKey(key) &amp;&amp; Objects.equals(map.get(key), oldValue)) {<br \/>\n *   map.put(key, newValue);<br \/>\n *   return true;<br \/>\n * } else<br \/>\n *   return false;<br \/>\n * }<br \/>\n *<br \/>\n * except that the action is performed atomically.<br \/>\n *\/<br \/>\nboolean replace(K key, V oldValue, V newValue);<br \/>\n[\/java]<\/p>\n<p>\u8fd9\u5176\u5b9e\u5c31\u662f\u4e00\u4e2a\u6700\u5178\u578b\u7684CAS\u64cd\u4f5c\uff0cexcept that the action is performed atomically.\u8fd9\u53e5\u8bdd\u771f\u662f\u5e2e\u4e86\u5927\u5fd9\uff0c\u6211\u4eec\u53ef\u4ee5\u4fdd\u8bc1\u6bd4\u8f83\u548c\u8bbe\u7f6e\u662f\u4e00\u4e2a\u539f\u5b50\u64cd\u4f5c\uff0c\u5f53A\u7ebf\u7a0b\u5c1d\u8bd5\u5728increase\u65f6\uff0c\u65e7\u503c\u88ab\u4fee\u6539\u7684\u8bdd\u5c31\u56de\u5bfc\u81f4replace\u5931\u6548\uff0c\u800c\u6211\u4eec\u53ea\u9700\u8981\u7528\u4e00\u4e2a\u5faa\u73af\uff0c\u4e0d\u65ad\u83b7\u53d6\u6700\u65b0\u503c\uff0c\u76f4\u5230\u6210\u529freplace\u4e00\u6b21\uff0c\u5373\u53ef\u5b8c\u6210\u7edf\u8ba1\u3002<\/p>\n<p>\u6539\u8fdb\u540e\u7684increase\u65b9\u6cd5\u5982\u4e0b<\/p>\n<p>\u4ee3\u7801\u6e05\u53553\uff1a<\/p>\n<p>[java]<br \/>\npublic long increase2(String url) {<br \/>\n        Long oldValue, newValue;<br \/>\n        while (true) {<br \/>\n            oldValue = urlCounter.get(url);<br \/>\n            if (oldValue == null) {<br \/>\n                newValue = 1l;<br \/>\n                \/\/\u521d\u59cb\u5316\u6210\u529f\uff0c\u9000\u51fa\u5faa\u73af<br \/>\n                if (urlCounter.putIfAbsent(url, 1l) == null)<br \/>\n                    break;<br \/>\n                \/\/\u5982\u679c\u521d\u59cb\u5316\u5931\u8d25\uff0c\u8bf4\u660e\u5176\u4ed6\u7ebf\u7a0b\u5df2\u7ecf\u521d\u59cb\u5316\u8fc7\u4e86<br \/>\n            } else {<br \/>\n                newValue = oldValue + 1;<br \/>\n                \/\/+1\u6210\u529f\uff0c\u9000\u51fa\u5faa\u73af<br \/>\n                if (urlCounter.replace(url, oldValue, newValue))<br \/>\n                    break;<br \/>\n                \/\/\u5982\u679c+1\u5931\u8d25\uff0c\u8bf4\u660e\u5176\u4ed6\u7ebf\u7a0b\u5df2\u7ecf\u4fee\u6539\u8fc7\u4e86\u65e7\u503c<br \/>\n            }<br \/>\n        }<br \/>\n        return newValue;<br \/>\n    }<\/p>\n<p>console output\uff1a<br \/>\n\u8c03\u7528\u6b21\u6570\uff1a100000[\/java]<\/p>\n<p>\u518d\u6b21\u8c03\u7528\u540e\u83b7\u5f97\u4e86\u6b63\u786e\u7684\u7ed3\u679c\uff0c\u4e0a\u8ff0\u65b9\u6848\u770b\u4e0a\u53bb\u6bd4\u8f83\u7e41\u7410\uff0c\u56e0\u4e3a\u7b2c\u4e00\u6b21\u8c03\u7528\u65f6\u9700\u8981\u8fdb\u884c\u4e00\u6b21\u521d\u59cb\u5316\uff0c\u6240\u4ee5\u591a\u4e86\u4e00\u4e2a\u5224\u65ad\uff0c\u4e5f\u7528\u5230\u4e86\u53e6\u4e00\u4e2aCAS\u64cd\u4f5cputIfAbsent\uff0c\u4ed6\u7684\u6e90\u4ee3\u7801\u63cf\u8ff0\u5982\u4e0b<\/p>\n<p>[java]<br \/>\n    \/** If the specified key is not already associated<br \/>\n     * with a value, associate it with the given value.<br \/>\n     * This is equivalent to<br \/>\n     *<br \/>\n     * if (!map.containsKey(key))<br \/>\n     *   return map.put(key, value);<br \/>\n     * else<br \/>\n     *   return map.get(key);<br \/>\n     * }[\/java]<\/p>\n<p>\u7b80\u5355\u7ffb\u8bd1\u5982\u4e0b\uff1a\u201c\u5982\u679c\uff08\u8c03\u7528\u8be5\u65b9\u6cd5\u65f6\uff09key-value \u5df2\u7ecf\u5b58\u5728\uff0c\u5219\u8fd4\u56de\u90a3\u4e2a value \u503c\u3002\u5982\u679c\u8c03\u7528\u65f6 map \u91cc\u6ca1\u6709\u627e\u5230 key \u7684 mapping\uff0c\u8fd4\u56de\u4e00\u4e2a null \u503c\u201d\u3002\u503c\u5f97\u6ce8\u610f\u70b9\u7684\u4e00\u70b9\u5c31\u662fconcurrentHashMap\u7684value\u662f\u4e0d\u80fd\u5b58\u5728null\u503c\u7684\u3002\u5b9e\u9645\u4e0a\u5462\uff0c\u4e0a\u8ff0\u7684\u65b9\u6848\u4e5f\u53ef\u4ee5\u628aLong\u66ff\u6362\u6210AtomicLong\uff0c\u53ef\u4ee5\u7b80\u5316\u5b9e\u73b0\uff0c ConcurrentHashMap<\/p>\n<p>[java]<br \/>\nprivate AtomicLongMap&lt;String&gt; urlCounter3 = AtomicLongMap.create();<\/p>\n<p>public long increase3(String url) {<br \/>\n    long newValue = urlCounter3.incrementAndGet(url);<br \/>\n    return newValue;<br \/>\n}<\/p>\n<p>public Long getCount3(String url) {<br \/>\n    return urlCounter3.get(url);[\/java]<\/p>\n<p>AtomicLong: \u5728Java\u5e76\u53d1\u5305\u4e2d\u6709\u8fd9\u6837\u4e00\u4e2a\u5305\uff0cjava.util.concurrent.atomic\uff0c\u8be5\u5305\u662f\u5bf9Java\u90e8\u5206\u6570\u636e\u7c7b\u578b\u7684\u539f\u5b50\u5c01\u88c5\uff0c\u5728\u539f\u6709\u6570\u636e\u7c7b\u578b\u7684\u57fa\u7840\u4e0a\uff0c\u63d0\u4f9b\u4e86\u539f\u5b50\u6027\u7684\u64cd\u4f5c\u65b9\u6cd5\uff0c\u4fdd\u8bc1\u4e86\u7ebf\u7a0b\u5b89\u5168\u3002<\/p>\n<p>\u53c2\u8003:<br \/>\n<a href=\"http:\/\/www.hollischuang.com\/archives\/934\">\u6df1\u5165\u7406\u89e3\u4e50\u89c2\u9501\u4e0e\u60b2\u89c2\u9501<\/a><br \/>\n<a href=\"http:\/\/pettyandydog.com\/2017\/07\/27\/concurrentHashMap\/\">\u6df1\u5165\u5e76\u53d1\u5305-ConcurrentHashMap<\/a><br \/>\n<a href=\"http:\/\/www.importnew.com\/22007.html\">ConcurrentHashMap\u603b\u7ed3<\/a><br \/>\n<a href=\"http:\/\/blog.csdn.net\/u013815546\/article\/details\/61466117\">java\u5e76\u53d1\u5b9e\u8df5&#8211;ConcurrentHashMap\u4e0eCAS<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u3001CAS:Compare and Swap,\u6bd4\u8f83\u5e76\u4ea4\u6362\u3002 1. \u60b2\u89c2\u9501\u548c\u4e50\u89c2\u9501 java.util.conc [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[19],"tags":[96],"_links":{"self":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/2086"}],"collection":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2086"}],"version-history":[{"count":4,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/2086\/revisions"}],"predecessor-version":[{"id":2107,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/2086\/revisions\/2107"}],"wp:attachment":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2086"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2086"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2086"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}