{"id":66816,"date":"2026-01-27T16:49:40","date_gmt":"2026-01-27T08:49:40","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/66816.html"},"modified":"2026-01-27T16:49:40","modified_gmt":"2026-01-27T08:49:40","slug":"java%e5%b9%b6%e5%8f%91%e7%bc%96%e7%a8%8b-cas","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/66816.html","title":{"rendered":"Java\u5e76\u53d1\u7f16\u7a0b\u2014\u2014CAS"},"content":{"rendered":"<h2>Java\u5e76\u53d1\u7f16\u7a0b\u57fa\u77f3&#xff1a;\u6df1\u5165\u7406\u89e3CAS&#xff08;Compare-And-Swap&#xff09;<\/h2>\n<p>\u5728Java\u5e76\u53d1\u7f16\u7a0b\u4e2d&#xff0c;\u5982\u679c\u4f60\u542c\u8fc7\u4e0b\u9762\u8fd9\u4e9b\u7c7b&#xff1a;<\/p>\n<ul>\n<li>AtomicInteger \/ AtomicLong \/ AtomicReference<\/li>\n<li>ConcurrentHashMap&#xff08;1.8&#043;\u7684\u90e8\u5206\u5b9e\u73b0&#xff09;<\/li>\n<li>LongAdder \/ Striped64\u7cfb\u5217<\/li>\n<li>AQS\u4e2d\u7684\u72b6\u6001\u66f4\u65b0<\/li>\n<\/ul>\n<p>\u90a3\u4e48\u4f60\u5176\u5b9e\u5df2\u7ecf\u5927\u91cf\u95f4\u63a5\u4f7f\u7528\u8fc7 CAS \u4e86\u3002<\/p>\n<p>\u4f46\u5f88\u591a\u4eba\u53ea\u662f\u77e5\u9053\u201cCAS\u662f\u65e0\u9501\u7684\u201d\u201c\u6bd4synchronized\u8f7b\u91cf\u201d&#xff0c;\u5374\u4e0d\u6e05\u695a\u5b83\u5230\u5e95\u662f\u600e\u4e48\u5b9e\u73b0\u7684\u3001\u6709\u4ec0\u4e48\u9690\u60a3\u3001\u4ec0\u4e48\u65f6\u5019\u8be5\u7528\u3001\u4ec0\u4e48\u65f6\u5019\u575a\u51b3\u4e0d\u80fd\u7528\u3002<\/p>\n<p>\u8fd9\u7bc7\u6587\u7ae0\u5e26\u4f60\u4ece\u539f\u7406 \u2192 \u6e90\u7801 \u2192 \u4f18\u7f3a\u70b9 \u2192 \u5178\u578b\u95ee\u9898 \u2192 \u5de5\u7a0b\u5b9e\u8df5 \u5b8c\u6574\u68b3\u7406\u4e00\u904dCAS\u3002<\/p>\n<h3>\u4e00\u3001CAS\u5230\u5e95\u662f\u4ec0\u4e48&#xff1f;<\/h3>\n<p>CAS \u5168\u79f0&#xff1a;Compare And Swap&#xff08;\u6bd4\u8f83\u5e76\u4ea4\u6362&#xff09;<\/p>\n<p>\u6838\u5fc3\u601d\u60f3\u53ea\u6709\u4e00\u53e5\u8bdd&#xff1a;<\/p>\n<p>\u201c\u6211\u8ba4\u4e3a\u5f53\u524d\u503c\u5e94\u8be5\u662fA&#xff0c;\u5982\u679c\u786e\u5b9e\u662fA&#xff0c;\u5c31\u628a\u5b83\u6539\u4e3aB&#xff1b;\u5982\u679c\u4e0d\u662fA&#xff0c;\u8bf4\u660e\u522b\u4eba\u52a8\u8fc7\u4e86&#xff0c;\u6211\u4e0d\u505a\u4fee\u6539&#xff08;\u6216\u8005\u91cd\u8bd5&#xff09;\u3002\u201d<\/p>\n<p>\u7528\u4f2a\u4ee3\u7801\u8868\u793a\u5c31\u662f&#xff1a;<\/p>\n<p><span class=\"token keyword\">boolean<\/span> <span class=\"token function\">cas<\/span><span class=\"token punctuation\">(<\/span>\u5730\u5740<span class=\"token class-name\">V<\/span><span class=\"token punctuation\">,<\/span> \u671f\u671b\u503c<span class=\"token class-name\">A<\/span><span class=\"token punctuation\">,<\/span> \u65b0\u503c<span class=\"token class-name\">B<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n    <span class=\"token keyword\">if<\/span> <span class=\"token punctuation\">(<\/span>\u5185\u5b58\u4f4d\u7f6e<span class=\"token class-name\">V<\/span>\u7684\u503c <span class=\"token operator\">&#061;&#061;<\/span> <span class=\"token class-name\">A<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n        \u628a<span class=\"token class-name\">V<\/span>\u7684\u503c\u6539\u4e3a<span class=\"token class-name\">B<\/span><br \/>\n        <span class=\"token keyword\">return<\/span> <span class=\"token boolean\">true<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span> <span class=\"token keyword\">else<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n        <span class=\"token keyword\">return<\/span> <span class=\"token boolean\">false<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<p>\u5173\u952e\u70b9&#xff1a;\u4e0a\u9762\u6574\u4e2a\u201c\u6bd4\u8f83 \u2192 \u4fee\u6539\u201d\u662f\u4e00\u4e2a\u539f\u5b50\u64cd\u4f5c&#xff0c;\u7531CPU\u786c\u4ef6\u6307\u4ee4\u76f4\u63a5\u4fdd\u8bc1&#xff08;cmpxchg \/ cmpXchg \/ LL-SC \u7b49&#xff09;\u3002<\/p>\n<p>Java\u4e2d\u5e76\u6ca1\u6709\u76f4\u63a5\u66b4\u9732\u8fd9\u6761\u6307\u4ee4&#xff0c;\u800c\u662f\u901a\u8fc7 sun.misc.Unsafe&#xff08;\u540e\u6539\u4e3ajdk.internal.misc.Unsafe&#xff09;\u6765\u8c03\u7528\u3002<\/p>\n<h3>\u4e8c\u3001Java\u4e2dCAS\u662f\u600e\u4e48\u5b9e\u73b0\u7684&#xff1f;<\/h3>\n<p>\u4ee5AtomicInteger\u4e3a\u4f8b&#xff0c;\u770b\u6700\u6838\u5fc3\u7684incrementAndGet()\u65b9\u6cd5&#xff1a;<\/p>\n<p><span class=\"token keyword\">public<\/span> <span class=\"token keyword\">final<\/span> <span class=\"token keyword\">int<\/span> <span class=\"token function\">incrementAndGet<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n    <span class=\"token keyword\">return<\/span> unsafe<span class=\"token punctuation\">.<\/span><span class=\"token function\">getAndAddInt<\/span><span class=\"token punctuation\">(<\/span><span class=\"token keyword\">this<\/span><span class=\"token punctuation\">,<\/span> valueOffset<span class=\"token punctuation\">,<\/span> <span class=\"token number\">1<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token operator\">&#043;<\/span> <span class=\"token number\">1<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<p>\u518d\u5f80\u4e0b\u8ffdgetAndAddInt&#xff08;\u7b80\u5316\u7248&#xff09;&#xff1a;\u6b64\u5904\u7684compareAndSwapInt\u65b9\u6cd5\u4f1a\u91cd\u65b0\u83b7\u53d6obj \u5728 offset \u4f4d\u7f6e\u7684\u503c&#xff08;\u5373\u65b0v&#xff09;&#xff0c;\u4e0e\u4e4b\u524d\u5148\u83b7\u53d6\u7684v\u503c\u8fdb\u884c\u6bd4\u8f83&#xff0c;\u76f8\u540c\u65f6\u624d\u4f1a\u8fd4\u56de\u4e3atrue,\u5426\u5219\u4e3afalse\u3002 <font color=\"red\">\u6ce8\u610f<\/font>&#xff1a;<\/p>\n<ul>\n<li>incrementAndGet\u5e76\u4e0d\u4f1a\u4fee\u6539\u5185\u5b58\u503c&#xff0c;\u5b83\u53ea\u662f\u8c03\u7528\u4e86 getAndAddInt() \u5e76\u5728\u7ed3\u679c\u4e0a\u52a01&#xff0c;\u7136\u540e\u628a\u6700\u7ec8\u7684\u65b0\u503c\u8fd4\u56de\u7ed9\u8c03\u7528\u8005\u3002<\/li>\n<li>\u771f\u6b63\u8d1f\u8d23\u5c1d\u8bd5\u4fee\u6539\u5185\u5b58&#xff08;\u5e76\u4e14\u53ea\u6709\u5728 CAS \u6210\u529f\u65f6\u624d\u771f\u6b63\u4fee\u6539&#xff09;\u7684\u4ee3\u7801\u662f compareAndSwapInt<\/li>\n<\/ul>\n<p><span class=\"token keyword\">public<\/span> <span class=\"token keyword\">final<\/span> <span class=\"token keyword\">int<\/span> <span class=\"token function\">getAndAddInt<\/span><span class=\"token punctuation\">(<\/span><span class=\"token class-name\">Object<\/span> obj<span class=\"token punctuation\">,<\/span> <span class=\"token keyword\">long<\/span> offset<span class=\"token punctuation\">,<\/span> <span class=\"token keyword\">int<\/span> delta<span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n    <span class=\"token keyword\">int<\/span> v<span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token keyword\">do<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n        v <span class=\"token operator\">&#061;<\/span> <span class=\"token function\">getIntVolatile<\/span><span class=\"token punctuation\">(<\/span>obj<span class=\"token punctuation\">,<\/span> offset<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span>           <span class=\"token comment\">\/\/ \u8bfb\u6700\u65b0\u503c<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span> <span class=\"token keyword\">while<\/span> <span class=\"token punctuation\">(<\/span><span class=\"token operator\">!<\/span><span class=\"token function\">compareAndSwapInt<\/span><span class=\"token punctuation\">(<\/span>obj<span class=\"token punctuation\">,<\/span> offset<span class=\"token punctuation\">,<\/span> v<span class=\"token punctuation\">,<\/span> v <span class=\"token operator\">&#043;<\/span> delta<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span>  <span class=\"token comment\">\/\/ CAS\u5931\u8d25\u5c31\u91cd\u8bd5<\/span><br \/>\n    <span class=\"token keyword\">return<\/span> v<span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<p>\u53ef\u4ee5\u770b\u5230\u5178\u578b\u7684\u81ea\u65cb &#043; CAS \u6a21\u5f0f&#xff1a;<\/p>\n<li>\u5148\u8bfb\u53d6\u5f53\u524d\u503c&#xff08;volatile\u8bfb&#xff0c;\u4fdd\u8bc1\u53ef\u89c1\u6027&#xff09;<\/li>\n<li>\u8ba1\u7b97\u65b0\u503c<\/li>\n<li>\u7528CAS\u5c1d\u8bd5\u628a\u201c\u65e7\u503c\u2192\u65b0\u503c\u201d\u5199\u56de\u53bb<\/li>\n<li>\u5931\u8d25\u5c31\u56de\u5230\u7b2c1\u6b65\u91cd\u65b0\u8bfb&#xff08;\u81ea\u65cb&#xff09;<\/li>\n<p>\u8fd9\u79cd\u6a21\u5f0f\u4e5f\u88ab\u79f0\u4e3a\u4e50\u89c2\u9501&#xff08;\u5148\u5e72&#xff0c;\u51b2\u7a81\u4e86\u518d\u56de\u6eda\u91cd\u6765&#xff09;\u3002<\/p>\n<p>\u800csynchronized\u548cReentrantLock\u5c5e\u4e8e\u60b2\u89c2\u9501&#xff08;\u5148\u4e0a\u9501\u518d\u5e72&#xff09;\u3002<\/p>\n<h3>\u4e09\u3001CAS\u7684\u4f18\u7f3a\u70b9\u5bf9\u6bd4<\/h3>\n<table>\n<tr>\u7ef4\u5ea6CAS&#xff08;\u4e50\u89c2\u9501&#xff09;synchronized \/ ReentrantLock&#xff08;\u60b2\u89c2\u9501&#xff09;<\/tr>\n<tbody>\n<tr>\n<td>\u662f\u5426\u963b\u585e<\/td>\n<td>\u4e0d\u963b\u585e&#xff08;\u81ea\u65cb&#xff09;<\/td>\n<td>\u4f1a\u963b\u585e<\/td>\n<\/tr>\n<tr>\n<td>\u9ad8\u7ade\u4e89\u573a\u666f\u6027\u80fd<\/td>\n<td>\u53ef\u80fd\u9000\u5316\u4e25\u91cd&#xff08;\u5927\u91cf\u7a7a\u81ea\u65cb&#xff09;<\/td>\n<td>\u76f8\u5bf9\u7a33\u5b9a&#xff08;\u7ebf\u7a0b\u8fdb\u5165\u963b\u585e\u961f\u5217&#xff09;<\/td>\n<\/tr>\n<tr>\n<td>\u4f4e\u7ade\u4e89\u573a\u666f\u6027\u80fd<\/td>\n<td>\u6781\u9ad8&#xff08;\u51e0\u4e4e\u65e0\u4e0a\u4e0b\u6587\u5207\u6362&#xff09;<\/td>\n<td>\u8f83\u91cd&#xff08;\u81f3\u5c11\u6709\u4e00\u6b21\u52a0\u9501\/\u89e3\u9501\u5f00\u9500&#xff09;<\/td>\n<\/tr>\n<tr>\n<td>\u4ee3\u7801\u590d\u6742\u5ea6<\/td>\n<td>\u76f8\u5bf9\u590d\u6742&#xff08;\u9700\u8981\u81ea\u5df1\u5904\u7406\u91cd\u8bd5\u903b\u8f91&#xff09;<\/td>\n<td>\u7b80\u5355<\/td>\n<\/tr>\n<tr>\n<td>\u662f\u5426\u53ef\u91cd\u5165<\/td>\n<td>\u672c\u8eab\u65e0\u201c\u9501\u201d\u6982\u5ff5<\/td>\n<td>\u652f\u6301\u91cd\u5165<\/td>\n<\/tr>\n<tr>\n<td>\u662f\u5426\u652f\u6301\u6761\u4ef6\u7b49\u5f85<\/td>\n<td>\u4e0d\u652f\u6301<\/td>\n<td>\u652f\u6301&#xff08;wait\/notify\u3001Condition&#xff09;<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u4e00\u53e5\u8bdd\u603b\u7ed3\u9002\u7528\u573a\u666f&#xff1a;<\/p>\n<ul>\n<li>\u8bfb\u591a\u5199\u5c11\u3001\u4f4e\u51b2\u7a81 \u2192 CAS \u901a\u5e38\u5b8c\u80dc<\/li>\n<li>\u5199\u591a\u3001\u51b2\u7a81\u6fc0\u70c8 \u2192 synchronized \/ Lock \u5f80\u5f80\u66f4\u7a33\u5b9a<\/li>\n<\/ul>\n<h3>\u56db\u3001\u6700\u7ecf\u5178\u7684\u95ee\u9898&#xff1a;ABA\u95ee\u9898<\/h3>\n<p>\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u6808\u9876\u6307\u9488&#xff08;\u7528AtomicReference\u5b9e\u73b0&#xff09;&#xff0c;\u5f53\u524d\u662f&#xff1a;<\/p>\n<p>A \u2192 B \u2192 C<br \/>\n\u2191<br \/>\nhead<\/p>\n<p>\u7ebf\u7a0b1\u60f3\u505a pop \u64cd\u4f5c&#xff1a;\u5e0c\u671b head \u4ece A \u53d8\u6210 B<\/p>\n<p>\u6b63\u5e38\u6d41\u7a0b&#xff1a;<\/p>\n<li>\u8bfb\u5230 head &#061; A<\/li>\n<li>CAS(head, A, B) \u2192 \u6210\u529f<\/li>\n<p>\u4f46\u771f\u5b9e\u4e16\u754c\u53ef\u80fd\u53d1\u751f\u4e0b\u9762\u8fd9\u4ef6\u4e8b&#xff1a;<\/p>\n<p>\u7ebf\u7a0b1 \u8bfb\u5230 head &#061; A&#xff08;\u51c6\u5907CAS&#xff09; \u2193 \u7ebf\u7a0b2 pop \u4e86 A&#xff0c;head \u53d8\u6210 B \u7ebf\u7a0b3 push \u4e00\u4e2a\u65b0\u7684 A&#xff0c;head \u53c8\u53d8\u56de A \u2193 \u7ebf\u7a0b1 \u6267\u884c CAS(head, A, B) \u2192 \u6210\u529f&#xff01;<\/p>\n<p>\u4f46\u8fd9\u5176\u5b9e\u662f\u9519\u8bef\u7684&#xff01; \u56e0\u4e3a\u6b64\u65f6\u7684 A \u5df2\u7ecf\u4e0d\u662f\u539f\u6765\u7684 A \u4e86&#xff08;\u53ef\u80fd\u662f\u65b0\u7684\u5bf9\u8c61&#xff0c;\u7248\u672c\u4e0d\u540c&#xff09;&#xff0c;\u4f1a\u5bfc\u81f4\u6570\u636e\u7ed3\u6784\u635f\u574f\u3002<\/p>\n<p>ABA\u95ee\u9898\u672c\u8d28&#xff1a;\u53ea\u6bd4\u8f83\u201c\u503c\u201d\u662f\u5426\u76f8\u540c&#xff0c;\u800c\u6ca1\u6709\u6bd4\u8f83\u201c\u7248\u672c\u201d\u662f\u5426\u76f8\u540c\u3002<\/p>\n<h4>\u4e1a\u754c\u4e3b\u6d41\u89e3\u51b3\u65b9\u6848<\/h4>\n<li>\n<p>\u52a0\u7248\u672c\u53f7&#xff08;\u6700\u5e38\u89c1&#xff09;<\/p>\n<p>AtomicStampedReference \/ AtomicMarkableReference<\/p>\n<p> <span class=\"token comment\">\/\/ \u5e26\u7248\u672c\u6233\u7684\u5f15\u7528<\/span><br \/>\n<span class=\"token class-name\">AtomicStampedReference<\/span><span class=\"token generics\"><span class=\"token punctuation\">&lt;<\/span><span class=\"token class-name\">Node<\/span><span class=\"token punctuation\">&gt;<\/span><\/span> head <span class=\"token operator\">&#061;<\/span> <span class=\"token keyword\">new<\/span> <span class=\"token class-name\">AtomicStampedReference<\/span><span class=\"token generics\"><span class=\"token punctuation\">&lt;<\/span><span class=\"token punctuation\">&gt;<\/span><\/span><span class=\"token punctuation\">(<\/span>nodeA<span class=\"token punctuation\">,<\/span> <span class=\"token number\">0<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token comment\">\/\/ CAS\u65f6\u540c\u65f6\u68c0\u67e5\u7248\u672c<\/span><br \/>\nhead<span class=\"token punctuation\">.<\/span><span class=\"token function\">compareAndSet<\/span><span class=\"token punctuation\">(<\/span>oldRef<span class=\"token punctuation\">,<\/span> newRef<span class=\"token punctuation\">,<\/span> oldStamp<span class=\"token punctuation\">,<\/span> oldStamp <span class=\"token operator\">&#043;<\/span> <span class=\"token number\">1<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span>\n <\/li>\n<li>\n<p>\u53ea\u5173\u5fc3\u662f\u5426\u201c\u52a8\u8fc7\u201d\u800c\u4e0d\u5173\u5fc3\u5177\u4f53\u503c \u2192 \u7528AtomicMarkableReference&#xff08;\u53ea\u7528\u4e00\u4e2aboolean\u6807\u8bb0&#xff09;<\/p>\n<\/li>\n<h3>\u4e94\u3001\u5e38\u89c1\u7684CAS\u4f7f\u7528<\/h3>\n<table>\n<tr>\u573a\u666f\u63a8\u8350\u5de5\u5177\u7c7b\u5907\u6ce8<\/tr>\n<tbody>\n<tr>\n<td>\u666e\u901aint\/long\u81ea\u589e<\/td>\n<td>AtomicInteger \/ AtomicLong<\/td>\n<td>\u65e5\u5e38\u6700\u5e38\u7528<\/td>\n<\/tr>\n<tr>\n<td>\u9ad8\u5e76\u53d1\u7edf\u8ba1&#xff08;Java8&#043;&#xff09;<\/td>\n<td>LongAdder \/ DoubleAdder<\/td>\n<td>\u5206\u6bb5\u601d\u60f3&#xff0c;\u6027\u80fd\u8fdc\u8d85Atomic*<\/td>\n<\/tr>\n<tr>\n<td>boolean\u6807\u5fd7\u4f4d<\/td>\n<td>AtomicBoolean<\/td>\n<td>\u2014<\/td>\n<\/tr>\n<tr>\n<td>\u5f15\u7528\u7c7b\u578b<\/td>\n<td>AtomicReference<\/td>\n<td>\u2014<\/td>\n<\/tr>\n<tr>\n<td>\u5e26\u7248\u672c\u7684\u5f15\u7528<\/td>\n<td>AtomicStampedReference<\/td>\n<td>\u89e3\u51b3ABA<\/td>\n<\/tr>\n<tr>\n<td>\u53ea\u5173\u5fc3\u662f\u5426\u52a8\u8fc7<\/td>\n<td>AtomicMarkableReference<\/td>\n<td>\u8f7b\u91cf\u7ea7ABA\u65b9\u6848<\/td>\n<\/tr>\n<tr>\n<td>\u5bf9\u8c61\u5b57\u6bb5\u539f\u5b50\u66f4\u65b0<\/td>\n<td>AtomicIntegerFieldUpdater \u7b49<\/td>\n<td>\u4e0d\u6539\u4e1a\u52a1\u4ee3\u7801&#xff0c;\u9002\u5408\u9057\u7559\u7cfb\u7edf<\/td>\n<\/tr>\n<tr>\n<td>ConcurrentHashMap\u8ba1\u6570<\/td>\n<td>CounterCell &#043; baseCount<\/td>\n<td>\u5185\u90e8\u5927\u91cf\u4f7f\u7528LongAdder\u601d\u60f3<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u4e00\u4e2a\u5c0f\u5efa\u8bae&#xff1a;<\/p>\n<p>\u5f53\u5e76\u53d1\u91cf\u4e0d\u662f\u7279\u522b\u9ad8&#xff08;&lt; \u51e0\u767eqps&#xff09;&#xff0c;\u76f4\u63a5\u7528LongAdder\u51e0\u4e4e\u603b\u662f\u6bd4AtomicLong\u66f4\u4f18\u3002<\/p>\n<p><span class=\"token comment\">\/\/ \u63a8\u8350\u5199\u6cd5&#xff08;2024~2025\u4e3b\u6d41&#xff09;<\/span><br \/>\n<span class=\"token keyword\">private<\/span> <span class=\"token keyword\">final<\/span> <span class=\"token class-name\">LongAdder<\/span> counter <span class=\"token operator\">&#061;<\/span> <span class=\"token keyword\">new<\/span> <span class=\"token class-name\">LongAdder<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token keyword\">public<\/span> <span class=\"token keyword\">void<\/span> <span class=\"token function\">incr<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n    counter<span class=\"token punctuation\">.<\/span><span class=\"token function\">increment<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<p><span class=\"token keyword\">public<\/span> <span class=\"token keyword\">long<\/span> <span class=\"token function\">get<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n    <span class=\"token keyword\">return<\/span> counter<span class=\"token punctuation\">.<\/span><span class=\"token function\">sum<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<h3>\u516d\u3001\u603b\u7ed3\u4e00\u53e5\u8bdd<\/h3>\n<p>CAS\u662fJava\u5e76\u53d1\u5305&#xff08;JUC&#xff09;\u6700\u6838\u5fc3\u7684\u539f\u8bed\u4e4b\u4e00&#xff0c;\u5b83\u8ba9\u201c\u65e0\u9501\u7f16\u7a0b\u201d\u6210\u4e3a\u53ef\u80fd&#xff0c;\u4f46\u4e5f\u5e26\u6765\u4e86\u81ea\u65cb\u5f00\u9500\u3001ABA\u9690\u60a3\u7b49\u5de5\u7a0b\u4ee3\u4ef7\u3002<\/p>\n<p>\u771f\u6b63\u7684\u9ad8\u624b\u4e0d\u662f\u201c\u80fd\u4e0d\u80fd\u7528CAS\u201d&#xff0c;\u800c\u662f\u201c\u5728\u4ec0\u4e48\u573a\u666f\u4e0b\u7528\u54ea\u79cd\u53d8\u4f53\u7684CAS\u6700\u5408\u9002\u201d\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Java\u5e76\u53d1\u7f16\u7a0b\u57fa\u77f3&#xff1a;\u6df1\u5165\u7406\u89e3CAS&#xff08;Compare-And-Swap&#xff09;<br \/>\n\u5728Java\u5e76\u53d1\u7f16\u7a0b\u4e2d&#xff0c;\u5982\u679c\u4f60\u542c\u8fc7\u4e0b\u9762\u8fd9\u4e9b\u7c7b&#xff1a;<br \/>\nAtomicInteger \/ AtomicLong \/ AtomicReferenceConcurrentHashMap&#xff08;1.8\u7684\u90e8\u5206\u5b9e\u73b0&#xff09;LongAdder \/ Striped64\u7cfb\u5217AQS\u4e2d\u7684\u72b6\u6001\u66f4\u65b0<br \/>\n\u90a3\u4e48\u4f60\u5176\u5b9e\u5df2\u7ecf\u5927\u91cf\u95f4\u63a5\u4f7f\u7528\u8fc7 CAS \u4e86\u3002<br \/>\n\u4f46<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[99,426,190],"topic":[],"class_list":["post-66816","post","type-post","status-publish","format-standard","hentry","category-server","tag-java","tag-jvm","tag-190"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Java\u5e76\u53d1\u7f16\u7a0b\u2014\u2014CAS - \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\/66816.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Java\u5e76\u53d1\u7f16\u7a0b\u2014\u2014CAS - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"Java\u5e76\u53d1\u7f16\u7a0b\u57fa\u77f3&#xff1a;\u6df1\u5165\u7406\u89e3CAS&#xff08;Compare-And-Swap&#xff09; \u5728Java\u5e76\u53d1\u7f16\u7a0b\u4e2d&#xff0c;\u5982\u679c\u4f60\u542c\u8fc7\u4e0b\u9762\u8fd9\u4e9b\u7c7b&#xff1a; AtomicInteger \/ AtomicLong \/ AtomicReferenceConcurrentHashMap&#xff08;1.8\u7684\u90e8\u5206\u5b9e\u73b0&#xff09;LongAdder \/ Striped64\u7cfb\u5217AQS\u4e2d\u7684\u72b6\u6001\u66f4\u65b0 \u90a3\u4e48\u4f60\u5176\u5b9e\u5df2\u7ecf\u5927\u91cf\u95f4\u63a5\u4f7f\u7528\u8fc7 CAS \u4e86\u3002 \u4f46\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/66816.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2026-01-27T08:49:40+00:00\" \/>\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=\"2 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/66816.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/66816.html\",\"name\":\"Java\u5e76\u53d1\u7f16\u7a0b\u2014\u2014CAS - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-01-27T08:49:40+00:00\",\"dateModified\":\"2026-01-27T08:49:40+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/66816.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/66816.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/66816.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Java\u5e76\u53d1\u7f16\u7a0b\u2014\u2014CAS\"}]},{\"@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":"Java\u5e76\u53d1\u7f16\u7a0b\u2014\u2014CAS - \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\/66816.html","og_locale":"zh_CN","og_type":"article","og_title":"Java\u5e76\u53d1\u7f16\u7a0b\u2014\u2014CAS - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"Java\u5e76\u53d1\u7f16\u7a0b\u57fa\u77f3&#xff1a;\u6df1\u5165\u7406\u89e3CAS&#xff08;Compare-And-Swap&#xff09; \u5728Java\u5e76\u53d1\u7f16\u7a0b\u4e2d&#xff0c;\u5982\u679c\u4f60\u542c\u8fc7\u4e0b\u9762\u8fd9\u4e9b\u7c7b&#xff1a; AtomicInteger \/ AtomicLong \/ AtomicReferenceConcurrentHashMap&#xff08;1.8\u7684\u90e8\u5206\u5b9e\u73b0&#xff09;LongAdder \/ Striped64\u7cfb\u5217AQS\u4e2d\u7684\u72b6\u6001\u66f4\u65b0 \u90a3\u4e48\u4f60\u5176\u5b9e\u5df2\u7ecf\u5927\u91cf\u95f4\u63a5\u4f7f\u7528\u8fc7 CAS \u4e86\u3002 \u4f46","og_url":"https:\/\/www.wsisp.com\/helps\/66816.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-01-27T08:49:40+00:00","author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"2 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/66816.html","url":"https:\/\/www.wsisp.com\/helps\/66816.html","name":"Java\u5e76\u53d1\u7f16\u7a0b\u2014\u2014CAS - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-01-27T08:49:40+00:00","dateModified":"2026-01-27T08:49:40+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/66816.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/66816.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/66816.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"Java\u5e76\u53d1\u7f16\u7a0b\u2014\u2014CAS"}]},{"@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\/66816","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=66816"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/66816\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=66816"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=66816"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=66816"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=66816"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}