Chapter 2 AtomicInteger source code parsing: atomicinteger source code
1. Atomic class
- Possible atomic operations
- CAS-based
The following uses AtomicInteger as an example.
2. AtomicInteger
Before AtomicInteger is available, synchronous locks must be used for thread-safe operations on an Integer. Of course, ReentrantLock can also be used for this purpose, however, the best and most convenient implementation method is AtomicInteger.
Example:
Package com. collection. test; import java. util. concurrent. atomic. atomicInteger;/*** test the atomic class */public class AtomicTest {private static AtomicInteger atomicInteger = new AtomicInteger (); // obtain the current value public static void getCurrentValue () {System. out. println (atomicInteger. get (); // --> 0} // set the value to public static void setValue () {atomicInteger. set (12); // directly overwrite the old value System with 12. out. println (atomicInteger. get (); // --> 12} // get first according to the method name getAndSet, then the old value is returned, the new value is public static void getAndSet () {System. out. println (atomicInteger. getAndSet (15); // --> 12} public static void getAndIncrement () {System. out. println (atomicInteger. getAndIncrement (); // --> 15} public static void getAndDecrement () {System. out. println (atomicInteger. getAndDecrement (); // --> 16} public static void getAndAdd () {System. out. println (atomicInteger. getAndAdd (10); // --> 15} public static void incrementAndGet () {System. out. println (atomicInteger. incrementAndGet (); // --> 26} public static void decrementAndGet () {System. out. println (atomicInteger. decrementAndGet (); // --> 25} public static void addAndGet () {System. out. println (atomicInteger. addAndGet (20); // --> 45} public static void main (String [] args) {AtomicTest test = new AtomicTest (); test. getCurrentValue (); test. setValue (); // return the old value series test. getAndSet (); test. getAndIncrement (); test. getAndDecrement (); test. getAndAdd (); // return the new value series test. incrementAndGet (); test. decrementAndGet (); test. addAndGet ();}}View Code
Source code:
Private volatile int value; // initialization value/*** create a AtomicInteger with the initial value initialValue */public AtomicInteger (int initialValue) {value = initialValue ;} /*** create a AtomicInteger with the initial value 0 */public AtomicInteger () {}/ *** return value */public final int get () {return value ;} /*** set the value for the value (based on the value), while other operations are based on the old value <-- get () */public final void set (int newValue) {value = newValue;} public final boolean compareAndSet (int exact CT, int update) {return unsafe. compareAndSwapInt (this, valueOffset, exact CT, update);}/*** set a new value for the old value based on CAS and use an infinite loop, until the setting is successful ** @ return returns the old value */public final int getAndSet (int newValue) {for (;) {int current = get (); // obtain the current value (old value) if (compareAndSet (current, newValue) // The New Value of CAS replaces the old value return current; // return the old value}/*** current value + 1, using an infinite loop, until + 1 succeeds * @ return the previous value returns the old value */public final int getAndIncrement () {for (;) {int current = get (); // obtain the current value int next = current + 1; // The current value + 1 if (compareAndSet (current, next) // return current Based on CAS ;}} /*** the current value-1 adopts an infinite loop until-1 is successful * @ return the previous value returns the old value */public final int getAndDecrement () {for (;) {int current = get (); int next = current-1; if (compareAndSet (current, next) return current ;}} /*** the current value + delta, which adopts an infinite loop until + delta succeeds * @ return the previous value returns the old value */public final int getAndAdd (int delta) {for (;) {int current = get (); int next = current + delta; if (compareAndSet (current, next) return current ;}} /*** the current value is + 1, and an infinite loop is used until + 1 is successful * @ return the updated value to return the new value */public final int incrementAndGet () {for (;) {int current = get (); int next = current + 1; if (compareAndSet (current, next) return next; // return new value}/*** current value-1, using an infinite loop, until-1 is successful * @ return the updated value returns the new value */public final int decrementAndGet () {for (;) {int current = get (); int next = current-1; if (compareAndSet (current, next) return next; // return new value}/*** current value + delta, using an infinite loop, until + delta succeeds * @ return the updated value returns the new value */public final int addAndGet (int delta) {for (;) {int current = get (); int next = current + delta; if (compareAndSet (current, next) return next; // return new value}/*** get current value */public int intValue () {return get ();}View Code
Note: The Use and source code are too simple! Let's take a look at the annotations.
Note:
- Value is volatile, volatile related content see "Appendix 2 volatile", specific link: http://www.cnblogs.com/java-zhao/p/5125698.html
- Single-step operations: for example, set () directly performs operations on the value without CAS, because a single-step operation is an atomic operation.
- Multi-step operation: for example, getAndSet (int newValue) is a two-step operation --> get the value first, and set the value, so it needs to be atomic. Here CAS is used.
- For the method to return the old value or the new value, you can directly check whether the method starts with get (returns the old value) or ends with get (returns the new value ).
- CAS: compare whether the value in CPU memory is the current value. If so, replace it with the new value update. If not, it indicates that the value is obtained before the set value, this value has been set by others first. If you set another value, you need to operate on the value modified by others. Otherwise, the modification will be overwritten, in this case, false is returned directly, and then the infinite loop is executed to obtain the current value again. Then, the addition and subtraction operations are performed based on CAS.
- If you still don't understandCAS, optimistic locks similar to databases.