Java 中的并发数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6821379/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Concurrent Arrays in Java
提问by delmet
So, there is a concurrent hashmapin Java, the advantage of which is not to lock down the whole hash table, but only portions of it. I was wondering whether there was such a construction for arrays. Particularly when an array is being resized locking down the whole array is not desirable, especially in real time applications. Anything out there?
所以,Java中有一个并发hashmap,它的好处不是锁住整个hash表,而是锁其中的一部分。我想知道是否有这样的数组构造。特别是在调整阵列大小时,锁定整个阵列是不可取的,尤其是在实时应用程序中。外面有什么吗?
采纳答案by gnat
Java 6 also adds an interesting collection called ConcurrentSkipListSet
Java 6 还添加了一个有趣的集合,称为ConcurrentSkipListSet
...average log(n)time cost for thecontains, add
, andremove
operations and their variants. Insertion, removal, and access operations safely execute concurrently by multiple threads. Iterators are weakly consistent, returning elements reflecting the state of the set at some point at or since the creation of the iterator. They do not throw ConcurrentModificationException, and may proceed concurrently with other operations...
回答by Bozho
There is AtomicIntegerArray
(and the similar AtomicReferenceArray
) that may fit your description. But as noted by Marcelo - you can't resize arrays. So you only get concurrent safety without the need to explicitly lock (on) the entire array.
有AtomicIntegerArray
(和类似的AtomicReferenceArray
)可能符合您的描述。但正如 Marcelo 所指出的 - 你不能调整数组的大小。因此,您只需获得并发安全性,而无需显式锁定(锁定)整个数组。
An array ... in which elements may be updated atomically
一个数组......其中的元素可以自动更新
回答by duffymo
You can always use one of these:
您始终可以使用以下其中一种:
java.util.Collections.synchronizedList(List<T> list)
java.util.Collections.synchronizedCollection(Collection<T> collection)
java.util.Collections.synchronizedSet(Set<T> set)
java.util.Collections.synchronizedList(List<T> list)
java.util.Collections.synchronizedCollection(Collection<T> collection)
java.util.Collections.synchronizedSet(Set<T> set)
Your requirements aren't clear. Perhaps the java.util.collections copy-on-write list or set will do, too.
你的要求不是很清楚。也许 java.util.collections copy-on-write 列表或集合也可以。
回答by Tom Anderson
The closest thing in the standard library is the CopyOnWriteArrayList. That's 'concurrent' in the sense that there is no lock, and so not contention, for readers; however, access for writers is serialized, and is very expensive. The tradeoff is a bit sharper than for the concurrent hashmap: reads are really cheap, but writes are really expensive.
标准库中最接近的是CopyOnWriteArrayList。这是“并发”的,因为读者没有锁定,因此也没有争用;然而,作者的访问是序列化的,并且非常昂贵。权衡比并发散列图更尖锐:读取非常便宜,但写入非常昂贵。
It seems like it might be possible to write a list implementation which used the striped lock strategy of the concurrent hashmap for single-element, size-preserving operations like get
and set
(and perhaps add
to the end of the list), but the copy-on-write strategy for size-altering operations like add
and remove
. It might be rather complicated to get a sensible ordering of size-preserving and size-altering mutations, though.
似乎有可能编写一个列表实现,它使用并发散列图的条带锁策略进行单元素、大小保持操作,例如get
和set
(可能add
到列表的末尾),但是复制-为大小更改操作(如add
和 )编写策略remove
。然而,获得大小保持和大小改变突变的合理排序可能相当复杂。