📌  相关文章
📜  Java中的 AtomicLongArray compareAndSet() 方法及示例

📅  最后修改于: 2022-05-13 01:55:04.842000             🧑  作者: Mango

Java中的 AtomicLongArray compareAndSet() 方法及示例

Java.util.concurrent.atomic.AtomicLongArray.compareAndSet()是Java中的一个内置方法,如果当前值等于预期值,它会自动将某个位置的元素设置为给定的更新值。该方法将索引值、期望值和更新值作为参数,并返回一个布尔值,说明该值是否已更新。

句法:

参数:该函数接受三个参数:

  • i :要进行操作的索引。
  • expect :检查它是否等于当前值的期望值。
  • update :要更新的值。

    返回值:该函数返回一个布尔值,说明该值是否已更新。如果成功则返回true ,否则返回false ,表示当前值不等于预期值。

    下面的程序说明了上述方法:
    方案一:

    // Java program that demonstrates
    // the compareAndSet() function
      
    import java.util.concurrent.atomic.AtomicLongArray;
      
    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 operation is performed
            int idx = 3;
      
            // Value to expect at idx
            long expect = 4;
      
            // Value to update if current value
            // is equal to expected value
            long update = 40;
      
            // Updating the value at idx
            // applying compareAndSet
            arr.compareAndSet(idx, expect, update);
      
            // Displaying the AtomicLongArray
            System.out.println("The array after update : "
                               + arr);
        }
    }
    
    输出:
    The array : [1, 2, 3, 4, 5]
    The array after update : [1, 2, 3, 40, 5]
    

    方案二:

    // Java program that demonstrates
    // the compareAndSet() function
      
    import java.util.concurrent.atomic.AtomicLongArray;
      
    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 operation is performed
            int idx = 3;
      
            // Value to expect at idx
            long expect = 40;
      
            // Value to update if current value
            // is equal to expected value
            long update = 4;
      
            // Updating the value at
            // idx applying compareAndSet
            arr.compareAndSet(idx, expect, update);
      
            // Displaying the AtomicLongArray
            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/8/docs/api/java Java