Java Atomicinteger Source Code Analysis __java

Source: Internet
Author: User

Add the Java.util.concurrent (J.U.C) package to JDK1.5, which is built on top of CAs. A common implementation of the CAS non-blocking algorithm, which has a better performance compared to the synchronized blocking algorithm. optimistic Lock

Optimistic lock (optimistic locking) is actually a kind of thought. Relative pessimistic lock, optimistic lock hypothesis that the data will not create conflicts in general, so when the data is submitted to update, it will formally test the conflict or not, if found conflict, so that the return of the user error information, let the user decide how to do.

The concept of optimistic locking mentioned above has actually elaborated on his specific implementation details: Two steps: conflict detection and Data update. A typical way to implement this is compare and Swap (CAS). CAS

CAS is an optimistic lock technology that when multiple threads try to update the same variable with CAs, only one of the threads can update the value of the variable, while the other threads fail, and the failed thread is not suspended, but is told that the competition has failed and can be tried again.

The CAS operation contains three operands-memory location (V), expected original value (A), and new value (B). If the value of the memory location matches the expected original value, the processor automatically updates the location value to the new value. Otherwise, the processor does nothing. the realization of Atomicinteger.

Atomicinteger is an Integer class that supports atomic operations, which guarantees that the addition and reduction of the type int variable is atomic.

Atomicinteger source code (based on JDK1.7 source code, and made a partial deletion) as follows:

Package java.util.concurrent.atomic;  public class Atomicinteger extends number implements Java.io.Serializable {private static final long Serialversionuid
    = 6214790243416807050L;

    Setup to use Unsafe.compareandswapint for updates private static final Unsafe Unsafe = Unsafe.getunsafe ();

    private volatile int value;
    Public Atomicinteger (int initialvalue) {value = InitialValue;
    Public Atomicinteger () {} public final int get () {return value; Public final Boolean compareandset (int expect, int update) {return Unsafe.compareandswapint (this, valueoff
    Set, expect, update); Public final int getandincrement () {for (;;)
            {int current = get ();
            int next = current + 1;
        if (Compareandset, next) return to current; } public final int getanddecrement () {for (;;)
            {int current = get (); int next =current-1;
        if (Compareandset, next) return to current; }
    }
}

In the Getandincrement method, it is done by first obtaining the current Value property, then adding value 1 to a local next variable, however, both of these steps are not thread safe, but there is a dead loop inside. Continue to do compareandset operations, until successful, that is, the fundamental changes in the Compareandset method.

We can see that the call in the Compareandset () method is Sun.misc.Unsafe.compareAndSwapInt (Object obj, long valueoffset, int expect, int update) Method
Compareandswapint is based on the CPU's CAS directives. Therefore, CAS-based operations can be considered non-blocking, and the failure or suspension of one thread does not cause other threads to fail or hang. And because CAS operations are CPU primitives, performance is better.

Similarly, there is the Getanddecrement () method. The difference between this and getandincrement () is to subtract value by 1 and assign it to the next variable.

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.