我正在尝试找到一个或多个并发集合来使用,我可以实现以下行为(名称是出于类比目的而设计的):
/**
* Acts as a broker for a concurrent hash map that stores its keys in order
* of submission. At shipping time, the concurrent map is "sealed"
* (picture a truck with its cargo door being closed)
* and its contents presented as an immutable map, and is replaced
* by a new concurrent map ready to accept values.
*
* Consumers of this class that submit information to it, are expected to
* know that this contains a concurrent collection, and should use the
* compareAndSet paradigm, e.g. the following:
*
* LoadingDock loadingDock = ...
* boolean done = false;
* while (!done)
* {
* V oldValue = loadingDock.get();
* V newValue = computeNewValue(oldValue, otherInformation);
* if (oldValue == null)
* done = loadingDock.putIfAbsent(newValue) == null;
* else
* done = loadingDock.replace(oldValue, newValue) == oldValue;
* }
*
*
* Keys and values must be non-null. Keys are not ordered.
*/
class LoadingDock<K,V>
{
/**
* analogous to ConcurrentMap's replace, putIfAbsent, and get methods
*/
public boolean replace(K key, V oldValue, V newValue);
public V putIfAbsent(K key, V value);
public V get(K key)
/* see above */
public Map<K,V> ship();
}我对此有两个问题。
其一,Java和Guava都不包含ConcurrentLinkedHashMap。这让我想知道为什么不--也许我错过了这样一头野兽的微妙之处。如果putIfAbsent()被调用并返回null,那么我可以通过用一个类来装饰一个ConcurrentHashMap来自己创建一个映射--除了上面的方法之外,我不需要ConcurrentHashMap中的任何其他方法,所以除了调用putIfAbsent()之外,没有其他方法可以向映射中添加新的键。
另一个更隐蔽的问题是,我似乎无法想到如何在不阻塞同步的情况下实现ship() --当调用ship()时,LoadingDock需要将所有新调用定向到新映射,并且在确定所有并发写入都已完成之前,不能返回旧映射。(否则,我将只使用AtomicReference来保存并发映射。)
有没有一种不需要同步就能做到这一点的方法?
发布于 2012-02-10 01:27:41
您可以使用ConcurrentSkipListMap并提供自己的比较器,以便根据时间戳对条目进行排序。人们可以想象没有ConcurrentLinkedMap,因为没有任何比同步常规方法更好的方法来实现它。
对于ship()方法,只需使用打开了公平模式的ReadWriteLock即可。想要添加到map的线程获取读锁(我知道这是奇怪的语义,但它是如何工作的,可以将其视为对map的实际引用的读取模式,然后正常使用),以便可以同时添加尽可能多的锁。在ship方法中,您获得了写锁,它将阻止其他任何人在您导出和创建新的映射时更改映射。公平模式使得您可以在调用ship()时尽可能地“切断”加法器,让现有的加法器完成。
https://stackoverflow.com/questions/9215418
复制相似问题