AtomicStampedReference

概述

  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 + '\'' +
                    '}';
        }
    }

  

posted on 2024-09-14 10:18  anpeiyong  阅读(6)  评论(0编辑  收藏  举报

导航