The Java.util.concurrent.atomic.AtomicBoolean.getAndSet() is an inbuilt method in java that sets the given value to the value passed in the parameter and returns the value before updation which is of data-type boolean.
Syntax:
public final boolean getAndSet(boolean val)
Parameters: The function accepts a single mandatory parameter val which specifies the value to be updated.
Return Value: The function returns the value before update operation is performed to the previous value.
Below programs illustrate the above method:
Program 1:
import java.util.concurrent.atomic.AtomicBoolean;
public class GFG {
public static void main(String args[])
{
AtomicBoolean val = new AtomicBoolean( false );
boolean res
= val.getAndSet( true );
System.out.println( "Previous value: "
+ res);
System.out.println( "Current value: "
+ val);
}
}
|
Output:
Previous value: false
Current value: true
Program 2:
import java.util.concurrent.atomic.AtomicBoolean;
public class GFG {
public static void main(String args[])
{
AtomicBoolean val = new AtomicBoolean( true );
boolean res = val.getAndSet( false );
System.out.println( "Previous value: "
+ res);
System.out.println( "Current value: "
+ val);
}
}
|
Output:
Previous value: true
Current value: false
Reference: https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/atomic/AtomicBoolean.html#getAndSet-boolean-
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
27 Feb, 2019
Like Article
Save Article