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

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

Java中的 AtomicReferenceArray getAndSet() 方法及示例

AtomicReferenceArray类的getAndSet()方法用于原子地将 AtomicReferenceArray 对象的索引 i 的值设置为 newValue,该值作为参数传递并返回 AtomicReferenceArray 对象的旧值,具有由 VarHandle.getAndSet(Java指定的内存效果.lang.Object…).VarHandle.getAndSet(Java.lang.Object…) 指定变量是句柄作为设置的内存语义,就好像变量被声明为易失性一样。

句法:

public final E getAndSet(int i, E newValue)

参数:此方法接受:

  • i是执行操作的 AtomicReferenceArray 的索引,
  • newValue是要设置的新值。

返回值:此方法返回索引 i 的旧值。
下面的程序说明了 getAndSet() 方法:
方案一:

Java
// Java program to demonstrate
// AtomicReferenceArray.getAndSet() method
 
import java.util.concurrent.atomic.*;
 
public class GFG {
    public static void main(String[] args)
    {
        // create an atomic reference object.
        AtomicReferenceArray ref
            = new AtomicReferenceArray(3);
 
        // set some value
        ref.set(0, 1234);
        ref.set(1, 4322);
 
        // apply getAndSet()
        int oldV1 = ref.getAndSet(0, 8913);
        int oldV2 = ref.getAndSet(1, 6543);
 
        // print
        System.out.println("Old value at index 0: "
                           + oldV1);
        System.out.println("New value at index 0: "
                           + ref.get(0));
        System.out.println("Old value at index 1: "
                           + oldV2);
        System.out.println("New value at index 1: "
                           + ref.get(1));
    }
}


Java
// Java program to demonstrate
// AtomicReferenceArray.getAndSet() method
 
import java.util.concurrent.atomic.*;
 
public class GFG {
    public static void main(String[] args)
    {
        // create an atomic reference object.
        AtomicReferenceArray ref
            = new AtomicReferenceArray(3);
 
        // set some value
        ref.set(0, "GFG");
        ref.set(1, "JS");
 
        // apply getAndSet()
        String oldV1
            = ref.getAndSet(0, "GEEKS FOR GEEKS");
        String oldV2
            = ref.getAndSet(1, "JAVA SCRIPT");
 
        // print
        System.out.println(
            "Old value at index 0: "
            + oldV1);
        System.out.println(
            "New value at index 0: "
            + ref.get(0));
        System.out.println(
            "Old value at index 1: "
            + oldV2);
        System.out.println(
            "New value at index 1: "
            + ref.get(1));
    }
}


输出:
Old value at index 0: 1234
New value at index 0: 8913
Old value at index 1: 4322
New value at index 1: 6543

方案二:

Java

// Java program to demonstrate
// AtomicReferenceArray.getAndSet() method
 
import java.util.concurrent.atomic.*;
 
public class GFG {
    public static void main(String[] args)
    {
        // create an atomic reference object.
        AtomicReferenceArray ref
            = new AtomicReferenceArray(3);
 
        // set some value
        ref.set(0, "GFG");
        ref.set(1, "JS");
 
        // apply getAndSet()
        String oldV1
            = ref.getAndSet(0, "GEEKS FOR GEEKS");
        String oldV2
            = ref.getAndSet(1, "JAVA SCRIPT");
 
        // print
        System.out.println(
            "Old value at index 0: "
            + oldV1);
        System.out.println(
            "New value at index 0: "
            + ref.get(0));
        System.out.println(
            "Old value at index 1: "
            + oldV2);
        System.out.println(
            "New value at index 1: "
            + ref.get(1));
    }
}
输出:
Old value at index 0: GFG
New value at index 0: GEEKS FOR GEEKS
Old value at index 1: JS
New value at index 1: JAVA SCRIPT

参考资料: https: Java/util/concurrent/atomic/AtomicReferenceArray.html#getAndSet(int, E)