Java.util.concurrent.atomic.AtomicLong.incrementAndGet()是Java中的一种内置方法,该方法将先前值增加1,并在更新后返回long数据类型的值。
    用法:
    public final long incrementAndGet()

    参数:该函数不接受单个参数。
    返回值:该函数将执行增量运算之后的值返回。
    以下示例程序旨在说明上述方法:
    示例1:

    1. // Java program that demonstrates
    2. // the incrementAndGet() function
    3. import java.util.concurrent.atomic.AtomicLong;
    4. public class GFG {
    5. public static void main(String args[])
    6. {
    7. // Initially value as 0
    8. AtomicLong val
    9. = new AtomicLong(0);
    10. System.out.println("Previous value: "
    11. + val);
    12. // Incremenet and get
    13. long res
    14. = val.incrementAndGet();
    15. // Prints the updated value
    16. System.out.println("Current value: "
    17. + res);
    18. }
    19. }

    输出:
    Previous value: 0
    Current value: 1

    示例2:

    1. // Java program that demonstrates
    2. // the incrementAndGet() function
    3. import java.util.concurrent.atomic.AtomicLong;
    4. public class GFG {
    5. public static void main(String args[])
    6. {
    7. // Initially value as 18
    8. AtomicLong val
    9. = new AtomicLong(18);
    10. System.out.println("Previous value: "
    11. + val);
    12. // Incremenet and get new value
    13. long res = val.incrementAndGet();
    14. // Prints the updated value
    15. System.out.println("Current value: "
    16. + res);
    17. }
    18. }

    输出:
    Previous value: 18
    Current value: 19