Java.util.concurrent.atomic.AtomicLongArray.getAndUpdate()是Java中的一種內置方法,該方法將給定索引值上的給定更新函數後,對該AtomicLongArray任意索引處的值進行更新。該方法將AtomicLongArray的索引值和update函數作為參數,並通過對該值應用update函數來更新該索引處的值。該函數應無副作用,因為當嘗試更新由於線程間爭用而失敗時,可能會重新應用該函數。 getAndUpdate()和updateAndGet()函數之間的唯一區別是,前者返回更新前的值,而後者返回更新後的值。
用法:
public final long getAndUpdate(int i, LongUnaryOperator updateFunction)
參數:該函數接受兩個參數:
- i這是要進行更新的索引。
- updateFunction這是單個參數的更新函數,用於指示要進行的更新。
返回值:該函數返回一個long值,該值是應用指定的update函數後的值。
以下示例程序旨在說明上述方法:
示例1:
// Java program that demonstrates
// the getAndUpdate() function
import java.util.concurrent.atomic.AtomicLongArray;
import java.util.function.LongUnaryOperator;
public class GFG {
public static void main(String args[])
{
// Initializing an array
long a[] = { 1, 2, 3, 4, 5 };
// Initializing an AtomicLongArray with array a
AtomicLongArray arr = new AtomicLongArray(a);
// Displaying the AtomicLongArray
System.out.println("The array : " + arr);
// Index where update is to be made
int idx = 3;
// Declaring the updateFunction
LongUnaryOperator squaredFunction = (l) -> l * l;
// Updating the value at idx
// applying updateFunction and
// storing the previous value
long prev = arr.getAndUpdate(idx, squaredFunction);
// Displaying the previous value
System.out.println("The value before update"
+ " at index " + idx
+ " is " + prev);
// Displaying the AtomicLongArray
System.out.println("The array after update : "
+ arr);
}
}
輸出:
The array : [1, 2, 3, 4, 5] The value before update at index 3 is 4 The array after update : [1, 2, 3, 16, 5]
示例2:
// Java program that demonstrates
// the getAndUpdate() function
import java.util.concurrent.atomic.AtomicLongArray;
import java.util.function.LongUnaryOperator;
public class GFG {
public static void main(String args[])
{
// Initializing an array
long a[] = { 1, 2, 3, 4, 5 };
// Initializing an AtomicLongArray with array a
AtomicLongArray arr = new AtomicLongArray(a);
// Displaying the AtomicLongArray
System.out.println("The array : " + arr);
// Index where update is to be made
int idx = 3;
// Declaring the updateFunction
LongUnaryOperator cubeFunction = (l) -> l * l * l;
// Updating the value at idx
// applying updateFunction and
// storing the previous value
long prev = arr.getAndUpdate(idx, cubeFunction);
// Displaying the previous value
System.out.println("The value before update"
+ " at index " + idx
+ " is " + prev);
// Displaying the AtomicLongArray
System.out.println("The array after update : "
+ arr);
}
}
輸出:
The array : [1, 2, 3, 4, 5] The value before update at index 3 is 4 The array after update : [1, 2, 3, 64, 5]
相關用法
- Java AtomicIntegerArray getAndUpdate()用法及代碼示例
- Java AtomicReferenceArray getAndUpdate()用法及代碼示例
- Java AtomicLong getAndUpdate()用法及代碼示例
- Java AtomicReference getAndUpdate()用法及代碼示例
- Java AtomicLongArray get()用法及代碼示例
- Java AtomicLongArray set()用法及代碼示例
- Java AtomicLongArray toString()用法及代碼示例
- Java AtomicLongArray addAndGet()用法及代碼示例
- Java AtomicLongArray decrementAndGet()用法及代碼示例
- Java AtomicLongArray getAndAccumulate()用法及代碼示例
- Java AtomicLongArray getAndIncrement()用法及代碼示例
- Java AtomicLongArray getAndSet()用法及代碼示例
- Java AtomicLongArray incrementAndGet()用法及代碼示例
- Java AtomicLongArray getAndAdd()用法及代碼示例
- Java AtomicLongArray getAndDecrement()用法及代碼示例
注:本文由純淨天空篩選整理自rupesh_rao大神的英文原創作品 AtomicLongArray getAndUpdate() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。