Java Concurrency API Source code parsing: Atomic Class

Source: Internet
Author: User

The Java API's Java.util.concurrent.atomic package provides a series of classes based on the basic type wrapper class that do not require synchronization (implemented with hardware-related directives).

First look at an example Automicinteger:

public class Atomicinteger extends number implements Java.io.Serializable {private static final long Serialversionuid    = 6214790243416807050L;    Private static final unsafe unsafe = unsafe.getunsafe ();    Private static final long valueoffset; static {try {Valueoffset = Unsafe.objectfieldoffset (AtomicInteger.class.getDeclaredField ("value")      ));    } catch (Exception ex) {throw new Error (ex);}    } private volatile int value;    Public Atomicinteger (int initialvalue) {value = InitialValue;    Public Atomicinteger () {} public final int get () {return value;    Public final void set (int newvalue) {value = newvalue;    } public final void Lazyset (int. newvalue) {Unsafe.putorderedint (this, valueoffset, newvalue); } public final int getandset (int. newvalue) {for (;;)            {int current = get ();        if (Compareandset (current, newvalue)) return to current; }    }   Public final Boolean compareandset (int expect, int. update) {return Unsafe.compareandswapint (this, Valueoffset,    expect, update); Public final Boolean weakcompareandset (int expect, int. update) {return Unsafe.compareandswapint (this, valueOf    Fset, expect, update); } public final int getandincrement () {for (;;)            {int current = get ();            int next = current + 1;        if (Compareandset (current, next)) return to current; }} public final int getanddecrement () {for (;;)            {int current = get ();            int next = current-1;        if (Compareandset (current, next)) return to current; }} public final int getandadd (int. delta) {for (;;)            {int current = get ();            int next = current + Delta;        if (Compareandset (current, next)) return to current; }} public final int incrementandget () {for (;;){int current = get ();            int next = current + 1;        if (Compareandset (current, next)) return next; }} public final int decrementandget () {for (;;)            {int current = get ();            int next = current-1;        if (Compareandset (current, next)) return next; }} public final int addandget (int. delta) {for (;;)            {int current = get ();            int next = current + Delta;        if (Compareandset (current, next)) return next;    }} public String toString () {return integer.tostring (get ());    } public int Intvalue () {return get ();    } public Long Longvalue () {return (long) get ();    } public float Floatvalue () {return (float) get ();    } public double Doublevalue () {return (double) get (); }}

There is no volatile or synchronized in the source code, these synchronization mechanisms are not locked, in fact, its synchronization is implemented through hardware instructions. The Integer internal method is not thread-safe, so concurrent programming recommends using the Atomicinteger class to replace the wrapper class, but the atomic class is not a generic replacement for Java.lang.Integer and related classes, and they do not define such hashcode and CompareTo and other methods. For example: Atomicdouble is not provided and developers need to use double.doubletolongbits and double.longbitstodouble transformations to maintain Double values.

This package also contains the Updater class, which can be used to get the Compareandset action on any selected volatile field of any selected class. Resolves security issues for non-atomic operations of volatile fields.

Java Concurrency API Source code parsing: Atomic Class

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.