AtomicReference get() method in Java with Examples
Last Updated :
27 Dec, 2019
The get() method of a AtomicReference class is used to return the value of this AtomicReference object with memory semantics of reading as if the variable was declared volatile type of variable.
Syntax:
public final V get()
Parameters: This method accepts nothing.
Return value: This method returns current value of this AtomicReference.
Below programs illustrate the get() method:
Program 1:
import java.util.concurrent.atomic.AtomicReference;
public class GFG {
public static void main(String[] args)
{
AtomicReference<Integer> ref
= new AtomicReference<Integer>();
ref.set( 12 );
int value = ref.get();
System.out.println( "value = " + value);
}
}
|
Program 2:
import java.util.concurrent.atomic.AtomicReference;
public class GFG {
public static void main(String[] args)
{
AtomicReference<String> ref
= new AtomicReference<String>();
ref.set( "GEEKS FOR GEEKS" );
String value = ref.get();
System.out.println( "V = " + value);
}
}
|
Output:
V = GEEKS FOR GEEKS
Reference: https://docs.oracle.com/javase/10/docs/api/java/util/concurrent/atomic/AtomicReference.html#get()
Share your thoughts in the comments
Please Login to comment...