Wednesday, October 22, 2025
HomeLanguagesJavaAtomicReference getOpaque() method in Java with Examples

AtomicReference getOpaque() method in Java with Examples

The getOpaque() method of a AtomicReference class is used to return the current value of AtomicReference object, with memory effects as specified by VarHandle.getOpaque(java.lang.Object…). This VarHandle.getOpaque(java.lang.Object…) method handles operation with no assurance of memory ordering effects with respect to other threads.

Syntax:

public final V getOpaque()

Parameters: This method accepts nothing.

Return value: This method returns the value of AtomicReference.

Below programs illustrate the getOpaque() method:
Program 1:




// Java program to demonstrate
// AtomicReference.getOpaque() method
import java.util.concurrent.atomic.AtomicReference;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // create an atomic reference object.
        AtomicReference<Double> ref
            = new AtomicReference<Double>();
  
        // set some value
        ref.set(564324.2345);
  
        // get value using getOpaque()
        double value = ref.getOpaque();
  
        // print
        System.out.println("Value = " + value);
    }
}


Output:

Program 2:




// Java program to demonstrate
// AtomicReference.getOpaque() method
import java.util.concurrent.atomic.AtomicReference;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // create an atomic reference object.
        AtomicReference<String> ref
            = new AtomicReference<String>();
  
        // set some value
        ref.set("AtomicReference old value");
  
        // get value getOpaque()
        String value = ref.getOpaque();
  
        // print
        System.out.println("Value = " + value);
    }
}


Output:

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

RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS