AtomicInteger
works with two concepts : CAS and volatile
variable.
Using volatile
variable insures that the current value will be visible to all threads and it will not be cached.
But I am confused over CAS(compare AND set) concept which is explained below:
public final int getAndIncrement() { for (;;) { int current = get(); int next = current + 1; if (compareAndSet(current, next)) return current; } }
My question is that whatif(compareAndSet(current, next)
returns false
? Will the value not be updated? In this case what will happen when a Thread is executing the below case:
private AtomicInteger count = new AtomicInteger(); count.incrementAndGet();
Compare and swap is a technique used when designing concurrent algorithms. The approach is to compare the actual value of the variable to the expected value of the variable and if the actual value matches the expected value, then swap the actual value of the variable for the new value passed in.
The compare-and-swap (CAS) instruction is an uninterruptible instruction that reads a memory location, compares the read value with an expected value, and stores a new value in the memory location when the read value matches the expected value. Otherwise, nothing is done.
Method SummaryAtomically adds the given value to the current value. Atomically sets the value to the given updated value if the current value == the expected value. Atomically decrements by one the current value. Returns the value of this AtomicInteger as a double after a widening primitive conversion.
Setting the AtomicInteger Value set() example: AtomicInteger atomicInteger = new AtomicInteger(123); atomicInteger. set(234); This example creates an AtomicInteger example with an initial value of 123, and then sets its value to 234 in the next line.
The atomic objects make use of Compare and Swap mechanism to make them atomic - i.e. it is possible to guarantee that the value was as specified and is now at the new value.
The code you posted continually tries to set the current value to one more than it was before. Remember that another thread could also have performed a get
and is trying to set it too. If two threads race each other to change the value it is possible for one of the increments to fail.
Consider the following scenario:
get
and gets the value 1
.next
to be 2
.get
and gets the value 1
.next
to be 2
.Now because of atomics - only one thread will succeed, the other will recieve false
from the compareAndSet
and go around again.
If this mechanism was not used it would be quite possible for both threads to increment the value resulting in only one increment actually being done.
The confusing infinite loop for(;;)
will only really loop if many threads are writing to the variable at the same time. Under very heavy load it may loop around several times but it should complete quite quickly.
for (;;)
is an infinite loop, so it will just retry the attempt.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With