AtomicInteger.get()
时间: 2023-10-19 11:15:26 浏览: 81
AtomicInteger.get() is a method in the Java programming language that retrieves the current value of an AtomicInteger object. It returns the current value as an int.
An AtomicInteger is a class in Java that provides atomic operations on an int value. It can be used in a multithreaded environment to ensure that operations on the int value are atomic and thread-safe.
The get() method is one of the basic methods provided by the AtomicInteger class to read the current value of an AtomicInteger object. It does not modify the value of the AtomicInteger object.
Here is an example of how to use the get() method:
```
import java.util.concurrent.atomic.AtomicInteger;
public class AtomicIntegerExample {
public static void main(String[] args) {
AtomicInteger counter = new AtomicInteger(0);
int value = counter.get(); // retrieve the current value of the counter
System.out.println("Counter value is " + value); // prints "Counter value is 0"
}
}
```
In this example, we create a new AtomicInteger object with an initial value of 0. We then use the get() method to retrieve the current value of the counter and store it in the variable named "value". Finally, we print out the value of the counter. Since the counter has not been modified yet, the value printed is 0.
阅读全文