Simple example of Java atomic package

Example 1:

AtomicInteger shared = new AtomicInteger(1);
int increment(){
return shared.incrementAndGet();
}

Example 2:

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

Consider 10 threads are incrementing int i (initialized at 0) and outputting the value the console. You can get something like this:
1 2 2 3 3 5 6 6 8 8
AtomicInteger, for example, ensures that each thread can increment or decrement the value atomically, ensuring that the write operation happens in a synchronized manner, and for 10 threads, the output would always be:
1 2 3 4 5 6 7 8 9 10

Atomic package is a small toolkit of classes that support lock-free thread-safe programming on single variables. In essence, the classes in this package extend the notion of volatile values, fields, and array elements to those that also provide an atomic conditional update operation of the form:

   boolean compareAndSet(expectedValue, updateValue); 

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

Atomic classes are not general purpose replacements for java.lang.Integer and related classes. They do not define methods such as hashCode and compareTo. (Because atomic variables are expected to be mutated, they are poor choices for hash table keys.)

Class Summary 
ClassDescription
AtomicBoolean
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
long value that may be updated atomically.
AtomicLongArray
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.

Comments

Popular posts from this blog

Check MySQL query history from command line

Fixed: ImportError: No module named utilities.common.tools