http://stackoverflow.com/questions/6720396/different-types-of-thread-safe-sets-in-java
1) TheCopyOnWriteArraySet
is
a quite simple implementation - it basically has a list of elements in an array, and when changing the list, it copies the array. Iterations and other accesses which are running at this time continue with the old array, avoiding necessity of synchronization.
The normally fast set operations (especiallycontains()
)
are quite slow here, as the arrays will be searched in linear time.
Use this only for really small sets which will be read (iterated) often and changed seldom. (Swings listener-sets would be an example, but these are not really sets, and should be only used from the EDT anyway.)
2)Collections.synchronizedSet
will
simply wrap a synchronized-block around each method of the original set. You should not access the original set directly. This means that no two methods of the set can be executed concurrently (one will block until the other finishes) - this is thread-safe,
but you will not have concurrency if multiple threads are really using the set. If you use the iterator, you usually still need to synchronize externally to avoid ConcurrentModificationExceptions when modifying the set between iterator calls. The performance
will be like the performance of the original set (but with some synchronization overhead, and blocking if used concurrently).
Use this if you only have low concurrency, and want to be sure all changes are immediately visible to the other threads.
3)ConcurrentSkipListSet
is
the concurrentSortedSet
implementation,
with most basic operations in O(log n). It allows concurrent adding/removing and reading/iteration, where iteration may or may not tell about changes since the iterator was created. The bulk operations are simply multiple single calls, and not atomically -
other threads may observe only some of them.
Obviously you can use this only if you have some total order on your elements. This looks like an ideal candidate for high-concurrency situations, for not-too-large sets (because of the O(log n)).
4) For theConcurrentHashMap
(and
the Set derived from it): Here most basic options are (on average, if you have a good and fasthashCode()
)
in O(1) (but might degenerate to O(n)), like for HashMap/HashSet. There is a limited concurrency for writing (the table is partitioned, and write access will be synchronized on the needed partition), while read access is fully concurrent to itself and the
writing threads (but might not yet see the results of the changes currently being written). The iterator may or may not see changes since it was created, and bulk operations are not atomic. Resizing is slow (as for HashMap/HashSet), thus try to avoid this
by estimating the needed size on creation.
Use this when you have large sets, a good (and fast) hash function and can estimate the set size and needed concurrency before creating the map.
分享到:
相关推荐
Simulation and Comparison of Different Types of First-Order Decentralized Sliding Mode Estimators
According to the standard IEC 61508 fault insertion testing is required for the verification of fail-safe systems. Usually these systems are realized with microcontrollers. Fail-safe systems based on ...
pcb探头( Characterization of different types of PCB loop probes)
At that time, the priorities of the consumer electronics industry were quite different from those in the computer industry. Whereas five years ago the computer industry's mantra was compatibility, ...
在讨价还价游戏中大脑从上到下和至下而上的控制发挥了不同的作用,罗强,,大脑网络的时变特性又其生物物理可塑特性决定,但是在有效连接的分析中还没有得到足够的重视.特别地,常用的格兰杰因果分析在时变网
Machine learning is a subfield of artificial ... With a given set of data, an ML algorithm learns different properties of the data and infers the properties of the data that it may encounter in future.
MAP is built using the expertise and guidelines gathered while programming three types of multi-core processors for three different classes of applications. Thus, MAP has several stages: application ...
The different between java and c#
You will learn about design patterns specific to applications running in the cloud and find out how you can build a microservice in Java Spring using REST APIs. You will then take a deep dive into ...
We propose a low-threshold soliton fiber laser passively mode locked with two different types of film-like saturable absorbers, one of which is fabricated by mixing Bi
In particular, we investigated empirically derived problems that emerge with different types of time-oriented data (e.g., time points, time intervals) and provide various examples of quality problems...
By highlighting the benefits and drawbacks of the different fuzzy rough set models, this study appears a necessary first step to propose and develop new models in future research. ©2014 Elsevier B.V...
The effect of different types of nomination forms on teachers' identification of gifted children Psychology m the Schools Volume 20. Ortoher. 1983 THE EFFECT O F DIFFERENT TYPES OF NOMINATION ...
In particular they want to in- teractively test and explore the effect on analysis results of different hypothetical modifications applied to the appli- cation’s transition system and to the ...
Link prediction in dynamic social networks by integrating different types of information
number of samples that are required in order to ensure that most of the observers cannot perceive any noise is still open since the ideal image is unknown. Image Quality Assessment (IQA) is well known...
不同状态下稀土镁合金的生物相容性能研究,陆冶,白晶,传统医用镁合金材料有诸多缺点,比如含有有害元素以及较差的耐蚀性能。本文采用了了电化学测试、浸泡测试以及力学性能测试研究了
He has worked as a teacher, researcher, programmer, analyst, and writer, and he now works as an architect in all types of projects related to Java, especially J2EE. As a teacher, has taken over 1,000...
This thesis explores different aspects of the induction of tree-based regression models from data. The main goal of this study is to improve the predictive accuracy of regression trees,while retaining...
The plugins filters, when run or loaded into imagej, will allow user to be able to edit images and perform different operations on images. The user can build on the plugins to create edit an image ...