{"id":10801,"date":"2023-11-28T21:44:00","date_gmt":"2023-11-28T13:44:00","guid":{"rendered":"https:\/\/wx.kaifamiao.info\/?p=10801"},"modified":"2023-11-28T21:44:04","modified_gmt":"2023-11-28T13:44:04","slug":"java-zhong-shuo-yi-xiaatomic-de-yuan-li","status":"publish","type":"post","link":"http:\/\/wx.kaifamiao.info\/index.php\/2023\/11\/28\/java-zhong-shuo-yi-xiaatomic-de-yuan-li\/","title":{"rendered":"Java\u4e2d\u8bf4\u4e00\u4e0bAtomic\u7684\u539f\u7406\uff1f"},"content":{"rendered":"<p>Atomiclnteger\u4e2d\u7684incrementAndGet\u65b9\u6cd5\u5c31\u662f\u4e50\u89c2\u9501\u7684\u4e00\u4e2a\u5b9e\u73b0\uff0c\u4f7f\u7528\u81ea\u65cb\uff08\u5faa\u73af\u68c0\u6d4b\u66f4\u65b0\uff09 \u7684\u65b9\u5f0f\u6765\u66f4\u65b0\u5185\u5b58\u4e2d\u7684\u503c\u5e76\u901a\u8fc7\u5e95\u5c42CPU\u6267\u884c\u6765\u4fdd\u8bc1\u662f\u66f4\u65b0\u64cd\u4f5c\u662f\u539f\u5b50\u64cd\u4f5c\u3002\u65b9\u6cd5\u5982\u4e0b\uff1a<br \/>\npublic final int getAndAddlnt(Object varl, long var2, int var4) {<br \/>\nint var5;<br \/>\ndo {<br \/>\nvar5 = this.getIntVolatile(varl, var2);<br \/>\n} while (!this.compareAndSwapInt(varl, var2, var5, var5 + var4));<br \/>\n\/\/\u53ef\u4ee5\u770b\u505a compareAndSwapInt (obj, offset, expect, update)<br \/>\nreturn var5;<br \/>\n}<\/p>\n<p>\u9996\u5148\u8fd9\u4e2a\u65b9\u6cd5\u901a\u8fc7getIntVolatile\u65b9\u6cd5\uff0c\u4f7f\u7528\u5bf9\u8c61\u7684\u5f15\u7528\u4e0e\u503c\u7684\u504f\u79fb\u91cf\u5f97\u5230\u5f53\u524d\u503c\uff0c\u7136\u540e\u8c03 \u7528compareAndSwapInt\u68c0\u6d4b\u5982\u679cobj\u5185\u7684value\u548cexpect\u76f8\u7b49\uff0c\u5c31\u8bc1\u660e\u6ca1\u6709\u5176\u4ed6\u7ebf\u7a0b\u6539\u53d8\u8fc7 \u8fd9\u4e2a\u53d8\u91cf\uff0c\u90a3\u4e48\u5c31\u66f4\u65b0\u5b83\u4e3aupdate,\u5982\u679c\u8fd9\u4e00\u6b65\u7684CAS\u6ca1\u6709\u6210\u529f\uff0c\u90a3\u5c31\u91c7\u7528\u81ea\u65cb\u7684\u65b9\u5f0f\u7ee7\u7eed \u8fdb\u884cCAS\u64cd\u4f5c\u3002<br \/>\n\u5728\u8d4b\u503c\u7684\u65f6\u5019\u4fdd\u8bc1\u539f\u5b50\u64cd\u4f5c\u7684\u539f\u7406\u662f\u901a\u8fc7CPU\u7684cmpxchgl\u4e0elock\u6307\u4ee4\u7684\u652f\u6301\u6765\u5b9e\u73b0 AtomicInteger\u7684CAS\u64cd\u4f5c\u4e00\u5b9a\u7a0b\u5ea6\u4e0a\u7684\u539f\u5b50\u6027<br \/>\n\u8fd9\u4e2a\u65b9\u6cd5\u662f\u5148\u5f97\u5230\u503c\uff0c\u518d\u66f4\u65b0\u503c\uff0c\u6240\u4ee5\u5fc5\u987b\u4fdd\u8bc1\u66f4\u65b0\u7684\u503c\u662f\u5728\u539f\u6765\u7684\u57fa\u7840\u4e0a\u66f4\u65b0\u7684\uff0c\u6240\u4ee5\u91c7\u7528 CAS\u8fdb\u884c\u66f4\u65b0\uff0c\u90a3\u4e48\u4e3a\u4ec0\u4e48\u4e0d\u4f7f\u7528\u76f4\u63a5\u66f4\u65b0\u503c\u7136\u540e\u8fd4\u56de\u503c\u7684\u65b9\u5f0f\u6765\u505a\u5462\uff1f\u56e0\u4e3a\u66f4\u65b0\u503c\u7684\u524d\u63d0\u662f \u83b7\u53d6\u503c\uff0c\u8fd9\u662f\u4e24\u90e8\u6c47\u7f16\u7ea7\u522b\u7684\u64cd\u4f5c\uff0c\u4ec5\u4ec5\u66f4\u65b0\u503c\u662f\u65e0\u6cd5\u83b7\u53d6\u5230\u503c\u7684\u3002<\/p>\n<p>ABA\u95ee\u9898<br \/>\n\u4e0a\u9762\u63d0\u5230\u8fc7lock\u6307\u4ee4\uff0c\u5b83\u80fd\u4fdd\u8bc1\u5176\u4ed6CPU\u65e0\u6cd5\u53c2\u4e0e\u8fdb\u6765\uff0c\u4f46\u662f\u65e0\u6cd5\u4fdd\u8bc1\u5355\u4e2aCPU\u7684\u53e6\u4e00\u4e2a\u7ebf \u7a0b\u6267\u884c\u66f4\u65b0\u64cd\u4f5c\u3002\u6240\u4ee5\u5982\u679c\u4e00\u4e2a\u503c\u539f\u6765\u662fA,\u53d8\u6210\u4e86 B,\u53c8\u53d8\u6210\u4e86 A,\u90a3\u4e48\u4f7f\u7528CAS\u8fdb\u884c\u68c0\u67e5 \u65f6\u4f1a\u53d1\u73b0\u5b83\u7684\u503c\u6ca1\u6709\u53d1\u751f\u53d8\u5316\uff0c\u4f46\u662f\u5b9e\u9645\u4e0a\u5374\u53d8\u5316\u4e86\u3002\u8fd9\u5c31\u662fCAS\u7684ABA\u95ee\u9898\u3002<br \/>\n\u5e38\u89c1\u7684\u89e3\u51b3\u601d\u8def\u662f\u4f7f\u7528\u7248\u672c\u53f7\u3002\u5728\u53d8\u91cf\u524d\u9762\u8ffd\u52a0\u4e0a\u7248\u672c\u53f7\uff0c\u6bcf\u6b21\u53d8\u91cf\u66f4\u65b0\u7684\u65f6\u5019\u628a\u7248\u672c\u53f7\u52a0\u4e00, \u90a3\u4e48A-B-A\u5c31\u4f1a\u53d8\u62101A-2B-3A\u3002<br \/>\nAtomicStampedReference\u6765\u89e3\u51b3ABA\u95ee\u9898\u3002\u8fd9\u4e2a\u7c7b\u7684compareAndSet\u65b9\u6cd5\u4f5c\u7528\u662f\u9996\u5148\u68c0\u67e5\u5f53 \u524d\u5f15\u7528\u662f\u5426\u7b49\u4e8e\u9884\u671f\u5f15\u7528\uff0c\u5e76\u4e14\u5f53\u524d\u6807\u5fd7\u662f\u5426\u7b49\u4e8e\u9884\u671f\u6807\u5fd7\uff0c\u5982\u679c\u5168\u90e8\u76f8\u7b49\uff0c\u5219\u4ee5\u539f\u5b50\u65b9\u5f0f\u5c06 \u8be5\u5f15\u7528\u548c\u8be5\u6807\u5fd7\u7684\u503c\u8bbe\u7f6e\u4e3a\u7ed9\u5b9a\u7684\u66f4\u65b0\u503c\u3002<br \/>\n\u5faa\u73af\u65f6\u95f4\u957f\u5f00\u9500\u5927\u95ee\u9898<br \/>\n\u4e0a\u9762\u6211\u4eec\u8bf4\u8fc7\u5982\u679cCAS\u4e0d\u6210\u529f\uff0c\u5219\u4f1a\u539f\u5730\u81ea\u65cb\uff0c\u5982\u679c\u957f\u65f6\u95f4\u81ea\u65cb\u4f1a\u7ed9CPU\u5e26\u6765\u975e\u5e38\u5927\u7684\u6267\u884c\u5f00 \u9500<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Atomiclnteger\u4e2d\u7684incrementAndGet\u65b9\u6cd5\u5c31\u662f\u4e50\u89c2\u9501\u7684\u4e00\u4e2a\u5b9e\u73b0\uff0c\u4f7f\u7528\u81ea\u65cb\uff08\u5faa\u73af\u68c0\u6d4b\u66f4\u65b0\uff09 [&hellip;]<\/p>\n","protected":false},"author":7,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[118],"tags":[],"class_list":["post-10801","post","type-post","status-publish","format-standard","hentry","category-java"],"_links":{"self":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/10801","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/comments?post=10801"}],"version-history":[{"count":1,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/10801\/revisions"}],"predecessor-version":[{"id":10802,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/10801\/revisions\/10802"}],"wp:attachment":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/media?parent=10801"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/categories?post=10801"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/tags?post=10801"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}