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] ,对内部对象来维护带戳的引用;

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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);
        }
    }

  

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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   anpeiyong  阅读(7)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示