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