Implementation of the middle class subclass in Java

Source: Internet
Author: User

The atomic class provided by Java is implemented by Sun based on CAs, which is an optimistic lock. About optimistic lock and pessimistic lock.

The atomic variable class is equivalent to a generalized volatile variable that supports both atomic and conditional read-write operations. Atomicinteger represents a value of type int and provides the Get and set methods, which have the same memory semantics on read and write for the volatile type of int variables. It also provides an atomic Compareandset method (if the method executes successfully, it implements the same memory effect as reading/writing a volatile variable), as well as the addition, increment, and decrement of atoms. The atomicinteger surface is very much like an extended counter class, but provides greater scalability in the event of competition, as it directly leverages hardware-to-concurrency support.

The realization of Atomicinteger

Atomicinteger is an Integer class that supports atomic operations, which is to ensure that the increment and decrease of the Atomicinteger type variable is atomic, and there is no data inconsistency problem with multiple threads. If you do not use Atomicinteger, to implement an ID that is obtained sequentially, you must lock the operation on each fetch to avoid the phenomenon of getting the same ID when concurrency occurs.

The next step in the source code is to see how Atomicinteger is implemented in atomic operations.

First look at the Incrementandget () method, the following is the specific code:

 Public Final int Incrementandget () {        for  (;;) {            int current = get ();             int Next = current + 1;             if (Compareandset (current, next))                 return next;        } }

Through the source code, you can know that this method is to get to the current Value property values, and then add value 1, assigned to a local next variable, however, these two steps are non-thread-safe, but there is a dead loop inside, continue to do compareandset operation, Until successful, that is, the modification of the root in the Compareandset method, the code of the Compareandset () method is as follows:

 Public Final boolean compareandset (intint  update) {        return Unsafe.compareandswapint (This, Valueoffset, expect, update);}

The declaration of the Compareandswapint () method of the Compareandset () method call is a native method.

 Public Final native Boolean Long int VAR4, INTVAR5);

Compareandset the value that is obtained when the method is executed, next is the value after 1, and Compareandset is done with the Compareandswapint method that calls Sun's UnSafe, which is Nati ve method,Compareandswapint is based on the CPU of the CAS directive to achieve . So CAS-based operations can be considered non-blocking, and failure or suspension of one thread will not cause other threads to fail or hang. and because CAS operations are CPU primitives, performance is better .

Similarly, there is the Decrementandget () method. It differs from Incrementandget () by the value minus 1, which is assigned to the next variable.

Atomicinteger also have the getandincrement () and Getanddecrement () methods, their implementation principle and the above two methods exactly the same, the difference is the return value is different, the first two methods return is the value after the change, that is, next. The two methods return the value before the change, that is, current. There are a lot of other ways to do it.

Implementation of the middle class subclass in Java

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.