{"id":8709,"date":"2023-11-28T20:37:37","date_gmt":"2023-11-28T12:37:37","guid":{"rendered":"https:\/\/wx.kaifamiao.info\/?p=8709"},"modified":"2023-11-28T20:37:43","modified_gmt":"2023-11-28T12:37:43","slug":"java-zhong-yuan-zi-lei-de-chang-yong-lei","status":"publish","type":"post","link":"http:\/\/wx.kaifamiao.info\/index.php\/2023\/11\/28\/java-zhong-yuan-zi-lei-de-chang-yong-lei\/","title":{"rendered":"Java\u4e2d\u539f\u5b50\u7c7b\u7684\u5e38\u7528\u7c7b\uff1f"},"content":{"rendered":"<p>\u539f\u5b50\u64cd\u4f5c\u662f\u6307\u4e0d\u4f1a\u88ab\u7ebf\u7a0b\u8c03\u5ea6\u673a\u5236\u6253\u65ad\u7684\u64cd\u4f5c\uff0c\u8fd9\u79cd\u64cd\u4f5c\u4e00\u65e6\u5f00\u59cb\uff0c\u5c31\u4e00\u76f4\u8fd0\u884c\u5230\u7ed3\u675f\uff0c\u4e2d\u95f4\u4e0d\u4f1a\u6709\u4efb\u4f55\u7ebf\u7a0b\u4e0a\u4e0b\u6587\u5207\u6362\u3002<br \/>\n\u539f\u5b50\u64cd\u4f5c\u53ef\u4ee5\u662f\u4e00\u4e2a\u6b65\u9aa4\uff0c\u4e5f\u53ef\u4ee5\u662f\u591a\u4e2a\u64cd\u4f5c\u6b65\u9aa4\uff0c\u4f46\u662f\u5176\u987a\u5e8f\u4e0d\u53ef\u4ee5\u88ab\u6253\u4e71\uff0c\u4e5f\u4e0d\u53ef\u4ee5\u88ab\u5207\u5272\u800c\u53ea\u6267\u884c\u5176\u4e2d\u4e00\u90e8\u5206\uff0c\u5c06\u6574\u4e2a\u64cd\u4f5c\u89c6\u4f5c\u4e00\u4e2a\u6574\u4f53\u662f\u539f\u5b50\u6027\u7684\u6838\u5fc3\u7279\u5f81\u3002<br \/>\n\u5728java\u4e2d\u63d0\u4f9b\u4e86\u5f88\u591a\u539f\u5b50\u7c7b\uff0c\u5728\u6b64\u4e3b\u8981\u628a\u8fd9\u4e9b\u539f\u5b50\u7c7b\u5206\u6210\u56db\u5927\u7c7b\u3002<br \/>\n\u539f\u5b50\u66f4\u65b0\u57fa\u672c\u7c7b\u578b\u6216\u5f15\u7528\u7c7b\u578b<br \/>\n\u5982\u679c\u662f\u57fa\u672c\u7c7b\u578b\uff0c\u5219\u66ff\u6362\u5176\u503c\uff0c\u5982\u679c\u662f\u5f15\u7528\uff0c\u5219\u66ff\u6362\u5176\u5f15\u7528\u5730\u5740\uff0c\u8fd9\u4e9b\u7c7b\u4e3b\u8981\u6709\uff1a<br \/>\n1\u3001AtomicBoolean<br \/>\n\u539f\u5b50\u66f4\u65b0\u5e03\u5c14\u7c7b\u578b\uff0c\u5185\u90e8\u4f7f\u7528int\u7c7b\u578b\u7684value\u5b58\u50a81\u548c0\u8868\u793atrue\u548cfalse\uff0c\u5e95\u5c42\u4e5f\u662f\u5bf9int\u7c7b\u578b\u7684\u539f\u5b50\u64cd\u4f5c\u3002<br \/>\n2\u3001AtomicInteger<br \/>\n\u539f\u5b50\u66f4\u65b0int\u7c7b\u578b\u3002<br \/>\n3\u3001AtomicLong<br \/>\n\u539f\u5b50\u66f4\u65b0long\u7c7b\u578b\u3002<br \/>\n4\u3001AtomicReference<br \/>\n\u539f\u5b50\u66f4\u65b0\u5f15\u7528\u7c7b\u578b\uff0c\u901a\u8fc7\u6cdb\u578b\u6307\u5b9a\u8981\u64cd\u4f5c\u7684\u7c7b\u3002<br \/>\n5\u3001AtomicMarkableReference<br \/>\n\u539f\u5b50\u66f4\u65b0\u5f15\u7528\u7c7b\u578b\uff0c\u5185\u90e8\u4f7f\u7528Pair\u627f\u8f7d\u5f15\u7528\u5bf9\u8c61\u53ca\u662f\u5426\u88ab\u66f4\u65b0\u8fc7\u7684\u6807\u8bb0\uff0c\u907f\u514d\u4e86ABA\u95ee\u9898\u3002<br \/>\n6\u3001AtomicStampedReference<br \/>\n\u539f\u5b50\u66f4\u65b0\u5f15\u7528\u7c7b\u578b\uff0c\u5185\u90e8\u4f7f\u7528Pair\u627f\u8f7d\u5f15\u7528\u5bf9\u8c61\u53ca\u66f4\u65b0\u7684\u90ae\u6233\uff0c\u907f\u514d\u4e86ABA\u95ee\u9898\u3002<br \/>\n\u8fd9\u51e0\u4e2a\u7c7b\u7684\u64cd\u4f5c\u57fa\u672c\u7c7b\u4f3c\uff0c\u5e95\u5c42\u90fd\u8c03\u7528Unsafe\u7684compareAndSwapXxx()\u6765\u5b9e\u73b0\uff0c\u57fa\u672c\u7528\u6cd5\u5982\u4e0b:<br \/>\nprivate static void testAtomicReference(){<br \/>\nAtomicInteger atomicInteger = new AtomicInteger(1);<br \/>\natomicInteger.incrementAndGet();<br \/>\natomicInteger.getAndIncrement();<br \/>\natomicInteger.compareAndSet(3,666);<br \/>\nSystem.out.println(atomicInteger.get());<br \/>\nAtomicStampedReference<Integer> atomicStampedReference = new AtomicStampedReference&lt;&gt;(1,1);<br \/>\natomicStampedReference.compareAndSet(1,2,1,3);<br \/>\natomicStampedReference.compareAndSet(2,666,3,5);<br \/>\nSystem.out.println(atomicStampedReference.getReference());<br \/>\nSystem.out.println(atomicStampedReference.getStamp());<br \/>\n}<br \/>\n\u539f\u5b50\u66f4\u65b0\u6570\u7ec4\u4e2d\u7684\u5143\u7d20<br \/>\n\u539f\u5b50\u66f4\u65b0\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\uff0c\u53ef\u4ee5\u66f4\u65b0\u6570\u7ec4\u4e2d\u6307\u5b9a\u7d22\u5f15\u4f4d\u7f6e\u7684\u5143\u7d20\uff0c\u8fd9\u4e9b\u7c7b\u4e3b\u8981\u6709:<br \/>\n1\u3001AtomicIntegerArray<br \/>\n\u539f\u5b50\u66f4\u65b0int\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u3002<br \/>\n2\u3001AtomicLongArray<br \/>\n\u539f\u5b50\u66f4\u65b0long\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u3002<br \/>\n3\u3001AtomicReferenceArray<br \/>\n\u539f\u5b50\u66f4\u65b0Object\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u3002<br \/>\n\u8fd9\u51e0\u4e2a\u7c7b\u7684\u64cd\u4f5c\u57fa\u672c\u7c7b\u4f3c\uff0c\u66f4\u65b0\u5143\u7d20\u65f6\u90fd\u8981\u6307\u5b9a\u5728\u6570\u7ec4\u4e2d\u7684\u7d22\u5f15\u4f4d\u7f6e\uff0c\u57fa\u672c\u7528\u6cd5\u5982\u4e0b\uff1a<br \/>\nprivate static void testAtomicReferenceArray(){<br \/>\nAtomicIntegerArray atomicIntegerArray = new AtomicIntegerArray(10);<br \/>\natomicIntegerArray.getAndIncrement(O);<br \/>\natomicIntegerArray.getAndAdd(1,666);<br \/>\natomicIntegerArray.incrementAndGet(2);<br \/>\natomicIntegerArray.addAndGet(3,666);<br \/>\natomicIntegerArray.compareAndSet(4,0,666);<br \/>\nSystem.out.println(atomicIntegerArray.get(0));<br \/>\nSystem.out.println(atomicIntegerArray.get(1));<br \/>\nSystem.out.println(atomicIntegerArray.get(2));<br \/>\nSystem.out.println(atomicIntegerArray.get(3));<br \/>\nSystem.out.println(atomicIntegerArray.get(4));<br \/>\nSystem.out.println(atomicIntegerArray.get(5));<br \/>\n}<br \/>\n\u539f\u5b50\u66f4\u65b0\u5bf9\u8c61\u4e2d\u7684\u5b57\u6bb5<br \/>\n\u539f\u5b50\u66f4\u65b0\u5bf9\u8c61\u4e2d\u7684\u5b57\u6bb5\uff0c\u53ef\u4ee5\u66f4\u65b0\u5bf9\u8c61\u4e2d\u6307\u5b9a\u5b57\u6bb5\u540d\u79f0\u7684\u5b57\u6bb5\uff0c\u8fd9\u4e9b\u7c7b\u4e3b\u8981\u6709\uff1a<br \/>\n1\u3001AtomicIntegerFieldUpdater<br \/>\n\u539f\u5b50\u66f4\u65b0\u5bf9\u8c61\u4e2d\u7684int\u7c7b\u578b\u5b57\u6bb5\u3002<br \/>\n2\u3001AtomicLongFieldUpdater<br \/>\n\u539f\u5b50\u66f4\u65b0\u5bf9\u8c61\u4e2d\u7684long\u7c7b\u578b\u5b57\u6bb5\u3002<br \/>\n3\u3001AtomicReferenceFieldUpdater<br \/>\n\u539f\u5b50\u66f4\u65b0\u5bf9\u8c61\u4e2d\u7684\u5f15\u7528\u7c7b\u578b\u5b57\u6bb5\u3002<br \/>\n\u8fd9\u51e0\u4e2a\u7c7b\u7684\u64cd\u4f5c\u57fa\u672c\u7c7b\u4f3c\uff0c\u90fd\u9700\u8981\u4f20\u5165\u8981\u66f4\u65b0\u7684\u5b57\u6bb5\u540d\u79f0\uff0c\u57fa\u672c\u7528\u6cd5\u5982\u4e0b\uff1a<\/p>\n<p>private static void testAtomicReferenceField() {<br \/>\nAtomicReferenceFieldUpdater&lt;User, String&gt; updateName = AtomicReferenceFieldUpdater.newUpdater(User.class, String.class,&quot;name&quot;);<br \/>\nAtomicIntegerFieldUpdater<User> updateAge = AtomicIntegerFieldUpdater.newUpdater(User.class, &quot;age&quot;);<\/p>\n<pre><code class=\"language-plain_text\">User user = new User(&quot;tong ge&quot;, 21);\nupdateName.compareAndSet(user, &quot;tong ge&quot;, &quot;read source code&quot;);\nupdateAge.compareAndSet(user, 21, 25);\nupdateAge.incrementAndGet(user);\n\nSystem.out.println(user);\n<\/code><\/pre>\n<p>}<\/p>\n<p>private static class User {<br \/>\nvolatile String name;<br \/>\nvolatile int age;<\/p>\n<pre><code class=\"language-plain_text\">public User(String name, int age) {\n    this.name = name;\n    this.age = age;\n}\n\n@Override\npublic String toString() {\n    return &quot;name: &quot; + name + &quot;, age: &quot; + age;\n}\n<\/code><\/pre>\n<p>}<br \/>\n\u9ad8\u6027\u80fd\u539f\u5b50\u7c7b<br \/>\n\u9ad8\u6027\u80fd\u539f\u5b50\u7c7b\uff0c\u662fjava8\u4e2d\u589e\u52a0\u7684\u539f\u5b50\u7c7b\uff0c\u5b83\u4eec\u4f7f\u7528\u5206\u6bb5\u7684\u601d\u60f3\uff0c\u628a\u4e0d\u540c\u7684\u7ebf\u7a0bhash\u5230\u4e0d\u540c\u7684\u6bb5\u4e0a\u53bb\u66f4\u65b0\uff0c\u6700\u540e\u518d\u628a\u8fd9\u4e9b\u6bb5\u7684\u503c\u76f8\u52a0\u5f97\u5230\u6700\u7ec8\u7684\u503c\uff0c\u8fd9\u4e9b\u7c7b\u4e3b\u8981\u6709\uff1a<br \/>\n1\u3001Striped64<br \/>\n\u4e0b\u9762\u56db\u4e2a\u7c7b\u7684\u7236\u7c7b\u3002<br \/>\n2\u3001LongAccumulator<br \/>\nlong\u7c7b\u578b\u7684\u805a\u5408\u5668\uff0c\u9700\u8981\u4f20\u5165\u4e00\u4e2along\u7c7b\u578b\u7684\u4e8c\u5143\u64cd\u4f5c\uff0c\u53ef\u4ee5\u7528\u6765\u8ba1\u7b97\u5404\u79cd\u805a\u5408\u64cd\u4f5c\uff0c\u5305\u62ec\u52a0\u4e58\u7b49\u3002<br \/>\n3\u3001LongAdder<br \/>\nlong\u7c7b\u578b\u7684\u7d2f\u52a0\u5668\uff0cLongAccumulator\u7684\u7279\u4f8b\uff0c\u53ea\u80fd\u7528\u6765\u8ba1\u7b97\u52a0\u6cd5\uff0c\u4e14\u4ece0\u5f00\u59cb\u8ba1\u7b97\u3002<br \/>\n4\u3001DoubleAccumulator<br \/>\ndouble\u7c7b\u578b\u7684\u805a\u5408\u5668\uff0c\u9700\u8981\u4f20\u5165\u4e00\u4e2adouble\u7c7b\u578b\u7684\u4e8c\u5143\u64cd\u4f5c\uff0c\u53ef\u4ee5\u7528\u6765\u8ba1\u7b97\u5404\u79cd\u805a\u5408\u64cd\u4f5c\uff0c\u5305\u62ec\u52a0\u4e58\u7b49\u3002<br \/>\n5\u3001DoubleAdder<br \/>\ndouble\u7c7b\u578b\u7684\u7d2f\u52a0\u5668\uff0cDoubleAccumulator\u7684\u7279\u4f8b\uff0c\u53ea\u80fd\u7528\u6765\u8ba1\u7b97\u52a0\u6cd5\uff0c\u4e14\u4ece0\u5f00\u59cb\u8ba1\u7b97\u3002<br \/>\n\u8fd9\u51e0\u4e2a\u7c7b\u7684\u64cd\u4f5c\u57fa\u672c\u7c7b\u4f3c\uff0c\u5176\u4e2dDoubleAccumulator\u548cDoubleAdder\u5e95\u5c42\u5176\u5b9e\u4e5f\u662f\u7528long\u6765\u5b9e\u73b0\u7684\uff0c\u57fa\u672c\u7528\u6cd5\u5982\u4e0b\uff1a<br \/>\nprivate static void testNewAtomic() {<br \/>\nLongAdder longAdder = new LongAdder();<br \/>\nlongAdder.increment();<br \/>\nlongAdder.add(666);<br \/>\nSystem.out.println(longAdder.sum());<\/p>\n<pre><code class=\"language-plain_text\">LongAccumulator longAccumulator = new LongAccumulator((left, right)-&gt;left + right * 2, 666);\nlongAccumulator.accumulate(1);\nlongAccumulator.accumulate(3);\nlongAccumulator.accumulate(-4);\nSystem.out.println(longAccumulator.get());\n<\/code><\/pre>\n<p>}<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u539f\u5b50\u64cd\u4f5c\u662f\u6307\u4e0d\u4f1a\u88ab\u7ebf\u7a0b\u8c03\u5ea6\u673a\u5236\u6253\u65ad\u7684\u64cd\u4f5c\uff0c\u8fd9\u79cd\u64cd\u4f5c\u4e00\u65e6\u5f00\u59cb\uff0c\u5c31\u4e00\u76f4\u8fd0\u884c\u5230\u7ed3\u675f\uff0c\u4e2d\u95f4\u4e0d\u4f1a\u6709\u4efb\u4f55\u7ebf\u7a0b\u4e0a\u4e0b\u6587\u5207\u6362\u3002  [&hellip;]<\/p>\n","protected":false},"author":8,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[99],"tags":[],"class_list":["post-8709","post","type-post","status-publish","format-standard","hentry","category-javabase"],"_links":{"self":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/8709","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\/8"}],"replies":[{"embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/comments?post=8709"}],"version-history":[{"count":1,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/8709\/revisions"}],"predecessor-version":[{"id":8710,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/8709\/revisions\/8710"}],"wp:attachment":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/media?parent=8709"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/categories?post=8709"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/tags?post=8709"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}