Java.AtomicLong.accumulateAndGet()方法是一種內置方法,該方法通過對當前值和作為參數傳遞的值應用指定的操作來更新對象的當前值。它以long作為其參數以及LongBinaryOperator接口的對象,並將該對象中指定的操作應用於這些值。它返回更新的值。
用法:
public final long accumulateAndGet(long y, LongBinaryOperator function)
參數:此方法接受長值y和LongBinaryOperator函數作為參數。它將給定函數應用於對象的當前值和值y。
返回值:該函數返回當前對象的更新值。
實例演示函數。
// Java programt to demonstrate
// AtomicLong accumulateAndGet() method
import java.util.concurrent.atomic.AtomicLong;
import java.util.function.LongBinaryOperator;
public class Demo {
public static void main(String[] args)
{
// AtomicLong initialized with a value of 555
AtomicLong atomicLong = new AtomicLong(555);
// Binary operator defined
// to calculate the product
LongBinaryOperator binaryOperator
= (x, y) -> x * y;
System.out.println("Initial Value is "
+ atomicLong);
// Function called and the binary operator
// is passed as an argument
long value
= atomicLong
.accumulateAndGet(555, binaryOperator);
System.out.println("Updated value is "
+ value);
}
}
輸出:
Initial Value is 555 Updated value is 308025
參考: https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/atomic/AtomicLong.html
相關用法
- Java AtomicIntegerArray accumulateAndGet()用法及代碼示例
- Java AtomicInteger accumulateAndGet()用法及代碼示例
- Java AtomicReference accumulateAndGet()用法及代碼示例
- Java AtomicReferenceArray accumulateAndGet()用法及代碼示例
- Java AtomicLongArray accumulateAndGet()用法及代碼示例
- Java AtomicLong get()用法及代碼示例
- Java AtomicLong set()用法及代碼示例
- Java AtomicLong toString()用法及代碼示例
- Java AtomicLong getAndAdd()用法及代碼示例
- Java AtomicLong compareAndSet()用法及代碼示例
- Java AtomicLong addAndGet()用法及代碼示例
- Java AtomicLong longValue()用法及代碼示例
- Java AtomicLong intValue()用法及代碼示例
- Java AtomicLong doubleValue()用法及代碼示例
- Java AtomicLong floatValue()用法及代碼示例
注:本文由純淨天空篩選整理自CharchitKapoor大神的英文原創作品 AtomicLong accumulateAndGet() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。