public class AtomicStampedReferenceTest {
public final static AtomicStampedReference ATOMIC_REFERENCE = new AtomicStampedReference("abc" ,0);
public static void main(String []args) {
for(int i = 0 ; i < 100 ; i++) {
final int num = i;
final int stamp = ATOMIC_REFERENCE.getStamp();
new Thread() {
public void run() {
try {
Thread.sleep(Math.abs((int)(Math.random() * 100)));
} catch (InterruptedException e) {
e.printStackTrace();
}
//时间戳初始化为0 然后每次进行判断当前的值是否为0 然后判断当前值是否为abc 同时满足的话则吧值修改成abc2 时间戳改成1
if(ATOMIC_REFERENCE.compareAndSet("abc" , "abc2" , stamp , stamp + 1)) {
System.out.println("我是线程:" + num + ",我获得了锁进行了对象修改!"+ATOMIC_REFERENCE.getStamp());
}
System.out.println(stamp);//这里会一直输出1 不会输出2 下面的线程不是修改成2了么 ????
}
}.start();
}
new Thread() {
public void run() {
int stamp = ATOMIC_REFERENCE.getStamp();
//
while(!ATOMIC_REFERENCE.compareAndSet("abc2", "abc" , stamp , stamp + 1));
System.out.println("已经改回为原始值!"+ ATOMIC_REFERENCE.getStamp());
}
}.start();
}
}
对????标注的地方不是很明白 还望各位指教