AtomicReferenceArray getOpaque() method in Java with Examples
Last Updated :
02 Jun, 2020
The getOpaque() method of a AtomicReferenceArray class is used to return the value of the element at index i for this AtomicReferenceArray 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 E getOpaque(int i)
Parameters: This method accepts the index i to get the value.
Return value: This method returns the current value at index i.
Below programs illustrate the getOpaque() method:
Program 1:
import java.util.concurrent.atomic.*;
public class GFG {
public static void main(String[] args)
{
AtomicReferenceArray<Integer> array
= new AtomicReferenceArray<Integer>( 5 );
array.set( 0 , 987 );
array.set( 1 , 988 );
array.set( 2 , 989 );
array.set( 3 , 986 );
for ( int i = 0 ; i < 4 ; i++) {
int value = array.getOpaque(i);
System.out.println( "value at "
+ i + " = "
+ value);
}
}
}
|
Output:
Program 2:
import java.util.concurrent.atomic.*;
public class GFG {
public static void main(String[] args)
{
String[] names
= { "AMAN" , "AMAR" , "SURAJ" };
AtomicReferenceArray<String> array
= new AtomicReferenceArray<String>(names);
for ( int i = 0 ; i < array.length(); i++) {
String value = array.getOpaque(i);
System.out.println( "value at "
+ i + " = "
+ value);
}
}
}
|
Output:
References: https://docs.oracle.com/javase/10/docs/api/java/util/concurrent/atomic/AtomicReferenceArray.html#getOpaque(int)
Share your thoughts in the comments
Please Login to comment...