Wednesday, July 3, 2024
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()

Nokonwaba Nkukhwana
Experience as a skilled Java developer and proven expertise in using tools and technical developments to drive improvements throughout a entire software development life cycle. I have extensive industry and full life cycle experience in a java based environment, along with exceptional analytical, design and problem solving capabilities combined with excellent communication skills and ability to work alongside teams to define and refine new functionality. Currently working in springboot projects(microservices). Considering the fact that change is good, I am always keen to new challenges and growth to sharpen my skills.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments