/**
* Atomically update Java variable to <tt>x</tt> if it is currently
* holding <tt>expected</tt>.
* @return <tt>true</tt> if successful
*/
public final native boolean compareAndSwapObject(Object o, long offset,
Object expected,
Object x);
1) How do they “atomically” set the value of “o” at position “offset” to “x” if “value==expected”?
2) How do they “lock” the object when we use synchronized method or synchronized block? I am just curious to know what goes under the hood.
As this is native method, they will fall back to whatever is offered by target platform. IIRC, “compare and swap” was present on MIPS and SPARC as processor command more than 10 years ago.
they use something like 1 behind the scenes to obtain lock on object