Skip to content
Related Articles

Related Articles

AtomicReference getAcquire() method in Java with Examples
  • Last Updated : 27 Dec, 2019

The getAcquire() method of a AtomicReference class is used to return the value of this AtomicReference object with memory ordering effects on geting variable is compatible with memory_order_acquire ordering.

Syntax:

public final V getAcquire()

Parameters: This method accepts nothing.

Return value: This method returns current value of this AtomicReference object with memory ordering effects.

Below programs illustrate the get() method:



Program 1:




// Java program to demonstrate
// AtomicReference.getAcquire() method
  
import java.util.concurrent.atomic.AtomicReference;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // create an atomic reference object
        // which stores Integer.
        AtomicReference<Integer> ref
            = new AtomicReference<Integer>();
  
        // set some value
        ref.set(1632798372);
  
        // apply getAcquire() and print value
        int value = ref.getAcquire();
        System.out.println("value  = " + value);
    }
}
Output:

Program 2:




// Java program to demonstrate
// AtomicReference.get() method
import java.util.concurrent.atomic.AtomicReference;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // create an atomic reference object
        // which stores String.
        AtomicReference<String> ref
            = new AtomicReference<String>();
  
        // set some value
        ref.set("WELCOME TO "
                + "GEEKS FOR GEEKS"
                + " LERAN AND APPLY");
  
        // apply getAcquire and print value
        String value = ref.getAcquire();
        System.out.println(value);
    }
}
Output:

Reference: https://docs.oracle.com/javase/10/docs/api/java/util/concurrent/atomic/AtomicReference.html#getAcquire()

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :