Java concurrent Programming: The Concurrenthashmap of concurrent containers

Source: Internet
Author: User
Tags volatile

Ava concurrent Programming: The Concurrenthashmap of concurrent containers

The following sections are reproduced from:

http://www.haogongju.net/art/2350374

A new concurrent package is added to the JDK5, and the concurrency container improves concurrency performance through a number of mechanisms relative to the synchronization container. Because the synchronization container will all access to the state of the container

serialization, which guarantees thread security, so the cost of this approach is to severely reduce concurrency, which is severely degraded when multiple threads compete with the container. So Java5.0 Open

The concurrent access design for multithread is provided, and the concurrent container with better concurrency is introduced, and the Java.util.concurrent package is imported. With vectors and Hashtable,

Compared with collections.synchronizedxxx () synchronization containers, the concurrent containers introduced in util.concurrent mainly solve two problems:
1 According to the specific scene design, try to avoid synchronized, provide concurrency.
2 defines some concurrent and secure composite operations, and ensures that the iterative operation in the concurrent environment is not wrong.

Util.concurrent in the iteration, the container can be encapsulated in the synchronized, you can guarantee that the exception is not thrown, but not every time you see the "latest, current" data.

The following is a brief description of the concurrent container:

Concurrenthashmap instead of the synchronized map (collections.synchronized (new HashMap)), it is well known that HASHMAP is stored according to the hash value, and the synchronization map locks all segments while synchronizing. While the Concurrenthashmap lock is based on the hash value of the hash value lock corresponding to the paragraph, thereby improving concurrency performance. Concurrenthashmap also adds support for commonly used composite operations, such as "add If not": putifabsent (), Replacement: replace (). These 2 operations are atomic operations.

Copyonwritearraylist and Copyonwritearrayset, respectively, instead of list and set, are mainly in lieu of synchronized lists and synchronized sets in the context of traversal operations, which is the idea that the iterative process guarantees no errors, In addition to locking, another way is to "clone" the container object.

Concurrentlinkedquerue is an advanced first-out queue. It is a non-blocking queue.

Concurrentskiplistmap can replace soredmap in efficient concurrency (for example, TreeMap wrapped with collections.synchronzedmap).

Concurrentskiplistset can replace soredset in efficient concurrency (for example, TreeMap wrapped with collections.synchronzedset).

  

This article focuses on 2 concurrent containers: Concurrenthashmap and Copyonwritearraylist concurrenthashmap,copyonwritearraylist in the next article.

Original link: http://www.iteye.com/topic/1103980

We all know hashmap is not thread-safe, Hashtable is thread-safe, but because Hashtable is synchronized with synchronized, the equivalent of all threads to read and write to compete for a lock, resulting in very low efficiency.

Concurrenthashmap can read data without locking, and its internal structure allows it in the write operation can keep the size of the lock as small as possible, do not have to lock the entire concurrenthashmap. the internal structure of the Concurrenthashmap

Concurrenthashmap in order to improve its concurrency capability, in the internal use of a structure called segment, a segment is actually a class hash table structure, segment internal maintenance of a list of linked lists, Let's look at the internal structure of the Concurrenthashmap with the following picture:

From the structure above, we can see that the process of locating an element requires two hash operations, the first hash to the segment, and the second hash to the head of the linked list where the element is located, so the Concurrenthashmap The side effect of this kind of structure is that the process of the hash is longer than the ordinary hashmap, but the benefit is that the writing operation can only add the lock to the segment of the element, and will not affect the other segment, so that, in the ideal case, Concurrenthashmap can support a segment number of write operations at the same time (just as these writes are evenly distributed across all segment), so the concurrency capability of Concurrenthashmap can be greatly improved through this structure. Segment

Let's take a concrete look at the data structure of segment:

1 2 3 4 5 6 7 static  final  class  Segment<k,v>&nbs P extends  reentrantlock  implements  Serializable {     transient  volatile   int  count;      transient  int  Modcount;      transient  int  threshold;      transient  volatile 
Related Article

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.