AtomicLongArray

Jakob Jenkov
Last update: 2015-05-25

The Java AtomicLongArray class (java.util.concurrent.atomic.AtomicLongArray) represents an array of long . The long elements in the AtomicLongArray can be updated atomically. The AtomicLongArray also supports compare-and-swap functionality.

Creating an AtomicLongArray

You can create an AtomicLongArray using one of its two constructors.

The first constructor takes an int as parameter. This int specifies the length of the AtomicLongArray to create, meaning how many elements it should have space for. Here is a Java example of creating an AtomicLongArray using this constructor:

AtomicLongArray array = new AtomicLongArray(10);

This example creates a AtomicLongArray with a capacity of 10 longs (it has space for 10 long elements).

The second constructor takes a long[] array as parameter. The AtomicLongArray created using this constructor will have the same capacity as the array parameter, and all elements from the array parameter will be copied into the AtomicLongArray. Here is a Java example of creating an AtomicLongArray using this constructor:

long[] longs = new long[10];

longs[5] = 123;

AtomicLongArray array = new AtomicLongArray(longs);

This example first creates a long[] array and sets a value into the element with index 5. Then it creates an AtomicLongArray with the long[] array as parameter.

get()

You can get the value of a given element using the get() method on the AtomicLongArray. Here is an example showing how calling the get() method looks:

long value = array.get(5);

set()

You can set the value of a given element using the set() method on the AtomicLongArray. Here is an example showing how calling the set() method looks:

array.set(5, 999);

compareAndSet()

The compareAndSet() method is used to compare the value of a given element with a specified value, and if the two values are equal, set a new value for that element. This is an example of the atomic compare-and-swap functionality support by the AtomicLongArray. Only one thread at a time can execute the compareAndSet() method.

Here is how calling the compareAndSet() method of the AtomicLongArray:

boolean swapped = array.compareAndSet(5, 999, 123);

This example compares the element with index 5 to the expected value 999, and if equal, sets the new value 123 for the element with index 5. The compareAndSet() method returns a boolean with the value true if the element had a new value set, and false if not (if the element did not have the expected value).

addAndGet()

The AtomicLongArray also contains a method that can be used to add a value to a given element, and return the new value of the element. Calling addAndGet() is also an atomic operation (like all operations on the AtomicLongArray). Here is an example of calling addAndGet() :

long newValue = array.addAndGet(5, 3);

After executing this code the newValue variable would contain the value of the element with index 5 with 3 added to it.

getAndAdd()

The AtomicLongArray class also contains a method named getAndAdd(). The getAndAdd() method does the same as the addAndGet() method, except the getAndAdd() method returns the value of the element before a value is added to it. Here is a getAndAdd() example:

long oldValue = array.getAndAdd(5, 3);

After executing this code the oldValue variable would contain the old value of the element with index 5, before the value 3 was added to it.

incrementAndGet()

The incrementAndGet() method increments (adds 1) to the value of a given element and returns the new value of that element. Here is a incrementAndGet() example:

long newValue = array.incrementAndGet(5);

After executing this code the newValue variable would contain the value of the element with index 5 with 1 added to it.

getAndIncrement()

The AtomicLongArray class also contains a method named getAndIncrement(). The getAndIncrement() method does the same as the incrementAndGet() method, except the getAndIncrement() method returns the value of the element before it is incremented. Here is a getAndIncrement() example:

long oldValue = array.getAndIncrement(5);

After executing this code the oldValue variable would contain the old value of the element with index 5, before it was incremented (1 was added to it).

decrementAndGet()

The decrementAndGet() method decrements (subtracts 1) to the value of a given element and returns the new value of that element. Here is a decrementAndGet() example:

long newValue = array.decrementAndGet(5);

After executing this code the newValue variable would contain the value of the element with index 5 with 1 subtracted from it.

getAndDecrement()

The AtomicLongArray class also contains a method named getAndDecrement(). The getAndDecrement() method does the same as the decrementAndGet() method, except the getAndDecrement() method returns the value of the element before it is decremented. Here is a getAndDecrement() example:

long oldValue = array.getAndDecrement(5);

After executing this code the oldValue variable would contain the old value of the element with index 5, before it was decremented (1 was subtracted from it).

Additional Methods

The AtomicLongArray has several more methods that you can use for special purposes. You should check out the JavaDoc for the AtomicLongArray class to learn more about those methods.

Jakob Jenkov

Featured Videos

Java Generics

Java ForkJoinPool

P2P Networks Introduction



















Close TOC
All Tutorial Trails
All Trails
Table of contents (TOC) for this tutorial trail
Trail TOC
Table of contents (TOC) for this tutorial
Page TOC
Previous tutorial in this tutorial trail
Previous
Next tutorial in this tutorial trail
Next