Threading Learning--(vii) Single and multi-threaded, synchronous class containers and concurrent class containers

Source: Internet
Author: User

First, the Synchronization class container

Synchronization class containers are thread-safe, but in some scenarios it may be necessary to lock to protect the composite operation. Compound classes operate such as iterating (repeatedly accessing elements, traversing through all elements in the container), jumping (finding the next element of the current element in the order specified), and conditional operations. These compound operations may exhibit unexpected behavior when modifying containers in multithreaded concurrency, the most classic being concurrentmodificationexception, because when the container iterates, the content is modified concurrently, This is because the early iterator design did not consider the problem of concurrent modification.

Synchronization class containers: such as the ancient vector/hashtable. The synchronization functions of these containers are actually implemented with the collections.syncronized of the JDK and other factory method areas. The underlying mechanism is to synchronize each common method with the traditional synchronized keyword so that only one thread can access the state of the container at a time. This is obviously not enough to meet the high concurrency requirements of our internet age today, and to ensure thread safety, we must have good performance at the same time.

Second, concurrent class container

jdk5.0 later provides a variety of concurrent class containers to replace synchronization classes to improve performance. The state of the synchronization class container is serialized. Although they are thread-safe, they severely reduce concurrency and severely reduce the throughput of applications in multithreaded environments.

Concurrent class containers are designed for concurrency, using concurrenthashmap instead of the traditional hashtable for hashing, and in concurrenthashmap, some common composite operations are added. And the use of copyonwritearrylist instead of vectors, concurrent copyonwritearrayset, and concurrent queue,concurrentlinkedqueue and Linkedblockingqueue, The former is a high-performance queue, the latter is in a blocking form of the queue, the implementation of a lot of queue, such as Arrayblockingqueue, Priorityblockingqueue, Synchronousqueue and so on.

Third, concurrent and Copyonwrite

Concurrenthashmap has a segment concept, a map is divided into multiple segments, each with its own lock, fine-grained design ideas, reducing the granularity of the lock, a map is divided into 16 segments.

Copyonwrite container, is also a design-based container, a copy of the container, write to an element, do not manipulate the current container, but the operation of the copy, write on the copy, operation and then point the original container to the copy. Suitable for use in cases where read and write are less.

Threading Learning--(vii) Single and multi-threaded, synchronous class containers and concurrent class containers

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.