Home > Back-end >  The implementation of the principle to solve AtomicInteger: getAndIncrement ()
The implementation of the principle to solve AtomicInteger: getAndIncrement ()

Time:09-16

AtomicInteger: getAndIncrement () the implementation of the principle of solving
Javaatomicinteger
Public final int getAndIncrement () {
For (;; ) {
Int current=the get ();
Int next=current + 1;
If (compareAndSet (current and next))
Return the current;
}
}

Public final Boolean compareAndSet (int expect, int the update) {
Return unsafe.com pareAndSwapInt (this, valueOffset, expect, update).
}
The book says:
Source for loop body in the first step to obtain AtomicInteger stored value, the second step is to add 1 AtomicInteger current value operation, the key of the third pace the compareAndSet methods for atomic updates, and the operation to check the current value is equal to the current, is equal to the mean AtomicInteger value has not been modified, other threads will update AtomicInteger current value into the value of the next, if compareAndSet method returns false, the program will enter a for loop to compareAndSet operation,
My question:
If you have been modified by the other threads, and the extra execution for () loop what is the point? Expected to 3 to 4, but at that time by other threads to 5, does not meet the compareAndSet, at this time to go in for () loop again? To understand

CodePudding user response:

This method is to perform an operation, can't tell how much is the duty to perform plus one, if there is such a logic should judge before calling the method

CodePudding user response:

This method is only to complete their own features, but you don't understand because you don't have the function of these two methods combined with application scenario.

CodePudding user response:

2020 years are all out

CodePudding user response:

refer to the building of the wind running reply:
AtomicInteger: getAndIncrement () the implementation of the principle to solve the
Javaatomicinteger
Public final int getAndIncrement () {
For (;; ) {
Int current=the get ();
Int next=current + 1;
If (compareAndSet (current and next))
Return the current;
}
}

Public final Boolean compareAndSet (int expect, int the update) {
Return unsafe.com pareAndSwapInt (this, valueOffset, expect, update).
}
The book says:
Source for loop body in the first step to obtain AtomicInteger stored value, the second step is to add 1 AtomicInteger current value operation, the key of the third pace the compareAndSet methods for atomic updates, and the operation to check the current value is equal to the current, is equal to the mean AtomicInteger value has not been modified, other threads will update AtomicInteger current value into the value of the next, if compareAndSet method returns false, the program will enter a for loop to compareAndSet operation,
My question:
If you have been modified by the other threads, and the extra execution for () loop what is the point? Expected to 3 to 4, but at that time by other threads to 5, does not meet the compareAndSet, at this time to go in for () loop again? Confusing

That you say so, that means you don't have a look at its construction method, its construction method was introduced into an int value, the value assigned to the private variables, this variable is used volatile, that is to say, every time he is copy in its cache variable values, due to the variable using the volatile, every time the variable changes, they will back to memory copy into the new value as the new variables, so its copy of the variable and not taking after is the same
  • Related