What is the AtomicBoolean.get() method in Java?

AtomicBoolean represents a boolean value that can be updated atomicallyAn atomic operation performs a single unit of work on a resource. No other operations are allowed on the same resource until the performing operation is finished..

AtomicBoolean is present in the java.util.concurrent.atomic package.

The get method of AtomicBoolean can be used to get the value present in the AtomicBoolean object.

Syntax

public final boolean get()

Arguments

This method takes no arguments.

Return value

This method returns the value present in the AtomicBoolean object.

Example

The below code demonstrates how to use the get method:

import java.util.concurrent.atomic.AtomicBoolean;
class Get{
public static void main(String[] args) {
AtomicBoolean atomicBoolean = new AtomicBoolean(true);
boolean val = atomicBoolean.get();
System.out.println("The value in the atomicBoolean object is " + val);
}
}

Explanation

  • Line 1: We import the AtomicBoolean class.
  • Line 4: We create a new object for the AtomicBoolean class with the name atomicBoolean and with the value true.
  • Line 5: We use the get method of the atomicBoolean and store it to the boolean variable val, then print the value.

Free Resources