Different types of thread-safe Sets in Java
来源:互联网 发布:年入千万的网络主播 编辑:程序博客网 时间:2024/06/14 02:06
http://stackoverflow.com/questions/6720396/different-types-of-thread-safe-sets-in-java
1) The CopyOnWriteArraySet
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 (especially contains()
) 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 concurrent SortedSet
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 the ConcurrentHashMap
(and the Set derived from it): Here most basic options are (on average, if you have a good and fast hashCode()
) 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.
- Different types of thread-safe Sets in Java
- How to create different types of thread pool in Java
- Different types of keystore in Java -- JKS
- Different types of keystore in Java -- PKCS12
- Different types of keystore in Java -- Overview
- Different Types of Switches in Networking
- Different ways of associating known types in wcf
- Thread Safe In Servlet
- Different types of sales document
- Different types of SVM -- opencv
- Different types Server in Python
- Java - How to write Thread-Safe Code in Java
- Two Types of Error in JAVA
- 5 Examples of Different Types of CAPTCHAs
- Java Thread-safe
- Thread safe Singleton in C#
- The different types of polymorphism in OOP-面向对象程序设计中的多态类型
- For different transaction types,What's the meaning of fields in MTL_MATERIAL_TRANSACTIONS
- linux svn 使用
- 电脑不要睡了,说好的wifi呢?
- 性感无比的代码编辑器,程序员必备《Sublime Text》,神器
- OJ 1.F
- 关于service--android
- Different types of thread-safe Sets in Java
- 稀疏矩阵的转置
- ffmpeg从视频文件中读取成bmp图片
- Wpf itemsControl listbox 虚拟化平滑滚动
- Java NIO与IO的主要区别
- NFS服务器安装与配置
- 修改android AVD的默认路径
- Reversing Microsoft Visual C++ Part II: Classes, Methods and RTTI
- Linux下获取文件的路径