Java concurrency container

Source: Internet
Author: User

The synchronization container will serialize all access to the container state for thread safety. The disadvantage of this approach is that it severely reduces concurrency. Java 5.0 provides a variety of concurrent containers to improve the performance of the synchronization container. such as Concurrenthashmap instead of synchronous and hash-based map,copyonwritearraylist, used in the case of the traversal operation of the main operation in lieu of synchronous list, while providing some common compliance operations, such as, if not added, Replacement and conditional deletion. Java 6 introduces Concurrentsckiplistmap and Concurrentskiplistset, instead of synchronized sortedmap and SortedSet.

Replacing synchronous containers with concurrent containers can greatly improve performance and reduce risk.

Concurrenthashmap

The synchronization container class holds locks during each operation. In some operations, such as Hashmap.get and list.contains, it may involve a lot of work: traversing a hash table or a linked list to find a particular object, you must call the Equals method. This will take a lot of time, while other threads will not be able to access the container during this time.
Concurrenthashmap is also a hash-based map, instead of synchronizing each method in the same lock and allowing only one thread to access the container at a time, it uses a finer-grained locking mechanism to achieve a large degree of sharing, a mechanism called a segmented lock. Any number of read threads can access the map concurrently, the thread that performs the read operation and the thread that performs the write operation can access the map concurrently, and a certain number of write threads can concurrently modify the map.
Similar concurrent containers enhance synchronization containers, and they provide iterators that do not throw concurrentmodeficationexception, so they do not need to be locked during an iterative process. The iterator returned by Concurrenthashmap has weak consistency, can tolerate concurrent modifications, iterates over existing elements when an iterator is created, and can reflect the modification to the container after the iterator is constructed.
While there are many improvements to concurrent containers, there are also factors that need to be weighed, and for methods that need to be computed on the entire map, such as size and IsEmpty, the results returned by these methods may have expired and are actually estimates only. Although this seems like a big problem, in fact, in a concurrency environment, these methods work very little because their values are constantly changing. These operations are weakened in exchange for performance optimizations for other more important operations, such as Get,put,containskey and remove.
Compared with Hashtable and Synchronizedmap, Concurrenthashmap has more advantages and less disadvantage, so using concurrenthashmap instead of synchronous map in most cases can further improve the scalability of the code. You should discard Concurrenthashmap only if you need to lock the map for exclusive access.
Because Concurrenthashmap cannot be locked to perform exclusive access, new atomic operations cannot be established using client-side locking, but some common compound operations have been implemented as atomic operations.

Copyonwritearraylist

The copyonwritearraylist is used instead of synchronizing lists, which in some cases provides better concurrency performance. And there is no need to lock or copy during iterations. Thread safety is a "write-on-time" system that creates and re-publishes a new container copy each time it is modified. The iterator to the copy-on-write container retains a reference to the underlying underlying array, which is located at the beginning of the iterator and, because it is not modified, only ensures the visibility of the array contents when synchronizing. Multiple threads can iterate over this container. The element returned by the container's iterator is exactly the same as the element that was created by the iterator.

The underlying array is copied whenever the container is modified, which requires some overhead, especially when the scale is large. The copy-on-write container should be used only if the iteration operation is much larger than the modify operation. Similarly, there are copyonwritearrayset. For event notification systems, the operation to register and unregister events is much less than the action of receiving events.

Java concurrency container

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.