JAVA concurrent API source code parsing: Atomic class, api source code

Source: Internet
Author: User

JAVA concurrent API source code parsing: Atomic class, api source code

The JAVA. util. concurrent. atomic package of java APIs provides a series of classes that do not need to be synchronized when the concurrency is based on the basic type packaging class (implemented using hardware-related commands ).

First, let's look at an example of 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 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, valueOffset, expect, update);    }    public final int getAndIncrement() {        for (;;) {            int current = get();            int next = current + 1;            if (compareAndSet(current, next))                return current;        }    }    public final int getAndDecrement() {        for (;;) {            int current = get();            int next = current - 1;            if (compareAndSet(current, next))                return current;        }    }    public final int getAndAdd(int delta) {        for (;;) {            int current = get();            int next = current + delta;            if (compareAndSet(current, next))                return 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 synchronization mechanism in the source code, and there is no lock. In fact, its synchronization is implemented through hardware commands. The internal method of Integer is NOT thread-safe. Therefore, we recommend that you use the AtomicInteger class to replace the packaging class for concurrent programming, but the atomic class is not java. lang. general Replacement Methods of Integer and related classes. They do not define methods such as hashCode and compareTo. For example, if AtomicDouble is not provided, developers need to convert Double. doubleToLongBits and Double. longBitsToDouble to keep the double value.

This package also contains the Updater class, which can be used to obtain the compareAndSet operation on any volatile field of any selected class. Solves the security problem of non-atomic operations on volatile fields.

Related Article

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.