AtomicReferenceArray類的getOpaque()方法用於返回此AtomicReferenceArray對象的索引i處的元素的值,並具有由VarHandle.getOpaque(java.lang.Object…)指定的內存效果。此VarHandle.getOpaque(java.lang.Object…)方法處理操作時,不能保證相對於其他線程的內存排序效果。
用法:
public final E getOpaque(int i)
參數:此方法接受索引i以獲取值。
返回值:此方法返回索引i處的當前值。
以下示例程序旨在說明getOpaque()方法:
程序1:
// Java program to demonstrate
// AtomicReferenceArray.getOpaque() method
import java.util.concurrent.atomic.*;
public class GFG {
public static void main(String[] args)
{
// create an atomic reference array
// object which stores Integer.
AtomicReferenceArray<Integer> array
= new AtomicReferenceArray<Integer>(5);
// set some value in array
array.set(0, 987);
array.set(1, 988);
array.set(2, 989);
array.set(3, 986);
// get and print the value
// using getOpaque method
for (int i = 0; i < 4; i++) {
int value = array.getOpaque(i);
System.out.println("value at "
+ i + " = "
+ value);
}
}
}
輸出:
程序2:
// Java program to demonstrate
// AtomicReferenceArray.getOpaque() method
import java.util.concurrent.atomic.*;
public class GFG {
public static void main(String[] args)
{
// create a array of Strings
String[] names
= { "AMAN", "AMAR", "SURAJ" };
// create an atomic reference object.
AtomicReferenceArray<String> array
= new AtomicReferenceArray<String>(names);
// get and print the value
// using getOpaque method
for (int i = 0; i < array.length(); i++) {
String value = array.getOpaque(i);
System.out.println("value at "
+ i + " = "
+ value);
}
}
}
輸出:
相關用法
- Java AtomicReference getOpaque()用法及代碼示例
- Java AtomicReferenceArray set()用法及代碼示例
- Java AtomicReferenceArray get()用法及代碼示例
- Java AtomicReferenceArray updateAndGet()用法及代碼示例
- Java AtomicReferenceArray lazySet()用法及代碼示例
- Java AtomicReferenceArray length()用法及代碼示例
- Java AtomicReferenceArray getPlain()用法及代碼示例
- Java AtomicReferenceArray toString()用法及代碼示例
- Java AtomicReferenceArray getAndSet()用法及代碼示例
- Java AtomicReferenceArray accumulateAndGet()用法及代碼示例
- Java AtomicReferenceArray compareAndSet()用法及代碼示例
- Java AtomicReferenceArray compareAndExchangeRelease()用法及代碼示例
- Java AtomicReferenceArray getAcquire()用法及代碼示例
- Java AtomicReferenceArray compareAndExchange()用法及代碼示例
- Java AtomicReferenceArray compareAndExchangeAcquire()用法及代碼示例
注:本文由純淨天空篩選整理自AmanSingh2210大神的英文原創作品 AtomicReferenceArray getOpaque() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。