Java AtomicIntegerArray compareAndSet()方法及示例
Java.util.concurrent.atomic.AtomicIntegerArray.compareAndSet() 是java中的一个内置方法,如果当前值等于预期值,则将某个位置的元素原子化地设置为给定的更新值。该方法以索引值、预期值和更新值为参数,并返回一个布尔值,说明该值是否已被更新。
语法
public final boolean compareAndSet(int i, int expect, int update)
参数: 该函数接受三个参数。
- i : 将要进行操作的索引。
- expect : 要检查是否等于当前值的预期值。
- update : 要更新的值。
返回值 :该函数返回一个布尔值,说明该值是否已经被更新。如果成功则返回true,否则返回false,表明当前值不等于预期值。
下面的程序说明了上述方法:
程序1 :
// Java program that demonstrates
// the compareAndSet() function
import java.util.concurrent.atomic.AtomicIntegerArray;
public class GFG {
public static void main(String args[])
{
// Initializing an array
int a[] = { 1, 2, 3, 4, 5 };
// Initializing an AtomicIntegerArray with array a
AtomicIntegerArray arr = new AtomicIntegerArray(a);
// Displaying the AtomicIntegerArray
System.out.println("The array : " + arr);
// Index where operation is performed
int idx = 3;
// Value to expect at idx
int expect = 4;
// Value to update if current value
// is equal to expected value
int update = 40;
// Updating the value at idx
// applying compareAndSet
arr.compareAndSet(idx, expect, update);
// Displaying the AtomicIntegerArray
System.out.println("The array after update : "
+ arr);
}
}
输出。
The array : [1, 2, 3, 4, 5]
The array after update : [1, 2, 3, 40, 5]
程序2
// Java program that demonstrates
// the compareAndSet() function
import java.util.concurrent.atomic.AtomicIntegerArray;
public class GFG {
public static void main(String args[])
{
// Initializing an array
int a[] = { 1, 2, 3, 4, 5 };
// Initializing an AtomicIntegerArray with array a
AtomicIntegerArray arr = new AtomicIntegerArray(a);
// Displaying the AtomicIntegerArray
System.out.println("The array : " + arr);
// Index where operation is performed
int idx = 3;
// Value to expect at idx
int expect = 40;
// Value to update if current value
// is equal to expected value
int update = 4;
// Updating the value at
// idx applying compareAndSet
arr.compareAndSet(idx, expect, update);
// Displaying the AtomicIntegerArray
System.out.println("The array after update : "
+ arr);
}
}
输出。
The array : [1, 2, 3, 4, 5]
The array after update : [1, 2, 3, 4, 5]
参考资料:Java.util.concurrent.atomic.AtomicIntegerArray.compareAndSet() 是java中的一个内置方法,如果当前值等于预期值,则将某个位置上的元素原子化地设置为给定的更新值。该方法以索引值、预期值和更新值为参数,并返回一个布尔值,说明该值是否已被更新。
语法
public final boolean compareAndSet(int i, Integer expect, Integer update)
参数: 该函数接受三个参数。
- i : 将要进行操作的索引。
- expect : 要检查是否等于当前值的预期值。
- update : 要更新的值。要更新的值。
返回值 :该函数返回一个布尔值,说明该值是否已经被更新。如果成功则返回true,否则返回false,表明当前值不等于预期值。
以下程序说明了上述方法:
程序1 :
// Java program that demonstrates
// the compareAndSet() function
import java.util.concurrent.atomic.AtomicIntegerArray;
public class GFG {
public static void main(String args[])
{
// Initializing an array
Integer a[] = { 1, 2, 3, 4, 5 };
// Initializing an AtomicIntegerArray with array a
AtomicIntegerArray arr = new AtomicIntegerArray(a);
// Displaying the AtomicIntegerArray
System.out.println("The array : " + arr);
// Index where operation is performed
int idx = 3;
// Value to expect at idx
Integer expect = 4;
// Value to update if current value
// is equal to expected value
Integer update = 40;
// Updating the value at idx
// applying compareAndSet
arr.compareAndSet(idx, expect, update);
// Displaying the AtomicIntegerArray
System.out.println("The array after update : "
+ arr);
}
}
输出。
The array : [1, 2, 3, 4, 5]
The array after update : [1, 2, 3, 40, 5]
程序2
// Java program that demonstrates
// the compareAndSet() function
import java.util.concurrent.atomic.AtomicIntegerArray;
public class GFG {
public static void main(String args[])
{
// Initializing an array
Integer a[] = { 1, 2, 3, 4, 5 };
// Initializing an AtomicIntegerArray with array a
AtomicIntegerArray arr = new AtomicIntegerArray(a);
// Displaying the AtomicIntegerArray
System.out.println("The array : " + arr);
// Index where operation is performed
int idx = 3;
// Value to expect at idx
Integer expect = 40;
// Value to update if current value
// is equal to expected value
Integer update = 4;
// Updating the value at
// idx applying compareAndSet
arr.compareAndSet(idx, expect, update);
// Displaying the AtomicIntegerArray
System.out.println("The array after update : "
+ arr);
}
}
输出。
The array : [1, 2, 3, 4, 5]
The array after update : [1, 2, 3, 4, 5]
参考: https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/atomic/AtomicIntegerArray.html#compareAndSet(int, %20int, %20int)
极客教程