"Java concurrency" Atomicinteger source Comment

Source: Internet
Author: User

Atomicinteger Source

Under the Java.util.concurrent.atomic package provides a large number of atomic classes, here to Atomicinteger source code for example, added some comments, personal understanding, for reference;

One of the more important concepts is the CAS operation, the modern CPU has been widely supported, in the JDK Atomicinteger class is called the unsafe class Compareandswapint method implementation, code (jdk1.7.0_79) As follows:

 Packagejava.util.concurrent.atomic;ImportSun.misc.Unsafe; Public classAtomicintegerextendsNumberImplementsjava.io.Serializable {//Serialization related    Private Static Final LongSerialversionuid = 6214790243416807050L; //a tool class object used in the JDK that provides methods for unsafe operations, which are generally not used in their own programs//The Objectfieldoffset, Putorderedint and Compareandswapint methods are mainly used here.    Private Static Finalunsafe unsafe =Unsafe.getunsafe (); //The offset of the memory address of the value member property relative to the object memory address    Private Static Final LongValueoffset; Static {      Try {        //Initialize Valueoffset, get member properties by Unsafe.objectfieldoffset method offset of value memory address relative to object memory address Valueoffset = Unsafe.objectfieldoffset (Atomicinteger.class. Getdeclaredfield ("value")); } Catch(Exception ex) {Throw NewError (ex);} }    //the value of int, set to volatile, to ensure visibility between threads    Private volatile intvalue; /*** Construction method, passing in the specified int value * *@paramInitialValue the initial value*/     PublicAtomicinteger (intInitialValue) {Value=InitialValue; }    /*** Construction method, using default value 0*/     PublicAtomicinteger () {}/*** Get int value*/     Public Final intget () {returnvalue; }    /*** Set to specified value*/     Public Final voidSetintnewvalue) {Value=NewValue; }    /*** Finally set to the specified value, but other threads cannot see the change immediately, will delay a*/     Public Final voidLazyset (intnewvalue) {Unsafe.putorderedint ( This, Valueoffset, NewValue); }    /*** atomically set to the given value and returns the old value*/     Public Final intGetandset (intnewvalue) {        //optimistic lock, non-blocking synchronous mode, loop call Compareandset until successful         for (;;) {            intCurrent =get (); //CAS operation, expected value current vs. in-memory value, equal to NewValue value//otherwise the next loop, call get () to get the current value, continue to perform the CAS operation until successful            if(Compareandset (current, newvalue))returnCurrent ; }    }    /*** CAs operation, modern CPU has been widely supported, is an atomic operation; * Simply put, when the expected value expect is equal to the value at the Valueoffset address, set to the update value*/     Public Final BooleanCompareandset (intExpectintupdate) {        returnUnsafe.compareandswapint ( This, Valueoffset, expect, update); }    /*** View the source code, and Compareandset method consistent, puzzled?? */     Public Final BooleanWeakcompareandset (intExpectintupdate) {        returnUnsafe.compareandswapint ( This, Valueoffset, expect, update); }    /*** Atomic operation, self-increment, return old value*/     Public Final intgetandincrement () { for (;;) {            intCurrent =get (); intNext = current + 1; if(Compareandset (current, next))returnCurrent ; }    }    /*** Atomic operation, self-subtraction, return old value*/     Public Final intgetanddecrement () { for (;;) {            intCurrent =get (); intNext = current-1; if(Compareandset (current, next))returnCurrent ; }    }    /*** Atomic operation, plus one number, returns the old value*/     Public Final intGetandadd (intDelta) {         for (;;) {            intCurrent =get (); intNext = current +Delta; if(Compareandset (current, next))returnCurrent ; }    }    /*** Atomic operation, self-increment, return new value*/     Public Final intIncrementandget () { for (;;) {            intCurrent =get (); intNext = current + 1; if(Compareandset (current, next))returnNext; }    }    /*** Atomic operation, self-subtraction, return new value*/     Public Final intDecrementandget () { for (;;) {            intCurrent =get (); intNext = current-1; if(Compareandset (current, next))returnNext; }    }    /*** Atomic operation, plus one number, returns the new value*/     Public Final intAddandget (intDelta) {         for (;;) {            intCurrent =get (); intNext = current +Delta; if(Compareandset (current, next))returnNext; }    }    /*** Returns The String representation of the current value. * @returnThe String representation of the current value. */     PublicString toString () {returninteger.tostring (Get ()); }     Public intintvalue () {returnget (); }     Public LongLongvalue () {return(Long) get (); }     Public floatFloatvalue () {return(float) get (); }     Public DoubleDoublevalue () {return(Double) get (); }}

"Java concurrency" Atomicinteger source Comment

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.