Java atomiclong

AtomicLong is a class in Java's java.util.concurrent.atomic package that provides atomic operations on long values. An atomic operation is an operation that is performed as a single, indivisible unit of execution, which means that it appears to occur instantaneously and cannot be interrupted by other operations.

The AtomicLong class provides the following atomic operations on long values:

  • get(): Returns the current value of the AtomicLong.
  • set(long newValue): Sets the value of the AtomicLong to the specified new value.
  • getAndSet(long newValue): Atomically sets the value of the AtomicLong to the specified new value and returns the previous value.
  • compareAndSet(long expect, long update): Atomically sets the value of the AtomicLong to the specified new value if the current value equals the expected value, and returns true if the update was successful.
  • incrementAndGet(): Atomically increments the value of the AtomicLong by one and returns the updated value.
  • decrementAndGet(): Atomically decrements the value of the AtomicLong by one and returns the updated value.
  • addAndGet(long delta): Atomically adds the specified delta to the value of the AtomicLong and returns the updated value.

These operations are useful for implementing thread-safe algorithms that require access to shared data. The AtomicLong class is especially useful for implementing lock-free data structures and algorithms that require high-performance concurrent access to long values.