1

How does Java's atomic variables like AtomicInteger work internally to achieve mutual exclusion/atomicity?

Is there any locking involved at machine instruction level which yields better performance?

Or an atomic machine level instruction itself does this compared to the regular set of machine instructions where the value is copied from main memory to register/cache and operation performed on this register and then stored back in main memory.

Xavier DSouza
  • 2,861
  • 7
  • 29
  • 40

1 Answers1

1

afaik it takes advantadge of CPU instructions already available for this kind of primitives. Or at least, in the CPUs where these instructions exist (don't know if all, that's part of the jvm portable stuff). In the ones that don't, some kind of locking might be used.

i guess you already did, but just in case it helps: here you can read some brief details.

albert_nil
  • 1,648
  • 7
  • 9