TopBlend: Here is the first difference. There are 29 differences. is old. is new.

Package java.util.concurrent.atomic

A small toolkit of classes that support lock-free thread-safeprogramming on single variables.

See:
           Description

Class Summary
AtomicBoolean A boolean value that may be updated atomically.
AtomicInteger An int value that may be updated atomically.
AtomicIntegerArray An int array in which elements may be updated atomically.
AtomicIntegerFieldUpdater<T> A reflection-based utility that enables atomic updates to designated volatile int fields of designated classes.
AtomicLong A long value that may be updated atomically.
AtomicLongArray A long array in which elements may be updated atomically.
AtomicLongFieldUpdater<T> A reflection-based utility that enables atomic updates to designated volatile long fields of designated classes.
AtomicMarkableReference<V> An AtomicMarkableReference maintains an object reference along with a mark bit, that can be updated atomically.
AtomicReference<V> An object reference that may be updated atomically.
AtomicReferenceArray<E> An array of object references in which elements may be updated atomically.
AtomicReferenceFieldUpdater<T,V> A reflection-based utility that enables atomic updates to designated volatile reference fields of designated classes.
AtomicStampedReference<V> An AtomicStampedReference maintains an object reference along with an integer "stamp", that can be updated atomically.
 

Package java.util.concurrent.atomic Description

A small toolkit of classes that support lock-free thread-safeprogramming on single variables. In essence, the classes in thispackage extend the notion of volatile values, fields, andarray elements to those that also provide an atomic conditional updateoperation of the form:


  boolean compareAndSet(expectedValue, updateValue);

This method (which varies in argument types across differentclasses) atomically sets a variable to the updateValue if itcurrently holds the expectedValue , reporting true onsuccess. The classes in this package also contain methods to get andunconditionally set values, as well as a weaker conditional atomicupdate operation operation:


 
 boolean weakCompareAndSet(expectedValue, updateValue);
 
The weak version may be more efficient in the normal case, but differsin that any given invocation of weakCompareAndSet . The weak version may bemore efficient in the normal case, but differs in that any giveninvocation of method mayfail, even spuriously (that is, for no apparent reason). A weakCompareAndSet method may fail, evenspuriously (that is, for no apparent reason). A false returnmeans return means only that the operation may be retried if desired, ifdesired, relying onthe on the guarantee that repeated invocation when the variable thevariable holds expectedValue and no other thread is also attempting alsoattempting to setthe set the variable will eventually succeed.

The specifications of these methods enable implementations toemploy efficient machine-level atomic instructions that are availableon contemporary processors. However on some platforms, support mayentail some form of internal locking. Thus the methods are notstrictly guaranteed to be non-blocking --a thread may block transiently before performing the operation. non-blocking.

The classes and methods in this package provide a few relatedoperations and capabilities to form a small toolkit of commonconstructions using atomics.

Instances of classes AtomicBoolean , AtomicInteger , AtomicLong , and AtomicReference each provide access andupdates and updates to a single variable of variableof the corresponding type. Each classalso class also provides appropriate utility appropriateutility methods for that type. For example,classes example, classes AtomicLong and AtomicInteger AtomicInt provide atomicincrement atomic increment methods. One application is applicationis to generate sequence numbers,as numbers, as in:


class Sequencer {
  private AtomicLong sequenceNumber = new AtomicLong(0);
  public long next() { return sequenceNumber.getAndIncrement(); }
}

The memory effects for accesses and updates of atomics generally follow therules atomicsfollow the rules for volatiles:

In addition to classes representing single values, this packagecontains Updater classes that can be used to obtain compareAndSet operations on any selected volatile field of any selected class. AtomicReferenceFieldUpdater , AtomicIntegerFieldUpdater , and AtomicLongFieldUpdater arereflection-based are reflection-based utilities that provide thatprovide access to the associated fieldtypes. field types. These are mainly of use in usein atomic data structures in whichseveral which several volatile fields of the ofthe same node (for example, thelinks the links of a tree node) are independently areindependently subject to atomicupdates. atomic updates. These classes enable greater flexibility greaterflexibility in how and when touse to use atomic updates, at the expense of more ofmore awkward reflection-basedsetup, reflection-based setup, less convenient usage, and weaker guarantees. weakerguarantees.

The AtomicIntegerArray , AtomicLongArray , and AtomicReferenceArray classes furtherextend further extend atomic operation support operationsupport to arrays of these types. Theseclasses These classes are also notable in providing inproviding volatile accesssemantics access semantics for their array elements, which elements,which is not supported forordinary for ordinary arrays.

The AtomicMarkableReference class associates a single boolean singleboolean with a reference. For example, thisbit this bit might be used inside a data adata structure to mean that the objectbeing object being referenced has logically been logicallybeen deleted. The AtomicStampedReference class associatesan integer associates aninteger value with a reference. This may be used for example, torepresent version numbers corresponding to series of updates.

Atomic classes Atomics are not used very often in application-level classes.They are designed primarily as building blocks forimplementing non-blocking for implementingnon-blocking data structures and related infrastructureclasses. infrastructure classes. The compareAndSet method is not a generalreplacement for locking. general replacement forlocking. It applies only when critical updates for anobject are confined an object areconfined to a single variable.

Atomic And atomic classes are not general notgeneral purpose replacements for java.lang.Integer and related andrelated classes. They do notdefine NOT define methods such as hashCode and compareTo . (Because atomic variables are variablesare expected to bemutated, be mutated, they are poor choices for hash table keys.) Additionally,classes tablekeys.) Additionally, classes are provided only for those types that are thatare commonly useful inintended applications. as atomics. For example, there is no atomic class forrepresenting classfor representing byte . In those infrequent cases where you wouldlike youwould like to do so, you can use an AtomicInteger to hold byte values, and cast appropriately. You can also hold floatsusing Similarly, you can holdfloating point types using Float.floatToIntBits and Float.intBitstoFloat conversions, and doubles using Double.doubleToLongBits and Double.longBitsToDouble conversions.

Since:
1.5