The first associated collection class in the Java class library is hashtable, which is part of jdk1.0. Hashtable provides an easy-to-use, thread-safe, and associated map function, which is of course convenient. However, thread security comes at a cost-All hashtable methods are synchronized. In this case, non-competitive synchronization will cause a considerable performance cost. Hashmap, the successor of hashtable, is a part of the collection framework in jdk1.2. It solves thread security issues by providing an unsynchronized base class and a synchronized package collections. synchronizedmap. By separating basic functions from thread security, collections. synchronizedmap allows users who need to synchronize data, and users who do not need to synchronize data do not have to pay the cost for synchronization.
The simple method used by hashtable and synchronizedmap to obtain synchronization (synchronize each method in the hashtable or synchronized map wrapper object) has two main shortcomings. First, this method is an obstacle to scalability, because only one thread can access the hash table at a time. At the same time, this is still insufficient to provide real thread security, and many common hybrid operations still require additional synchronization. Although simple operations such as get () and put () can be safely completed without additional synchronization, there are still some common operation sequences, for example, iteration or put-if-absent (Put If empty) requires external synchronization to avoid data contention.
Concurrenthashmap
Util. concurrenthashmap class in the concurrent package (it will also appear in Java. util. concurrent package) is the implementation of thread security for map, it provides much better concurrency than synchronizedmap. Multiple read operations can almost always be executed concurrently, while simultaneous read and write operations can also be executed concurrently, concurrent write operations can still be performed concurrently from time to time (related classes also provide similar concurrency for multiple read threads, but only one active write thread is allowed ). Concurrenthashmap is designed to optimize search operations. In fact, after successful get () operations are completed, there are usually no locked resources. It requires a certain skill to obtain thread security without using a lock, and a deep understanding of the details of the Java Memory Model (javamemorymodel. Concurrenthashmap implementation, coupled with other parts of the util. Concurrent package, has been taken into consideration by concurrent experts who are studying correctness and thread security. In the next month's article, we will look at the implementation details of concurrenthashmap.
Concurrenthashmap achieves higher concurrency by slightly easing its commitment to callers. The search operation can return the value inserted by the most recently completed insert operation, you can also return the value that is added by the concurrent insert operation at the pace (but will never return a meaningless result ). By concurrenthashmap. iterators returned by iterator () will return at most one element each time, and will never throw a concurrentmodificationexception, however, it may or may not reflect the insert or remove operations after the iterator is built. When performing an iteration on a set, the thread security can be provided without the table range lock. In any application that does not rely on locking the entire table to prevent updates, you can use concurrenthashmap to replace synchronizedmap or hashtable.
The above improvements enable concurrenthashmap to provide much higher scalability than hashtable, and there is no need to lose efficiency for many types of public cases (such as shared cache.
Copyonwritearraylist
In concurrent applications with larger traversal operations than insert or remove operations, the copyonwritearraylist class is generally used to replace arraylist. If it is used to store a listener list, such as in an AWT or swing application, or in a common JavaBean, this is common (related copyonwritearrayset uses a copyonwritearraylist to implement the set interface ).
If you are using a normal arraylist to store a listener list, as long as the list is variable and may be accessed by multiple threads, you must either lock the entire list during the iteration or during the clone operation before the iteration, both of which have high overhead. When the list is changed, copyonwritearraylist does not create a new copy for the list, its iterator can certainly return the status of the list when the iterator is created, without throwing concurrentmodificationexception. You do not need to clone the list or lock the list during iteration before performing an iteration on the list, because the list copy displayed by the iterator remains unchanged. In other words, the copyonwritearraylist contains a mutable reference to an unchangeable array. Therefore, as long as the reference is retained, you can get the benefits of immutable thread security without locking the list.
The synchronized collection classes hashtable and vector, and the synchronized Wrapper Classes collections. synchronizedmap and collections. synchronizedlist provide basic conditional thread security implementation for map and list. However, some factors make them unsuitable for applications with high concurrency-the single lock feature of their set range is an obstacle to scalability, and, in many cases, you must lock a set for a long period of time to prevent concurrentmodificationexceptions exceptions. Concurrenthashmap and copyonwritearraylist provide higher concurrency and thread security. They only offer discounts on the caller's commitment. Concurrenthashmap and copyonwritearraylist are not useful anywhere you use hashmap or arraylist, but they are designed to optimize certain public solutions. Many concurrent applications will benefit from their use.