1. Atomic class
- Some atomic operations can be implemented
- Based on CAs
Let's take Atomicinteger as an example.
2, Atomicinteger
In the absence of Atomicinteger, for an integer thread-safe operation, it is necessary to use a synchronous lock to achieve, of course, can now be implemented through Reentrantlock, but the best and most convenient way to implement is to use Atomicinteger.
Specific examples:
Packagecom.collection.test;ImportJava.util.concurrent.atomic.AtomicInteger;/*** Test of Atomic class*/ Public classAtomictest {Private StaticAtomicinteger Atomicinteger =NewAtomicinteger (); //Get Current value Public Static voidGetCurrentValue () {System.out.println (Atomicinteger.get ());//-->0 } //Setting value Values Public Static voidSetValue () {Atomicinteger.set (12);//overwrite the old value directly with 12System.out.println (Atomicinteger.get ());//-->12 } //according to the method name Getandset know first get, then the last return is the old value, if get in, is to return the new value Public Static voidGetandset () {System.out.println (Atomicinteger.getandset (15));//-->12 } Public Static voidgetandincrement () {System.out.println (atomicinteger.getandincrement ());//-->15 } Public Static voidgetanddecrement () {System.out.println (atomicinteger.getanddecrement ());//-->16 } Public Static voidGetandadd () {System.out.println (Atomicinteger.getandadd (10));//-->15 } Public Static voidIncrementandget () {System.out.println (Atomicinteger.incrementandget ());//-->26 } Public Static voidDecrementandget () {System.out.println (Atomicinteger.decrementandget ());//-->25 } Public Static voidAddandget () {System.out.println (Atomicinteger.addandget (20));//-->45 } Public Static voidMain (string[] args) {atomictest test=Newatomictest (); Test.getcurrentvalue (); Test.setvalue (); //return old value seriesTest.getandset (); Test.getandincrement (); Test.getanddecrement (); Test.getandadd (); //returns the new value seriesTest.incrementandget (); Test.decrementandget (); Test.addandget (); }}
View Code
Source:
Private volatile intValue//Initialize Value /*** Create a Atomicinteger with an initial value of InitialValue*/ PublicAtomicinteger (intInitialValue) {Value=InitialValue; } /*** Create a Atomicinteger with an initial value of 0*/ PublicAtomicinteger () {}/*** return value*/ Public Final intget () {returnvalue; } /*** value is set (based on value), while other operations are based on the old value <--get ()*/ Public Final voidSetintnewvalue) {Value=NewValue; } Public Final BooleanCompareandset (intExpectintupdate) { returnUnsafe.compareandswapint ( This, Valueoffset, expect, update); } /*** Set a new value for the old value based on CAs, using an infinite loop until the setting is successful * *@returnreturn old value*/ Public Final intGetandset (intnewvalue) { for (;;) { intCurrent = Get ();//get current value (old value) if(Compareandset (current, newvalue))//CAs new values replace old values returnCurrent//return old value } } /*** Current value +1, with infinite Loop until +1 successful *@returnThe previous value returns the old values*/ Public Final intgetandincrement () { for (;;) { intCurrent = Get ();//Get Current value intNext = current + 1;//Current Value +1 if(Compareandset (current, next))//based on CAS assignment values returnCurrent ; } } /*** Current Value-1, with Infinite loop, until-1 successful *@returnThe previous value returns the old values*/ Public Final intgetanddecrement () { for (;;) { intCurrent =get (); intNext = current-1; if(Compareandset (current, next))returnCurrent ; } } /*** Current value +delta, with infinite loop until +delta succeeds *@returnThe previous value returns the old values*/ Public Final intGetandadd (intDelta) { for (;;) { intCurrent =get (); intNext = current +Delta; if(Compareandset (current, next))returnCurrent ; } } /*** Current value +1, with infinite Loop until +1 successful *@returnThe updated value returns new Values*/ Public Final intIncrementandget () { for (;;) { intCurrent =get (); intNext = current + 1; if(Compareandset (current, next))returnNext//returns the new value } } /*** Current Value-1, with Infinite loop, until-1 successful *@returnThe updated value returns new Values*/ Public Final intDecrementandget () { for (;;) { intCurrent =get (); intNext = current-1; if(Compareandset (current, next))returnNext//returns the new value } } /*** Current value +delta, with infinite loop until +delta succeeds *@returnThe updated value returns new Values*/ Public Final intAddandget (intDelta) { for (;;) { intCurrent =get (); intNext = current +Delta; if(Compareandset (current, next))returnNext//returns the new value } } /*** Get Current value*/ Public intintvalue () {returnget (); }
View Code
Description: Use and source code are simple to explode! Look at the notes yourself.
Attention:
- Value is volatile, related to volatile content see "attached 2 volatile", Specific link: http://www.cnblogs.com/java-zhao/p/5125698.html
- Single-Step operations: For example, set () operates directly on value and does not require CAS because a single-step operation is an atomic operation.
- Multi-Step operations: For example, Getandset (int newvalue) is a two-step operation--first getting the value, setting the value, so it needs to be atomized, which is implemented by CAS.
- Whether the method returns an old value or a new value, it is good to see whether the method starts with a get (return old value) or a Get end (a new value is returned).
- CAS: Compare the value on the CPU memory is current value, if it is the new value of update, if not, the description gets the value after the setting value, the value has been set by others first, at this time if you set the value again, you need to change the value of other people on the basis of the operation, Otherwise it will overwrite other people's changes, so this time will directly return false, then an infinite loop, re-fetch the current value, and then based on CAS for the addition and subtraction operations.
- If you still do not understand CAS, analogy database optimistic lock .
11th Chapter Atomicinteger Source Code Analysis