首页 > 其他分享 >AtomicStampedReference

AtomicStampedReference

时间:2024-09-14 10:13:46浏览次数:1  
标签:reference stamp value int Pair AtomicStampedReference public

概述

  An {@code AtomicStampedReference} maintains an object reference along with an integer "stamp", that can be updated atomically.

    包含 一个对象引用 + 一个int的戳(Stamp),能被原子更新;

  Implementation note: This implementation maintains stamped references by creating internal objects representing "boxed" [reference, integer] pairs.

    通过创建 表示容器的pair[reference, integer] ,对内部对象来维护带戳的引用;

  

public class AtomicStampedReference<V> {
        
        private static class Pair<T> {
            final T reference;                              // 共享数据对象引用
            final int stamp;                                // 戳
            private Pair(T reference, int stamp) {
                this.reference = reference;
                this.stamp = stamp;
            }
            static <T> Pair<T> of(T reference, int stamp) {
                return Pair<T>(reference, stamp);
            }
        }

        private volatile Pair<V> pair;

        public AtomicStampedReference(V initialRef, int initialStamp) {
            pair = Pair.of(initialRef, initialStamp);
        }

        public V getReference() {
            return pair.reference;
        }

        public int getStamp() {
            return pair.stamp;
        }

        /**
         *
         * @param expectedReference the expected value of the reference     预期的引用对象值
         * @param newReference the new value for the reference              新的引用对象值
         * @param expectedStamp the expected value of the stamp             预期的戳值
         * @param newStamp the new value for the stamp                      新的戳值
         */
        public boolean compareAndSet(V expectedReference, V newReference, int expectedStamp, int newStamp) {
            Pair<V> current = pair;
            return expectedReference == current.reference && expectedStamp == current.stamp 
                    &&
                    ((newReference == current.reference && newStamp == current.stamp) || casPair(current, Pair.of(newReference, newStamp)));
        }

        private boolean casPair(Pair<V> cmp, Pair<V> val) {
            return UNSAFE.compareAndSwapObject(this, pairOffset, cmp, val);
        }
    }

  

示例

public static void main(String[] args) {
        Data initialValue = new Data("Initial Value");

        AtomicStampedReference<Data> dataAtomicStampedReference = new AtomicStampedReference<>(initialValue, 0);            // 构建初始值+Stamp

        Data newValue1 = new Data("newValue1");
        int stamp = dataAtomicStampedReference.getStamp();
        dataAtomicStampedReference.compareAndSet(dataAtomicStampedReference.getReference(), newValue1, stamp, stamp + 1);      

        System.out.println(dataAtomicStampedReference.getReference());                      
    }

    public static class Data {
        private String value;

        public Data(String value) {
            this.value = value;
        }

        public String getValue() {
            return value;
        }

        @Override
        public String toString() {
            return "Data{" +
                    "value='" + value + '\'' +
                    '}';
        }
    }

  

标签:reference,stamp,value,int,Pair,AtomicStampedReference,public
From: https://www.cnblogs.com/anpeiyong/p/18413431

相关文章

  • java原子类AtomicStampedReference
    一、什么是CASCAS,compareandswap的缩写,中文翻译成比较并交换。CAS操作包含三个操作数,内存位置(V)、预期原值(A)和新值(B)。如果内存位置的值与预期原值相匹配,那么处理器会自动将该位置值更新为新值。否则,处理器不做任何操作。二、案例publicstaticintcount=0;privatefinal......