{"id":235,"date":"2018-09-18T22:37:21","date_gmt":"2018-09-18T14:37:21","guid":{"rendered":"http:\/\/kaispace.cn\/?p=235"},"modified":"2018-09-18T22:37:21","modified_gmt":"2018-09-18T14:37:21","slug":"java%e5%a4%9a%e7%ba%bf%e7%a8%8b%e9%80%9a%e4%bf%a1%ef%bc%883%ef%bc%89%e4%b9%8bcas%e6%9c%ba%e5%88%b6","status":"publish","type":"post","link":"https:\/\/blog.kaispace.cn\/?p=235","title":{"rendered":"Java\u591a\u7ebf\u7a0b\u901a\u4fe1\uff083\uff09\u4e4bCAS\u673a\u5236"},"content":{"rendered":"<p><strong>\u4ec0\u4e48\u662fCAS\uff1f<\/strong><\/p>\n<p>CAS\u662f\u82f1\u6587\u5355\u8bcd<strong>Compare And Swap<\/strong>\u7684\u7f29\u5199\uff0c\u7ffb\u8bd1\u8fc7\u6765\u5c31\u662f\u6bd4\u8f83\u5e76\u66ff\u6362\u3002<\/p>\n<p>CAS\u673a\u5236\u5f53\u4e2d\u4f7f\u7528\u4e863\u4e2a\u57fa\u672c\u64cd\u4f5c\u6570\uff1a\u5185\u5b58\u5730\u5740V\uff0c\u65e7\u7684\u9884\u671f\u503cA\uff0c\u8981\u4fee\u6539\u7684\u65b0\u503cB\u3002<\/p>\n<p>\u66f4\u65b0\u4e00\u4e2a\u53d8\u91cf\u7684\u65f6\u5019\uff0c\u53ea\u6709\u5f53\u53d8\u91cf\u7684\u9884\u671f\u503cA\u548c\u5185\u5b58\u5730\u5740V\u5f53\u4e2d\u7684\u5b9e\u9645\u503c\u76f8\u540c\u65f6\uff0c\u624d\u4f1a\u5c06\u5185\u5b58\u5730\u5740V\u5bf9\u5e94\u7684\u503c\u4fee\u6539\u4e3aB\u3002\uff08\u901a\u8fc7\u81ea\u65cb\u4e0d\u65ad\u83b7\u53d6\u4fee\u6539\u76f4\u81f3\u5185\u5b58\u4e2d\u7684\u53d8\u91cf\u503c\u4e0e\u9884\u671f\u4e2d\u7684\u503c\u4e00\u81f4\u624d\u8df3\u51fa\uff09<\/p>\n<p>\u76f8\u6bd4\u8f83\u4e8esynchronized\u91c7\u7528<span style=\"color: #ff0000;\">\u60b2\u89c2\u9501<\/span>\uff0c\u9ed8\u8ba4\u5e76\u53d1\u4e25\u91cd\uff0cCAS\u91c7\u7528\u4e00\u79cd<span style=\"color: #ff0000;\">\u4e50\u89c2\u9501<\/span>\u7684\u5c5e\u6027\uff0c\u4e0d\u65ad\u8ba9\u7ebf\u7a0b\u8fdb\u884c\u5c1d\u8bd5\uff0c<span style=\"color: #ff0000;\">\u9002\u7528\u4e8e\u5e76\u53d1\u91cf\u6bd4\u8f83\u5c0f<\/span>\u7684\u60c5\u51b5\u3002<\/p>\n<p>CAS\u6027\u80fd\u5728\u5e76\u53d1\u91cf\u8f83\u5c0f\u65f6\u8f83\u4f18\u7684\u539f\u56e0\u662f\u5c11\u4e86\u9501\u961f\u5217\u7684\u7ef4\u62a4\u4ee5\u53ca\u5bf9\u4e0a\u9501\u89e3\u9501\u7b49\u8fc7\u7a0b\uff0c\u4f46\u5047\u5982\u7ebf\u7a0b\u8fc7\u591a\uff0c\u9891\u7e41\u7684\u5c1d\u8bd5\u4f1a\u5bfc\u81f4\u6027\u80fd\u8f83\u5dee\u3002<\/p>\n<p>CAS\u4e3b\u8981\u540c\u4e8ejava.util.concurrent.atomic\u5305\u4e2d\u7684\u7c7b\u4ee5\u53caLock\u4e2d\u7684\u5e95\u5c42\u5b9e\u73b0\u3002\uff08synchronized\u57281.6\u7248\u672c\u524d\u4e5f\u7528\u8fc7CAS\u673a\u5236\uff09<\/p>\n<p>&nbsp;<\/p>\n<blockquote><p><strong>CAS\u7684\u7f3a\u70b9\uff1a<\/strong><\/p>\n<p><strong>1.CPU\u5f00\u9500\u8f83\u5927<\/strong><\/p>\n<p>\u5728\u5e76\u53d1\u91cf\u6bd4\u8f83\u9ad8\u7684\u60c5\u51b5\u4e0b\uff0c\u5982\u679c\u8bb8\u591a\u7ebf\u7a0b\u53cd\u590d\u5c1d\u8bd5\u66f4\u65b0\u67d0\u4e00\u4e2a\u53d8\u91cf\uff0c\u5374\u53c8\u4e00\u76f4\u66f4\u65b0\u4e0d\u6210\u529f\uff0c\u5faa\u73af\u5f80\u590d\uff0c\u4f1a\u7ed9CPU\u5e26\u6765\u5f88\u5927\u7684\u538b\u529b\u3002<\/p>\n<p><strong>2.\u4e0d\u80fd\u4fdd\u8bc1\u4ee3\u7801\u5757\u7684\u539f\u5b50\u6027<\/strong><\/p>\n<p>CAS\u673a\u5236\u6240\u4fdd\u8bc1\u7684\u53ea\u662f\u4e00\u4e2a\u53d8\u91cf\u7684\u539f\u5b50\u6027\u64cd\u4f5c\uff0c\u800c\u4e0d\u80fd\u4fdd\u8bc1\u6574\u4e2a\u4ee3\u7801\u5757\u7684\u539f\u5b50\u6027\u3002\u6bd4\u5982\u9700\u8981\u4fdd\u8bc13\u4e2a\u53d8\u91cf\u5171\u540c\u8fdb\u884c\u539f\u5b50\u6027\u7684\u66f4\u65b0\uff0c\u5c31\u4e0d\u5f97\u4e0d\u4f7f\u7528Synchronized\u4e86\u3002<\/p>\n<p><strong>3.ABA\u95ee\u9898<\/strong><\/p>\n<p>\u8fd9\u662fCAS\u673a\u5236\u6700\u5927\u7684\u95ee\u9898\u6240\u5728\u3002<\/p>\n<p>1.A=&gt;B \u66f4\u65b0\u6210\u529f=&gt;<span style=\"color: #ff0000;\">2.A=&gt;B\u88ab\u963b\u585e\u66f4\u65b0\u5931\u8d25<\/span>=&gt;3.B=&gt;A\u66f4\u65b0\u6210\u529f=&gt;<span style=\"color: #ff0000;\">2.A=&gt;B\u66f4\u65b0\u6210\u529f<\/span><\/p>\n<p>\u7ebf\u7a0b2\u5c5e\u4e8e\u591a\u4f59\u64cd\u4f5c\uff0c<span style=\"color: #ff0000;\">\u5bf9\u540c\u4e00\u64cd\u4f5c\u8fdb\u884c\u4e8c\u6b21\u6267\u884c<\/span>\uff0c\u5bfc\u81f4\u6b63\u5e38\u64cd\u4f5c3\u88ab\u62b5\u6d88\u3002<\/p>\n<p>\u4f8b\u5982\u4f60\u672c\u6765\u6709100\u5757\u94b1\uff0c\u82b1\u4e8650\u5757\u94b1\uff0c\u540e\u518d\u5b58\u516550\uff0c\u6b64\u65f6\u4f59\u989d\u5e94\u8be5\u662f100\uff0c\u4f46\u7531\u4e8e\u6b65\u9aa41\u7684\u626350\u6267\u884c\u4e86\u4e24\u6b21\uff0c\u56e0\u6b64\u4f59\u989d\u53ea\u526950.<\/p><\/blockquote>\n<p><a href=\"https:\/\/mp.weixin.qq.com\/s\/nRnQKhiSUrDKu3mz3vItWg\">CAS\u673a\u5236<\/a><\/p>\n<p>\u4e3a\u89e3\u51b3ABA\u95ee\u9898\uff0c<strong>AtomicStampedReference<\/strong>\u7c7b\u589e\u52a0\u4e86\u7248\u672c\u53f7\u8fd9\u4e00\u4e2a\u5224\u65ad\u6761\u4ef6\uff0c\u9632\u6b62\u540c\u4e00\u64cd\u4f5c\u591a\u6b21\u6267\u884c\u7684\u9519\u8bef\u3002<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4ec0\u4e48\u662fCAS\uff1f CAS\u662f\u82f1\u6587\u5355\u8bcdCompare And Swap\u7684\u7f29\u5199\uff0c\u7ffb\u8bd1\u8fc7\u6765\u5c31\u662f\u6bd4\u8f83\u5e76\u66ff\u6362\u3002 CAS\u673a\u5236\u5f53&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[8],"tags":[],"_links":{"self":[{"href":"https:\/\/blog.kaispace.cn\/index.php?rest_route=\/wp\/v2\/posts\/235"}],"collection":[{"href":"https:\/\/blog.kaispace.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.kaispace.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.kaispace.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.kaispace.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=235"}],"version-history":[{"count":0,"href":"https:\/\/blog.kaispace.cn\/index.php?rest_route=\/wp\/v2\/posts\/235\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.kaispace.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=235"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.kaispace.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=235"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.kaispace.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=235"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}