java.util.concurrent.atomic.AtomicInteger.addandget()是Java中的一种内置方法,它将在函数的参数中传递的值添加到先前的值,并返回数据类型为int的新更新值。
用法:
public final int addAndGet(int val)
参数:该函数接受单个强制参数val,该参数指定要添加的值。
返回值:加完后,该函数返回整数值。
以下程序演示了该功能:
示例1:
// Java Program to demonstrates
// the addandget() function
import java.util.concurrent.atomic.AtomicInteger;
public class GFG {
public static void main(String args[])
{
// Initially value as 0
AtomicInteger val
= new AtomicInteger();
// Update the value
int c = val.addAndGet(6);
// Prints the updated value
System.out.println("Updated value: "
+ c);
}
}
输出:
Updated value: 6
示例2:
// Java Program to demonstrates
// the addandget() 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);
// Prints the updated value
System.out.println("Previous value: "
+ val);
// adds the value to 18
val.addAndGet(6);
// Prints the updated value
System.out.println("Updated value: "
+ val);
}
}
输出:
Previous value: 18
Updated value: 24