AtomicReferenceArray

The AtomicReferenceArray in the java.util.concurrent.atomic package is an array of object references which can be updated atomically. The AtomicReferenceArray class also supports compare-and-swap functionality.

Creating an AtomicReferenceArray

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

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

AtomicReferenceArray array = new AtomicReferenceArray(10);

This example creates a AtomicReferenceArray with a capacity of 10 (it has space for 10 object references as elements).

The second constructor takes a E[] array as parameter, where E is the type (class) of the object references. The AtomicReferenceArray 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 AtomicReferenceArray. Here is a Java example of creating an AtomicReferenceArray using this constructor:

Object[] source = new Object[10];

source[5] = "Some string";

AtomicReferenceArray array = new AtomicReferenceArray(source);

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

You can also set a generic type for the AtomicReferenceArray. Here is the example from above, where the generic type is set to String:

String[] source = new String[10];

source[5] = "Some string";

AtomicReferenceArray<String> array = 
    new AtomicReferenceArray<String>(source);

get()

The get() method returns the value of the element with the given index. The index is passed as parameter to the get() method. Here is an example:

Object element = array.get(5);

If the AtomicReferenceArray has a generic type, the get() method returns objects of that type. For instance, if the generic type is String, then you can call get() like this:

String element = array.get(5);

Notice that no casting to String is necessary.

set()

The set() method sets the value of an element with a specific index. The index and value is passed as parameters to the set() method. Here is a set() example:

array.set(5, "another object");

The first parameter is the index of the element to set. The second parameter is the value to set for the element. If the AtomicReferenceArray has a generic type, the type of this parameter will be that type. Otherwise the type is Object.

compareAndSet()

The compareAndSet() method of the AtomicReferenceArray can compare the current reference stored in a given element with an expected reference, and if the references are the same, swap the current reference with a new reference. Here is a compareAndSwap() example:

String string1 = "string1";
String string2 = "string2";
    
String[] source = new String[10];
source[5] = string1;

AtomicReferenceArray<String> array = 
    new AtomicReferenceArray<String>(source);
    

array.compareAndSet(5, string1, string2);

This example first creates a String array and sets the element with index 5 to point to the string1 reference. Then, it compares the value of the element with index 5 to the string1 reference, and if they are the same, sets the reference of the element to string2. If no other thread has changed the reference of the element with index 5 (which is not possible in the situation above), then the setting succeeds (the new reference is set).

Additional Methods

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

Leave a Comment