Java.util.concurrent.atomic.AtomicLongArray.getAndIncrement()是Java中的一種內置方法,可以自動將AtomicLongArray的任何索引上存在的值加1。該方法將AtomicLongArray的索引值作為參數,在遞增之前返回該索引處的值,然後在該索引處遞增該值。函數getAndIncrement()與incrementAndGet()函數相似,但前者返回增量前的值,而後者返回增量操作後的值。
用法:
public final long getAndIncrement(int i)
參數:該函數接受單個參數i,該參數是執行一個操作遞增的索引。
返回值:函數將在很長的更新之前返回該索引處的先前值。
以下示例程序旨在說明上述方法:
示例1:
// Java program that demonstrates
// the getAndIncrement() function
import java.util.concurrent.atomic.AtomicLongArray;
public class GFG {
public static void main(String args[])
{
// Initializing an array
long a[] = { 11, 12, 13, 14, 15 };
// Initializing an AtomicLongArray with array a
AtomicLongArray arr = new AtomicLongArray(a);
// Displaying the AtomicLongArray
System.out.println("The array : " + arr);
// Index where operation is performed
int idx = 0;
// Updating the value at
// idx applying getAndIncrement
// and storing the previous value
long prev = arr.getAndIncrement(idx);
// Previous value at idx before update
System.out.println("Value at index " + idx
+ " before the update is "
+ prev);
// Displaying the AtomicLongArray
System.out.println("The array after update : "
+ arr);
}
}
輸出:
The array : [11, 12, 13, 14, 15] Value at index 0 before the update is 11 The array after update : [12, 12, 13, 14, 15]
示例2:
// Java program that demonstrates
// the getAndIncrement() function
import java.util.concurrent.atomic.AtomicLongArray;
public class GFG {
public static void main(String args[])
{
// Initializing an array
long a[] = { 11, 12, 13, 14, 15 };
// Initializing an AtomicLongArray with array a
AtomicLongArray arr = new AtomicLongArray(a);
// Displaying the AtomicLongArray
System.out.println("The array : " + arr);
// Index where operation is performed
int idx = 3;
// Updating the value at
// idx applying getAndIncrement
// and storing the previous value
long prev = arr.getAndIncrement(idx);
// Previous value at idx before update
System.out.println("Value at index " + idx
+ " before the update is "
+ prev);
// Displaying the AtomicLongArray
System.out.println("The array after update : "
+ arr);
}
}
輸出:
The array : [11, 12, 13, 14, 15] Value at index 3 before the update is 14 The array after update : [11, 12, 13, 15, 15]
注意:函數getAndIncrement()與incrementAndGet()類似,但後一個函數返回更新後的值,而前一個函數在更新後返回前一個值。
相關用法
- Java AtomicLong getAndIncrement()用法及代碼示例
- Java AtomicIntegerArray getAndIncrement()用法及代碼示例
- Java AtomicInteger getAndIncrement()用法及代碼示例
- Java AtomicLongArray get()用法及代碼示例
- Java AtomicLongArray set()用法及代碼示例
- Java AtomicLongArray length()用法及代碼示例
- Java AtomicLongArray getAndAccumulate()用法及代碼示例
- Java AtomicLongArray getAndDecrement()用法及代碼示例
- Java AtomicLongArray lazySet()用法及代碼示例
- Java AtomicLongArray getAndUpdate()用法及代碼示例
- Java AtomicLongArray getAndAdd()用法及代碼示例
- Java AtomicLongArray updateAndGet()用法及代碼示例
- Java AtomicLongArray incrementAndGet()用法及代碼示例
- Java AtomicLongArray compareAndSet()用法及代碼示例
- Java AtomicLongArray accumulateAndGet()用法及代碼示例
注:本文由純淨天空篩選整理自rupesh_rao大神的英文原創作品 AtomicLongArray getAndIncrement() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。