Java中的 AtomicInteger get() 方法及示例
Java.util.concurrent.atomic.AtomicInteger.get()是Java中的一个内置方法,它返回日期类型 int 的当前值。
句法:
public final int get()
参数:该函数不接受任何参数。
返回值:函数返回当前值
下面的程序演示了该函数:
方案一:
// Java Program to demonstrates
// the get() function
import java.util.concurrent.atomic.AtomicInteger;
public class GFG {
public static void main(String args[])
{
// Initially value as 0
AtomicInteger val
= new AtomicInteger(0);
val.addAndGet(7);
// Prints the updated value
System.out.println("Previous value: "
+ val);
// Gets the current value
int res = val.get();
System.out.println("current value: "
+ res);
}
}
输出:
Previous value: 7
current value: 7
方案二:
// Java Program to demonstrates
// the get() function
import java.util.concurrent.atomic.AtomicInteger;
public class GFG {
public static void main(String args[])
{
// Initially value as 18
AtomicInteger val = new AtomicInteger(18);
val.addAndGet(7);
// Prints the updated value
System.out.println("Previous value: " + val);
// Gets the current value
int res = val.get();
System.out.println("current value: " + res);
}
}
输出:
Previous value: 25
current value: 25
参考: https: Java/util/concurrent/atomic/AtomicInteger.html#get–