{"id":3936,"date":"2019-02-27T16:51:43","date_gmt":"2019-02-27T08:51:43","guid":{"rendered":"http:\/\/www.xttblog.com\/?p=3936"},"modified":"2019-02-27T16:51:43","modified_gmt":"2019-02-27T08:51:43","slug":"cas%e9%9d%9e%e9%94%81%e5%ae%9e%e7%8e%b0%e5%8d%95%e4%be%8b%e7%9a%84%e4%b8%80%e4%b8%aa%e7%bc%ba%e9%99%b7","status":"publish","type":"post","link":"https:\/\/www.xttblog.com\/?p=3936","title":{"rendered":"CAS\u975e\u9501\u5b9e\u73b0\u5355\u4f8b\u7684\u4e00\u4e2a\u7f3a\u9677"},"content":{"rendered":"<p>\n\t\u6700\u8fd1\u53c8\u662f\u4e00\u5e74\u65b0\u6625\u7684\u9762\u8bd5\u5b63\uff0c\u6709\u4eba\u8bf4\u8fd9\u662f\u91d1\u4e09\u94f6\u56db\u3002\u4f46\u662f\u8bf4\u5230\u9762\u8bd5\uff0c\u5e76\u53d1\u548c\u9501\u80af\u5b9a\u662f\u5c11\u4e0d\u4e86\u7684\u3002\u5173\u4e8e\u5e76\u53d1\u53ef\u4ee5\u8bbf\u95ee\u6211\u7684\u8fd9\u7bc7\u6587\u7ae0\uff1a<a href=\"https:\/\/www.xttblog.com\/?p=3922\">\u6781\u5ba2\u65f6\u95f4\u300aJava\u5e76\u53d1\u7f16\u7a0b\u5b9e\u6218\u300b\u8d2d\u4e70\u8fd4\u73b024<\/a>\uff0c\u4eca\u5929\u6211\u4eec\u8981\u8bf4\u7684\u662f\uff0c\u65e0\u9501\u5b9e\u73b0\u5355\u4f8b\u6a21\u5f0f\uff0c\u4ee5\u53ca\u8fd9\u79cd CAS \u5b9e\u73b0\u7684\u5355\u4f8b\u7684\u7f3a\u70b9\u3002\n<\/p>\n<p>\n\t\u4f20\u7edf\u7684 7 \u79cd\u5355\u4f8b\u6a21\u5f0f\u5927\u81f4\u5982\u4e0b\uff1a\n<\/p>\n<p>\n\t<img decoding=\"async\" alt=\"\u5355\u4f8b\u6a21\u5f0f\u539f\u7406\" src=\"https:\/\/images.weserv.nl\/?url=https:\/\/wx1.sinaimg.cn\/large\/8fa5dcfcgy1g0l2u0vrppj20im095my6.jpg\" style=\"width: 670px;height: 329px\" \/>\n<\/p>\n<p>\n\t\u5b83\u4eec\u90fd\u662f\u7528\u9501\u6765\u5b9e\u73b0\u3002\u4f46\u662f\u5982\u679c\u5728\u9762\u8bd5\u8fc7\u7a0b\u4e2d\u9762\u8bd5\u5b98\u95ee\u4f60\u5982\u4f55\u4f7f\u7528\u975e\u9501\u6765\u5b9e\u73b0\u4e00\u4e2a\u5355\u4f8b\u5462\uff1f\n<\/p>\n<p>\n\t\u7b54\u6848\u5c31\u662f\u4e0b\u56fe\u8fd9\u79cd\u5b9e\u73b0\u3002\n<\/p>\n<pre class=\"brush:java;\">\r\npublic class Singleton {\r\n    private static final AtomicReference&lt;Singleton&gt; INSTANCE\r\n            = new AtomicReference&lt;Singleton&gt;();\r\n    private Singleton() {\r\n        System.out.println(&quot;\u6211\u88ab\u521d\u59cb\u5316\u4e86&quot;);\r\n        CasSingletonTest.objectcount.getAndIncrement();\r\n    }\r\n    public static Singleton getInstance() {\r\n        for (;;) {\r\n            Singleton singleton = INSTANCE.get();\r\n            if (null != singleton) {\r\n                return singleton;\r\n            }\r\n            singleton = new Singleton();\r\n            if (INSTANCE.compareAndSet(null, singleton)) {\r\n                return singleton;\r\n            }\r\n        }\r\n    }\r\n}<\/pre>\n<p>\n\t\u8fd9\u662f\u7f51\u4e0a\u4e00\u4f4d\u5927\u725b\u7684\u5b9e\u73b0\uff0c\u4ed6\u7684\u8fd9\u79cd\u975e\u9501 CAS \u5b9e\u73b0\u7684\u5355\u4f8b\uff0c\u633a\u597d\u7684\u3002\u4f46\u662f\u5e73\u65f6\u53ef\u80fd\u6ca1\u6709\u4eba\u4f7f\u7528\uff0c\u6bd4\u7528\u9501\u7a0d\u5fae\u590d\u6742\u4e86\u4e00\u70b9\uff0c\u8fd9\u4e5f\u662f\u4e3a\u4ec0\u4e48\u6ca1\u6709\u88ab\u5217\u5165\u5355\u4f8b\u6a21\u5f0f\u7684 7 \u5927\u5199\u6cd5\u4e4b\u4e2d\u4e86\u3002\u6211\u5728\u4ed6\u7684\u57fa\u7840\u4e0a\uff0c\u4e5f\u5c31\u662f\u4ed6\u7684\u6784\u9020\u65b9\u6cd5\u91cc\u6dfb\u52a0\u4e86\u4e24\u884c\u4ee3\u7801\u3002\n<\/p>\n<pre class=\"brush:java;\">\r\nSystem.out.println(&quot;\u6211\u88ab\u521d\u59cb\u5316\u4e86&quot;);\r\nCasSingletonTest.objectcount.getAndIncrement();<\/pre>\n<p>\n\t\u6211\u4e3b\u8981\u662f\u60f3\u770b\u770b\u5b83\u5230\u5e95\u662f\u5b9e\u4f8b\u5316\u4e86\u51e0\u6b21\u3002\u52a0\u4e0a\u8fd9\u4e24\u884c\u4ee3\u7801\uff0c\u53ef\u4ee5\u65b9\u4fbf\u6211\u89c2\u5bdf\u63a7\u5236\u53f0\uff0c\u548c\u7edf\u8ba1\u5b9e\u4f8b\u5316\u7684\u603b\u6b21\u6570\u3002\n<\/p>\n<p>\n\t\u7136\u540e\uff0c\u6211\u7684\u6d4b\u8bd5\u4ee3\u7801\u5982\u4e0b\uff1a\n<\/p>\n<pre class=\"brush:java;\">\r\npackage com.xttblog.canal.test;\r\nimport java.util.concurrent.CountDownLatch;\r\nimport java.util.concurrent.atomic.AtomicInteger;\r\n\/**\r\n * CasSingletonTest\r\n * @author www.xttblog.com\r\n * @date 2019\/2\/27 \u4e0b\u53482:39\r\n *\/\r\npublic class CasSingletonTest {\r\n    public static AtomicInteger objectcount = new AtomicInteger();\r\n    public static void main(String[] args) throws InterruptedException {\r\n        final CountDownLatch begin = new CountDownLatch(1);\r\n        final CountDownLatch last = new CountDownLatch(1000);\r\n        for(int i=0;i&lt;1000;i++){\r\n            new Thread(new Runnable() {\r\n                @Override\r\n                public void run() {\r\n                    try {\r\n                        begin.await();\r\n                        System.out.println(Thread.currentThread().getName()+&quot;:begin...&quot;);\r\n                        Singleton sba = Singleton.getInstance();\r\n                        System.out.println(Thread.currentThread().getName()+&quot;:OK&quot;);\r\n                        last.countDown();\r\n                    } catch (InterruptedException ex) {\r\n                        ex.printStackTrace();\r\n                    }\r\n                }\r\n            }).start();\r\n        }\r\n        begin.countDown();\r\n        last.await();\r\n        System.out.println(&quot;new objects: &quot;+objectcount.get());\r\n    }\r\n}<\/pre>\n<p>\n\t\u5173\u4e8e&nbsp;CountDownLatch \u6709\u4e0d\u4f1a\u7684\uff0c\u53ef\u4ee5\u770b\u6211\u7684\u300a<a href=\"https:\/\/www.xttblog.com\/?p=2844\">CountDownLatch \u538b\u6d4b\u6559\u7a0b<\/a>\u300b\u4e00\u6587\u3002\n<\/p>\n<p>\n\t\u6211\u8fd9\u91cc\u4e3b\u8981\u662f\u60f3\u538b\u6d4b\u4e00\u4e0b\uff0c\u975e\u9501 CAS \u5355\u4f8b\u6a21\u5f0f\u662f\u5426\u4f1a\u521b\u5efa\u591a\u6b21\u5bf9\u8c61\u3002\n<\/p>\n<p>\n\t\u8fd0\u884c\u4e0a\u9762\u7684 main \u65b9\u6cd5\uff0c\u6211\u622a\u56fe\u4e86\u4e00\u4e0b\u6700\u7ec8\u7ed3\u679c\u3002\n<\/p>\n<p>\n\t<img decoding=\"async\" alt=\"\u65e0\u9501\u5b9e\u73b0\u5355\u4f8b\" src=\"https:\/\/images.weserv.nl\/?url=https:\/\/wx1.sinaimg.cn\/large\/8fa5dcfcgy1g0l2wrnalfj208f03qt90.jpg\" style=\"width: 303px;height: 134px\" \/>\n<\/p>\n<p>\n\t\u7ed3\u8bba\uff1aCAS \u4ee5\u539f\u5b50\u65b9\u5f0f\u66f4\u65b0\u5185\u5b58\u4e2d\u76f8\u5e94\u7684\u503c\uff0c\u4ece\u800c\u4fdd\u8bc1\u4e86\u591a\u7ebf\u7a0b\u73af\u5883\u4e0b\u5171\u4eab\u53d8\u91cf\u66f4\u65b0\u64cd\u4f5c\u7684\u540c\u6b65\u3002\u7684\u786e\uff0c\u8fd9\u79cd\u65b9\u5f0f\u53ef\u4ee5\u4fdd\u8bc1\u6bcf\u6b21\u8c03\u7528getInstance() \u65b9\u6cd5\u5f97\u5230\u7684\u4e00\u5b9a\u662f\u540c\u4e00\u4e2a\u5b9e\u4f8b\u3002\u56e0\u6b64\uff0c\u4ece\u529f\u80fd\u5b9e\u73b0\u7684\u89d2\u5ea6\u6765\u770b\uff0c\u8fd9\u79cd\u505a\u6cd5\u8fbe\u5230\u4e86\u9884\u671f\u7684\u76ee\u7684\u3002\u4f46\u662f\uff0c\u7ecf\u8fc7\u5206\u6790\u548c\u6d4b\u8bd5\uff0c\u5374\u53d1\u73b0\u8fd9\u79cd\u65b9\u5f0f\u6709\u4e00\u4e9b\u9884\u671f\u4e4b\u5916\u7684\u5f0a\u75c5\uff1a\u53ef\u80fd\u4f1a\u521b\u5efa\u4e0d\u6b62\u4e00\u4e2a\u5bf9\u8c61\u3002\n<\/p>\n<p>\n\tCAS \u672c\u8eab\u7684\u64cd\u4f5c\u7684\u786e\u662f\u539f\u5b50\u65b9\u5f0f\uff0c\u4f46\u662f\u5305\u88c5 CAS \u6307\u4ee4\u7684\u65b9\u6cd5\u5e76\u975e\u662f\u5168\u7a0b\u540c\u6b65\u7684\uff0c\u5f53\u7136\uff0c\u5728\u5305\u542b CAS \u6307\u4ee4\u7684\u65b9\u6cd5\u5f00\u59cb\u8c03\u7528\u4e4b\u524d\uff0c\u53c2\u6570\u8ba1\u7b97\u8fc7\u7a0b\u4e2d\u66f4\u4e0d\u662f\u4e92\u65a5\u6267\u884c\u7684\uff01\u5f53\u4e00\u4e2a\u7ebf\u7a0b\u6d4b\u8bd5 instance.get() == null \u5f97\u5230 true \u4e4b\u540e\uff0c\u5f80\u4e0b\u5b83\u5c31\u4e00\u5b9a\u4f1a\u8c03\u7528 new Singleton()\u3002\u56e0\u4e3a\uff0c\u8fd9\u5e76\u4e0d\u662f CAS \u65b9\u6cd5\u7684\u4e00\u90e8\u5206\uff0c\u800c\u662f\u5b83\u7684\u53c2\u6570\u3002\u5728\u8c03\u7528\u4e00\u4e2a\u65b9\u6cd5\u4e4b\u524d\uff0c\u9700\u8981\u5148\u5c06\u5176\u53c2\u6570\u538b\u5165\u6808\uff0c\u5f53\u7136\uff0c\u9700\u8981\u5148\u8ba1\u7b97\u53c2\u6570\u8868\u8fbe\u5f0f\uff0c\u56e0\u6b64\uff0c\u4ea7\u751f\u5982\u4e0a\u7ed3\u679c\u4e5f\u5c31\u4e0d\u96be\u9884\u6599\u4e86\u3002\n<\/p>\n<p>\n\tCAS \u4e0e\u9501\u7684\u533a\u522b\u5728\u4e8e\uff0c\u5b83\u662f\u975e\u963b\u585e\u7684\uff0c\u4e5f\u5c31\u662f\u8bf4\uff0c\u5b83\u4e0d\u4f1a\u53bb\u7b49\u5f85\u4e00\u4e2a\u6761\u4ef6\uff0c\u800c\u662f\u4e00\u5b9a\u4f1a\u53bb\u6267\u884c\uff0c\u7ed3\u679c\u8981\u4e48\u6210\u529f\uff0c\u8981\u4e48\u5931\u8d25\u3002\u5b83\u7684\u64cd\u4f5c\u65f6\u95f4\u662f\u53ef\u9884\u671f\u7684\u3002\u5982\u679c\u6211\u4eec\u7684\u76ee\u7684\u662f\u4e00\u5b9a\u8981\u6210\u529f\u6267\u884c CAS\uff0c\u90a3\u5c31\u9700\u8981\u4e0d\u65ad\u5faa\u73af\u6267\u884c\u76f4\u81f3\u6210\u529f\uff0c\u540c\u65f6\uff0c\u5efa\u7acb\u5728\u6210\u529f\u9884\u671f\u4e4b\u4e0a\u5927\u91cf\u7684\u51c6\u5907\u5de5\u4f5c\u662f\u503c\u5f97\u7684\uff0c\u4f46\u662f\uff0c\u5982\u679c\u6211\u4eec\u4e0d\u5e0c\u671b\u64cd\u4f5c\u4e00\u5b9a\u6210\u529f\uff0c\u90a3\u4e3a\u6210\u529f\u64cd\u4f5c\u800c\u505a\u7684\u51c6\u5907\u5de5\u4f5c\u5c31\u6d6a\u8d39\u6389\u4e86\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=3936\">CAS\u975e\u9501\u5b9e\u73b0\u5355\u4f8b\u7684\u4e00\u4e2a\u7f3a\u9677<\/a><\/p><!-- <div class=\"bd-reward-stl\"><button id=\"bdRewardBtn\"><span><\/span><\/button><\/div> -->","protected":false},"excerpt":{"rendered":"<p>\u6700\u8fd1\u53c8\u662f\u4e00\u5e74\u65b0\u6625\u7684\u9762\u8bd5\u5b63\uff0c\u6709\u4eba\u8bf4\u8fd9\u662f\u91d1\u4e09\u94f6\u56db\u3002\u4f46\u662f\u8bf4\u5230\u9762\u8bd5\uff0c\u5e76\u53d1\u548c\u9501\u80af\u5b9a\u662f\u5c11\u4e0d\u4e86\u7684\u3002\u5173\u4e8e\u5e76\u53d1\u53ef\u4ee5\u8bbf\u95ee\u6211\u7684\u8fd9\u7bc7\u6587 [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[1740,710,1753,1644,1143],"class_list":["post-3936","post","type-post","status-publish","format-standard","hentry","category-java","tag-atomicinteger","tag-atomicreference","tag-cas","tag-countdownlatch","tag-1143"],"_links":{"self":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/3936","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\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3936"}],"version-history":[{"count":1,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/3936\/revisions"}],"predecessor-version":[{"id":3937,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/3936\/revisions\/3937"}],"wp:attachment":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3936"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3936"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3936"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}