Grokking Java Interview Questions
0% completed
Atomic Variables and Compare and Swap
A counter that many threads change does not need a lock. A modern processor can replace a value in memory on the condition that the value is still the expected one, and no other thread can change that value between the comparison and the replacement. The classes in java.util.concurrent.atomic give this operation to Java code.
The four answers explain how the operation works, how it differs from volatile and from synchronized, which change it cannot see, and why one shared counter becomes slow when many threads update it.
What is compare and swap?
Level: Mid-level
.....
.....
.....
Like the course? Get enrolled and start learning!
Reading Progress
0%