Java Concurrent Programming: concurrent HashMap

Java Concurrent Programming: concurrent HashMap of concurrent container (Reprint)

The following part is reproduced from:

Jdk5 adds a new concurrent package. Compared with the synchronization container, the concurrency container improves the concurrency performance through some mechanisms. Because synchronizing the container will all access to the container state

Serialization ensures the safety of threads, so the cost of this method is to seriously reduce the concurrency. When multiple threads compete for containers, the throughput is seriously reduced. So Java 5 0 on

Started to design for multithreaded concurrent access, provided a concurrency container with good concurrency performance, and introduced Java util. Concurrent package. With vector and hashtable

Collections. Synchronizedxxx() synchronizes containers, etc. compared to util The concurrency container introduced in concurrent mainly solves two problems: 1) design according to specific scenarios to avoid synchronized and provide concurrency. 2) Some concurrent and safe composite operations are defined, and the iterative operations in the concurrent environment will not make mistakes.

  util. During the iteration of concurrent, the container can not be encapsulated in synchronized, which can ensure that no exceptions are thrown, but it may not always see the "latest and current" data.

The following is a brief introduction to the concurrency container:

Concurrenthashmap replaces the synchronized map (collections. Synchronized (New hashmap()). As we all know, HashMap is stored in segments according to the hash value. The synchronized map locks all segments during synchronization, while the concurrenthashmap locks the segment corresponding to the hash value lock according to the hash value when locking, so the concurrency performance is improved. Concurrent HashMap also adds support for common composite operations, such as "add if not": putifabsent(), replace: replace(). Both operations are atomic.

Copyonwritearraylist and copyonwritearrayset replace list and set respectively, mainly in the case of traversal operation, instead of synchronized list and synchronized set, which is the idea described above: in addition to locking, another method is to "clone" container objects.

Concurrentlinkedqueue is a first in first out queue. It is a non blocking queue.

Concurrentskiplistmap can replace soredmap in efficient concurrency (for example, treemap wrapped in collections. Synchronizedmap).

Concurrentskiplistset can replace soredset in efficient concurrency (for example, treemap wrapped in collections.synchronizedset).

This article focuses on two concurrent containers: concurrenthashmap and copyonwritearraylist. The concurrenthashmap and copyonwritearraylist are described in the next article.

Original link:

We all know that HashMap is non thread safe and hashtable is thread safe. However, because hashtable is synchronized, it is equivalent to all threads competing for a lock when reading and writing, resulting in very low efficiency.

Concurrent HashMap can read data without locking, and its internal structure allows it to keep the lock granularity as small as possible when writing, without locking the whole concurrent HashMap.

Internal structure of concurrenthashmap

In order to improve its concurrency, concurrenthashmap internally adopts a structure called segment, which is actually a hash table like structure. Segment internally maintains a linked list array. Let's use the following figure to see the internal structure of concurrenthashmap:

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>