2 threads executing myAtomicReference.compareAndSet (expected new date ())

static boolean unsynchronizedSetter(Date expected){
    Date newDate = new Date();
    AtomicReference<Date> myAtomicReference = Lookup.getAtomicRef();
    boolean myStatus = myAtomicReference.compareAndSet(expected, newDate); //CAS
    return myStatus;
}

Q: If 2 threads are running, which object will be stored in the reference link?

In a multiprocessor machine, 2 threads can execute CAS in a single clock cycle. Suppose they both use the same myAtomicReference object to execute CAS, both use the correct "expected" value, but they try to put two different objects, that is, 2 newDate. One of them should fail, but will myStatus be false in this thread?

I assume that one hardware implementation of CompareAndSwap will force the thread queue to perform its updates. I think that even if 2 processors execute the CAS instruction in one clock cycle, one of them is probably delayed.

+3
2
Q: If 2 threads executes it, which object will get stored in the atomic reference?

. javadoc, .

In a multi-processor machine, 2 threads could be performing the CAS in the same clock cycle.

AFAIK, Intel/AMD.

One of them must fail, but will myStatus be false in that thread?

, , , java.util.concurrent . , myStatus , .

I guess one hardware implementation of CompareAndSwap would make the threads queue up to do their updates.

"queue up" ( - ), CAS .

+1

. , , myStatus == true -

"One of them must fail, but will myStatus be false in that thread?"

, , "", newDate. , , myStatus CAS. , myStatus , AtomicReference, . AtomicReference . AtomicReference - .

myStatus==true , expected, JVM . JVM newDate AtomicReference. "" .

, .

+1

Source: https://habr.com/ru/post/1774447/


All Articles