可以创建可以在原子上交换的原子recreference吗?
-
24-10-2019 - |
题
有什么方法可以实施一种可以与另一个原子交换的参考类型?
在爪哇,我们有 AtomicReference
可以用局部变量交换,但不能与另一个变量交换 AtomicReference
.
你可以做:
AtomicReference r1 = new AtomicReference("hello");
AtomicReference r2 = new AtomicReference("world");
并用两个操作的组合交换:
r1.set(r2.getAndSet(r1.get()));
但这使它们处于不一致的状态,两者都包含 "hello"
. 。同样,即使您可以原子交换它们,您仍然无法(作为一对)原子读它们。
我想做的是:
PairableAtomicReference r1 = new PairableAtomicReference("hello");
PairableAtomicReference r2 = new PairableAtomicReference("world");
AtomicRefPair rp = new AtomicRefPair(r1, r2);
然后
Object[] oldVal, newVal;
do {
oldVal = rp.get();
newVal = new Object[] {oldVal[1], oldVal[0]};
} while (! rp.compareAndSet(oldVal, newVal));
交换值,然后在另一个线程中:
AtomicRefPair otherRP = new AtomicRefPair(r1, r2);
System.out.println(Arrays.toString(otherRP.get()));
并确保输出将是 [hello, world]
或者 [world, hello]
.
笔记:
r1
和r2
为此操作配对,但是另一个线程可能会独立配对r1
还有另一个r3
(不幸的是,这意味着我无法使用 这个解决方案.)- 将有成千上万的参考文献,因此全球
ReentrantLock
将是一个主要的瓶颈。 rp
和otherRP
不一定在线程之间共享,因此简单地锁定它们将不起作用。他们可能是 实习, ,但是实习生池将需要自己的同步,这将是另一个瓶颈。- 我在这里只有2个参考文献,但是第3组或更多组的能力将是一个奖励。
是否可以实现无锁版本的 AtomicRefPair
?我有一个直觉,但如果不是这样,也许有一篇文章可以解释为什么?
有关的: 如何在C#中互换2个INT?
解决方案
我不知道是否有一个不错的解决方案,但是以下丑陋的人可以工作:
public final class MyReference<T> extends ReentrantLock implements Comparable<MyReference<T>> {
public MyReference() {
id = counter.incrementAndGet();
}
public void swap(MyReference<T> other) {
if (id < other.id) {
lock();
other.lock();
} else {
other.lock();
lock();
}
final T tmp = value;
value = other.value;
other.value = tmp;
unlock();
other.unlock();
}
public static <T> List<T> consistentGet(List<MyReference<T>> references) {
final ArrayList<MyReference<T>> sortedReferences = Lists.newArrayList(references);
Collections.sort(sortedReferences);
for (val r : sortedReferences) r.lock();
final List<T> result = Lists.newArrayListWithExpectedSize(sortedReferences.size());
for (val r : references) result.add(r.value);
for (val r : sortedReferences) r.unlock();
return result;
}
@Override
public int compareTo(MyReference<T> o) {
return id < o.id ? -1 : id > o.id ? 1 : 0;
}
private final static AtomicInteger counter = new AtomicInteger();
private T value;
private final int id;
}
- 使用myReference而不是原子归因。
- 它使用了很多锁,但它们都不是全局。
- 它以固定的顺序获取锁,因此无僵局。
- 它使用Lombok和Guava进行编译(将其作为伪代码,没有它们)。
其他提示
拥有一对不变的班级。那就是你的原子。交换对意味着更换原子。
更新:您的问题不太清楚。但是通常,对于由多个变量组成的并发系统,一个人可能想要
- 拍摄系统状态的快照。快照不会改变。
- 通过一次更改多个变量来更新系统状态。可能需要在我的更新A之前的快照之间没有其他更新(我的计算是基于该快照)
如果不会消耗太多资源,则可以直接在快照中对系统进行建模。
不隶属于 StackOverflow