什么时候CopyOnWriteArraySet有用于实现线程安全的HashSet? [英] When is CopyOnWriteArraySet useful to achieve thread-safe HashSet?
问题描述
在 Java
中,有线程安全版本 HashMap ,命名为 ConcurrentHashMap 和线程安全版本 TreeMap ,名称为 ConcurrentSkipListMap ,但 ConcurrentHashSet / HashSet.htmlrel =nofollow> HashSet 。
通常有4种方法使用线程安全设置
:
-
设置< String& mySet = Collections.newSetFromMap(new ConcurrentHashMap
()); -
Set< String& s = Collections.synchronizedSet(new HashSet< String>());
-
ConcurrentSkipListSet<
-
CopyOnWriteArraySet< E>
1使用
的
来实现
Set
和线程安全。
2使用同步
的方式,似乎这种方式不推荐。 >
3基于 ConcurrentSkipListMap
并被广泛使用。
4基于 CopyOnWriteArrayList ,因此它与 CopyOnWriteArrayList
共享相同的基本属性。以下是从 CopyOnWriteArraySet
中选择: http://docs.oracle.com/javase/8/docs/api/java/util/concurrent/CopyOnWriteArraySet.html
- 它最适用于集合大小通常保持为
小,只读操作大大超过突变操作的应用程序,以及
需要防止干扰 - 互斥操作(添加,设置,删除等)是昂贵的,因为它们通常需要复制整个底层数组。
- 迭代器不支持变异删除操作。
- 遍历迭代器速度快,不会受到其他线程的干扰。
- 迭代器依赖于构建迭代器时数组的不变快照。
由于1和3是常用的,为什么 CopyOnWriteArraySet
存在?
包含
操作。 code>列表
数据结构是O(n),而设置
数据结构是为了高性能 / code>操作,可以有人解释这个吗?解决方案线程安全收集。
一个例子是一组侦听器。您需要确保唯一性并有效地迭代它们。
BTW CopyOnWriteArraySet在每个引用的基础上具有最低的开销。它可以只有1/6的其他集合的大小。这是非常有用的,如果你有很多他们。
而设置数据结构是为了高性能包含操作,任何人可以解释这?
COWAS在内存方面更高效,而且包含
小集合比替代品。什么是高性能取决于用例。
In Java
, there is thread-safe version HashMap named ConcurrentHashMap and thread-safe version TreeMap named ConcurrentSkipListMap, but there is no ConcurrentHashSet
for HashSet.
Instead, there are usually 4 ways to use thread-safe Set
:
Set<String> mySet = Collections.newSetFromMap(new ConcurrentHashMap<String, Boolean>());
Set<String> s = Collections.synchronizedSet(new HashSet<String>());
ConcurrentSkipListSet<E>
CopyOnWriteArraySet<E>
1 use keySet()
of ConcurrentHashMap
to achieve both Set
and thread-safe.
2 use synchronized
way, it seems this way is not recommended.
3 is based on ConcurrentSkipListMap
and is widely used.
4 is based on CopyOnWriteArrayList, thus it shares the same basic properties of CopyOnWriteArrayList
. Following is select from CopyOnWriteArraySet
doc: http://docs.oracle.com/javase/8/docs/api/java/util/concurrent/CopyOnWriteArraySet.html
- It is best suited for applications in which set sizes generally stay
small, read-only operations vastly outnumber mutative operations, and
you need to prevent interference among threads during traversal.
- It is thread-safe.
- Mutative operations (add, set, remove, etc.) are expensive since they usually entail copying the entire underlying array.
- Iterators do not support the mutative remove operation.
- Traversal via iterators is fast and cannot encounter interference from other threads.
- Iterators rely on unchanging snapshots of the array at the time the iterators were constructed.
Since 1 and 3 are commonly used, why does CopyOnWriteArraySet
exist? When is CopyOnWriteArraySet
useful?
Added: CopyOnWriteArraySet
is based on CopyOnWriteArrayList
, and the contains
operation in List
data structure is O(n), while Set
data structure is for high performance contains
operation, could anybody explain this?
解决方案 It is useful when you have a small set of element for a thread safe collection.
One example is a Set of listeners. You need to ensure uniqueness and iterate over them efficiently.
BTW CopyOnWriteArraySet has the lowest overhead on a per reference basis. It can be as little as 1/6 the size of the other collections. This is particularly useful if you have a lot of them.
while Set data structure is for high performance contains operation, could anybody explain this?
COWAS is more efficient in terms of memory and it's contains
is faster for small collections than the alternatives. What is "high performance" depends on the use case.
这篇关于什么时候CopyOnWriteArraySet有用于实现线程安全的HashSet?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!