Sunday, November 17, 2024
Google search engine
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

Recent Comments