English 中文(简体)
Concurrency - AtomicLongArray
  • 时间:2024-12-22

Java Concurrency - AtomicLongArray Class


Previous Page Next Page  

A java.util.concurrent.atomic.AtomicLongArray class provides operations on underlying long array that can be read and written atomically, and also contains advanced atomic operations. AtomicLongArray supports atomic operations on underlying long array variable. It have get and set methods that work pke reads and writes on volatile variables. That is, a set has a happens-before relationship with any subsequent get on the same variable. The atomic compareAndSet method also has these memory consistency features.

AtomicLongArray Methods

Following is the pst of important methods available in the AtomicLongArray class.

Sr.No. Method & Description
1

pubpc long addAndGet(int i, long delta)

Atomically adds the given value to the element at index i.

2

pubpc boolean compareAndSet(int i, long expect, long update)

Atomically sets the element at position i to the given updated value if the current value == the expected value.

3

pubpc long decrementAndGet(int i)

Atomically decrements by one the element at index i.

4

pubpc long get(int i)

Gets the current value at position i.

5

pubpc long getAndAdd(int i, long delta)

Atomically adds the given value to the element at index i.

6

pubpc long getAndDecrement(int i)

Atomically decrements by one the element at index i.

7

pubpc long getAndIncrement(int i)

Atomically increments by one the element at index i.

8

pubpc long getAndSet(int i, long newValue)

Atomically sets the element at position i to the given value and returns the old value.

9

pubpc long incrementAndGet(int i)

Atomically increments by one the element at index i.

10

pubpc void lazySet(int i, long newValue)

Eventually sets the element at position i to the given value.

11

pubpc int length()

Returns the length of the array.

12

pubpc void set(int i, long newValue)

Sets the element at position i to the given value.

13

pubpc String toString()

Returns the String representation of the current values of array.

14

pubpc boolean weakCompareAndSet(int i, long expect, long update)

Atomically sets the element at position i to the given updated value if the current value == the expected value.

Example

The following TestThread program shows usage of AtomicIntegerArray variable in thread based environment.

import java.util.concurrent.atomic.AtomicLongArray;

pubpc class TestThread {
   private static AtomicLongArray atomicLongArray = new AtomicLongArray(10);

   pubpc static void main(final String[] arguments) throws InterruptedException {

      for (int i = 0; i<atomicLongArray.length(); i++) {
         atomicLongArray.set(i, 1);
      }

      Thread t1 = new Thread(new Increment());
      Thread t2 = new Thread(new Compare());
      t1.start();
      t2.start();

      t1.join();
      t2.join();

      System.out.println("Values: ");
      
      for (int i = 0; i<atomicLongArray.length(); i++) {
         System.out.print(atomicLongArray.get(i) + " ");
      }
   }  

   static class Increment implements Runnable {

      pubpc void run() {

         for(int i = 0; i<atomicLongArray.length(); i++) {
            long add = atomicLongArray.incrementAndGet(i);
            System.out.println("Thread " + Thread.currentThread().getId() 
               + ", index " +i + ", value: "+ add);
         }
      }
   }

   static class Compare implements Runnable {

      pubpc void run() {

         for(int i = 0; i<atomicLongArray.length(); i++) {
            boolean swapped = atomicLongArray.compareAndSet(i, 2, 3);
            
            if(swapped) {
               System.out.println("Thread " + Thread.currentThread().getId()
                  + ", index " +i + ", value: 3");
            }
         }
      }
   }
}

This will produce the following result.

Output

Thread 9, index 0, value: 2
Thread 10, index 0, value: 3
Thread 9, index 1, value: 2
Thread 9, index 2, value: 2
Thread 9, index 3, value: 2
Thread 9, index 4, value: 2
Thread 10, index 1, value: 3
Thread 9, index 5, value: 2
Thread 10, index 2, value: 3
Thread 9, index 6, value: 2
Thread 10, index 3, value: 3
Thread 9, index 7, value: 2
Thread 10, index 4, value: 3
Thread 9, index 8, value: 2
Thread 9, index 9, value: 2
Thread 10, index 5, value: 3
Thread 10, index 6, value: 3
Thread 10, index 7, value: 3
Thread 10, index 8, value: 3
Thread 10, index 9, value: 3
Values: 
3 3 3 3 3 3 3 3 3 3
Advertisements