java.util.concurrent.atomic.atomicreferencearray
类提供了可以原子读取和写入的底层引用数组的操作,并且还包含高级原子操作。 atomicreferencearray
支持对底层引用数组变量的原子操作。 它具有获取和设置方法,如在变量上的读取和写入。 也就是说,一个集合与同一变量上的任何后续获取相关联。 原子compareandset
方法也具有这些内存一致性功能。
以下是atomicreferencearray
类中可用的重要方法的列表。
序列 | 方法 | 描述 |
---|---|---|
1 | public boolean compareandset(int i, e expect, e update) |
如果当前值== 期望值,则将位置i 处的元素原子设置为给定的更新值。 |
2 | public e get(int i) |
获取位置i 的当前值。 |
3 | public e getandset(int i, e newvalue) |
将位置i 处的元素原子设置为给定值,并返回旧值。 |
4 | public void lazyset(int i, e newvalue) |
最终将位置i 处的元素设置为给定值。 |
5 | public int length() |
返回数组的长度。 |
6 | public void set(int i, e newvalue) |
将位置i 处的元素设置为给定值。 |
7 | public string tostring() |
返回数组的当前值的string 表示形式。 |
8 | public boolean weakcompareandset(int i, e expect, e update) |
如果当前值== 期望值,则将位置i 处的元素原子设置为给定的更新值。 |
以下testthread
程序显示了在线程环境中使用atomicreferencearray
变量。
import java.util.concurrent.atomic.atomicreferencearray;
public class testthread {
private static string[] source = new string[10];
private static atomicreferencearray<string> atomicreferencearray
= new atomicreferencearray<string>(source);
public static void main(final string[] arguments) throws interruptedexception {
for (int i=0; i<atomicreferencearray.length(); i++) {
atomicreferencearray.set(i, "item-2");
}
thread t1 = new thread(new increment());
thread t2 = new thread(new compare());
t1.start();
t2.start();
t1.join();
t2.join();
}
static class increment implements runnable {
public void run() {
for(int i=0; i<atomicreferencearray.length(); i++) {
string add = atomicreferencearray.getandset(i,"item-"+ (i+1));
system.out.println("thread " + thread.currentthread().getid()
+ ", index " +i + ", value: "+ add);
}
}
}
static class compare implements runnable {
public void run() {
for(int i=0; i<atomicreferencearray.length(); i++) {
system.out.println("thread " + thread.currentthread().getid()
+ ", index " +i + ", value: "+ atomicreferencearray.get(i));
boolean swapped = atomicreferencearray.compareandset(i, "item-2", "updated-item-2");
system.out.println("item swapped: " + swapped);
if(swapped){
system.out.println("thread " + thread.currentthread().getid()
+ ", index " +i + ", updated-item-2");
}
}
}
}
}
这将产生以下结果 -
thread 9, index 0, value: item-2
thread 10, index 0, value: item-1
item swapped: false
thread 10, index 1, value: item-2
item swapped: true
thread 9, index 1, value: updated-item-2
thread 10, index 1, updated-item-2
thread 10, index 2, value: item-3
item swapped: false
thread 10, index 3, value: item-2
item swapped: true
thread 10, index 3, updated-item-2
thread 10, index 4, value: item-2
item swapped: true
thread 10, index 4, updated-item-2
thread 10, index 5, value: item-2
item swapped: true
thread 10, index 5, updated-item-2
thread 10, index 6, value: item-2
thread 9, index 2, value: item-2
item swapped: true
thread 9, index 3, value: updated-item-2
thread 10, index 6, updated-item-2
thread 10, index 7, value: item-2
thread 9, index 4, value: updated-item-2
item swapped: true
thread 9, index 5, value: updated-item-2
thread 10, index 7, updated-item-2
thread 9, index 6, value: updated-item-2
thread 10, index 8, value: item-2
thread 9, index 7, value: updated-item-2
item swapped: true
thread 9, index 8, value: updated-item-2
thread 10, index 8, updated-item-2
thread 9, index 9, value: item-2
thread 10, index 9, value: item-10
item swapped: false