static boolean unsynchronizedSetter(Date expected){
Date newDate = new Date();
AtomicReference<Date> myAtomicReference = Lookup.getAtomicRef();
boolean myStatus = myAtomicReference.compareAndSet(expected, newDate); //CAS
return myStatus;
}
问:如果2个线程执行它,哪个对象将存储在原子引用?2个线程执行myAtomicReference.compareAndSet(预计,新的日期())
在多处理器机器中,2个线程可能在相同的时钟周期内执行CAS。假设它们都使用相同的myAtomicReference对象来执行CAS,它们都使用“expected”的正确值,但它们试图放入2个不同的对象,即2个newDate。其中一个必须失败,但myStatus会在该线程中为假?
我猜想CompareAndSwap的一个硬件实现会使线程排队以进行更新。我猜即使2个处理器在相同的时钟周期内执行CAS指令,其中一个可能会延迟。