{"id":85820,"date":"2020-05-12T09:30:20","date_gmt":"2020-05-12T01:30:20","guid":{"rendered":"http:\/\/4563.org\/?p=85820"},"modified":"2020-05-12T09:30:20","modified_gmt":"2020-05-12T01:30:20","slug":"%e4%b8%ba%e4%bb%80%e4%b9%88-atomicintegerarray-%e7%9a%84-array-%e6%88%90%e5%91%98%e4%b8%8d%e6%98%af-volatile-%e8%80%8c%e6%98%af-final-%e7%9a%84%e5%95%8a%ef%bc%9f","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=85820","title":{"rendered":"\u4e3a\u4ec0\u4e48 AtomicIntegerArray \u7684 array \u6210\u5458\u4e0d\u662f volatile \u800c\u662f final \u7684\u554a\uff1f"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  \u4e3a\u4ec0\u4e48 AtomicIntegerArray \u7684 array \u6210\u5458\u4e0d\u662f volatile \u800c\u662f final \u7684\u554a\uff1f               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : amiwrong123 <\/span>  <span><i><\/i> 5<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p>\u5bf9\u6bd4\u4e24\u4e2a\u539f\u5b50\u7c7b\uff1a<\/p>\n<pre><code>public class AtomicIntegerArray implements java.io.Serializable {      private final int[] array;          public final int get(int i) {         return getRaw(checkedByteOffset(i));     }      private int getRaw(long offset) {         return unsafe.getIntVolatile(array, offset);     } <\/code><\/pre>\n<pre><code>public class AtomicStampedReference&lt;V&gt; {     private static class Pair&lt;T&gt; {         final T reference;         final int stamp;         private Pair(T reference, int stamp) {             this.reference = reference;             this.stamp = stamp;         }         static &lt;T&gt; Pair&lt;T&gt; of(T reference, int stamp) {             return new Pair&lt;T&gt;(reference, stamp);         }     }      private volatile Pair&lt;V&gt; pair;          public V getReference() {         return pair.reference;     }      public int getStamp() {         return pair.stamp;     } <\/code><\/pre>\n<p>\u7b2c\u4e00\u4e2a\u95ee\u9898\u5c31\u662f\uff1a\u4e3a\u4ec0\u4e48 AtomicIntegerArray \u7684 array \u6210\u5458\u4e0d\u662f volatile \u800c\u662f final \u7684\u554a\uff1f\u4e3a\u5565\u8fd9\u4e48\u8bbe\u8ba1\u554a<\/p>\n<p>\u7b2c\u4e8c\u4e2a\u95ee\u9898\u5c31\u662f\uff1a\u4e3a\u4ec0\u4e48 AtomicIntegerArray \u7684 get \u51fd\u6570\u4e0d\u80fd\u50cf AtomicStampedReference \u4e00\u6837\u5b9e\u73b0\u5462\uff1f\u662f\u56e0\u4e3a\u6210\u5458\u4e0d\u662f volatile \u7684\u5457\u3002\u76f4\u63a5\u50cf\u4e0b\u9762\u8fd9\u6837\u5b9e\u73b0\uff0c\u4e0d\u884c\u5417\u3002\u5982\u679c\u6539\u6210 <code>private volatile int[] array;<\/code>\uff0c\u662f\u4e0d\u662f\u5c31\u53ef\u4ee5\u4e86\uff1f<\/p>\n<pre><code>    public final int get(int i) {         return array[i];  \/\/\u7c7b\u4f3c pair.reference;     } <\/code><\/pre>\n<p>\u5404\u4f4d\u5927\u4f6c\uff0c\u6c42\u89e3\u7b54\u554a<\/p>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>7<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"1496687\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : hoholiday <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u4e86\u89e3\u4e00\u4e0b unsafe \u7c7b                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1496688\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : luozic <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             https:\/\/tech.meituan.com\/2019\/02\/14\/talk-about-java-magic-class-unsafe.html                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1496689\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : amiwrong123 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @hoholiday <br \/>\u4e86\u89e3\u4e86\u554a\u3002\u8981\u77e5\u9053\u5927\u90e8\u5206\u539f\u5b50\u7c7b\uff0c\u5176\u6570\u636e\u6210\u5458\u90fd\u662f volatile \u7684\uff0c\u5176 get \u65b9\u6cd5\u90fd\u662f\u76f4\u63a5 return \u6210\u5458\uff0c\u800c\u4e0d\u662f\u901a\u8fc7 unsafe \u5bf9\u8c61 get \u3002<br \/>\u4f46\u539f\u5b50\u6570\u7ec4\u7c7b\uff0c\u5728\u8bbe\u8ba1\u4e0a\u5c31\u4e0d\u4e00\u6837\u4e86\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1496690\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : iffi <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             AtomicIntegerArray \u7684 set \u65b9\u6cd5\uff0c\u901a\u8fc7 putIntVolatile \u65b9\u5f0f\u4fee\u6539\uff08\u4fee\u6539\u7684\u662f array \u7684\u67d0\u4e2a index \u5bf9\u5e94\u7684\u503c\uff0c\u4e0d\u7528\u5bf9\u6574\u4e2a array \u52a0 volatile \u4fee\u9970\uff09<br \/> public final void set(int i, int newValue) {<br \/> unsafe.putIntVolatile(array, checkedByteOffset(i), newValue);<br \/> }<\/p>\n<p>AtomicStampedReference \u7684 set \u65b9\u6cd5\uff0c\u76f4\u63a5=\u8d4b\u503c\u4fee\u6539\uff0c\u5e76\u53d1\u60c5\u51b5\u4e0b\uff0c\u4e3a\u4e86\u5185\u5b58\u53ef\u89c1\u6027\u91c7\u7528 volatile \u4fee\u9970<br \/> public void set(V newReference, int newStamp) {<br \/> Pair&lt;V&gt; current = pair;<br \/> if (newReference != current.reference || newStamp != current.stamp)<br \/> this.pair = Pair.of(newReference, newStamp);<br \/> }                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1496691\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : amiwrong123 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @iffi <br \/>\u90a3\u5982\u679c\u4fee\u6539 AtomicIntegerArray \u4e3a private volatile int[] array\uff0c\u518d\u4fee\u6539 set \u51fd\u6570\u4e3a public final void set(int i, int newValue) {array[i] = newValue; } \u3002<\/p>\n<p>\u8fd9\u6837\u662f\u5426\u53ef\u4ee5\u4fdd\u8bc1 \u53ef\u89c1\u6027 \u5462                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1496692\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : iffi <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @amiwrong123 \u8fd9\u4e2a array \u521b\u5efa\u597d\u4e86\u4e4b\u540e\uff0c\u4e0d\u4f1a\u4fee\u6539\u5176\u5f15\u7528\u6307\u5411\uff0c\u6240\u4ee5 private volatile int[] array \u4e0d\u7b26\u5408\u8bed\u4e49\u3002\u4fee\u6539 set \u51fd\u6570\u4e3a public final void set(int i, int newValue) {array[i] = newValue; } \u6211\u731c\u4e0d\u4f1a\u4fdd\u8bc1\u53ef\u89c1\u6027\uff08\u4f60\u4e5f\u53ef\u4ee5\u590d\u5236 JDK \u7684\u6e90\u7801\u4fee\u6539\u5e76\u81ea\u5df1\u5b9e\u9a8c\u9a8c\u8bc1\u4e00\u4e0b\uff09\uff0c\u9664\u975e\u5bf9 array \u6bcf\u4e00\u4e2a\u5143\u7d20\u90fd\u52a0 volatile \u4fee\u9970\u3002\u8fd9\u4e5f\u662f\u4e3a\u4ec0\u4e48\u8981\u7528 unsafe.putIntVolatile(array, checkedByteOffset(i), newValue);                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1496693\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : SoloCompany <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u4f60\u9700\u8981\u7684\u662f AtomicReference \u800c\u4e0d\u662f AtomicXxxArray                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>\u4e3a\u4ec0\u4e48 AtomicInteger&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/85820"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=85820"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/85820\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=85820"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=85820"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=85820"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}