Atomic variables

The AtomicBoolean, AtomicInteger, AtomicLong, and AtomicReference support lock-free thread-safe programming on single variables providing atomic operations such as increment.

The AtomicIntegerArray, AtomicLongArray, and AtomicReferenceArray classes further extend support for atomic operations for these types of arrays. In addition, the elements in these arrays are volatile.

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