使用AtomicStampedReference<T>的大坑

//在初始化的时候会把引用和时间戳存到pair中

AtomicStampedReference<Integer> integerAtomicStampedReference = new AtomicStampedReference<Integer>(2020,1);

 

 

然后执行

System.out.println(integerAtomicStampedReference.compareAndSet(2020,2021,integerAtomicStampedReference.getStamp(),integerAtomicStampedReference.getStamp()+1));

 

 

//此时的2020和Pair中的2021不是同一个对象了

//这里注意Integer是一个类 在-127——+128有缓存

//大于128没有缓存,所以

 

//compareAndSet中expectedReference和current的地址就不同了我草,结果执行下边就返回false了

 1 public boolean compareAndSet(V   expectedReference,   2   3                              V   newReference,   4   5                              int expectedStamp,   6   7                              int newStamp) {   8   9     Pair<V> current = pair;  10  11     return  12  13         expectedReference == current.reference &&//这一行就是false  14  15         expectedStamp == current.stamp &&  16  17         ((newReference == current.reference &&  18  19           newStamp == current.stamp) ||  20  21          casPair(current, Pair.of(newReference, newStamp)));  22  23 }