Java atomiclongarray

https‮w//:‬ww.theitroad.com

AtomicLongArray is a class in Java's java.util.concurrent.atomic package that provides atomic operations on arrays of 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 AtomicLongArray class provides the following atomic operations on arrays of long values:

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

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