Atomic Variables
why the word atomic?
Atomic means something you can't divide further. In Java, atomic variables do two tasks as one atomic task.
The two tasks include get and set, or compare and increment.
In Java, atomic variables can be accessed by only one thread at a time.
Achieved using a special CPU instruction
The CPU serializes access to a memory address with a special instruction. Java just uses it.
CAS - Compare and Save
Compare and Save is just one of the features of atomic variables.
Atomic variables ensure atomicity for just that one line. And the CPU handles it. Even before the next line runs, another thread may have taken the lock and changed the value.
No blocking of threads
No threads are locked with atomic variables. Each thread just keeps trying to get full access to the memory address until it does.