In-depth java concurrent programming-ConcurrentHashMap

This article is for reading notes, the book is the art of Java concurrent programming. The
book seems to be 1.6.
I found that the jdk8 can't find the segments.

1. Why use ConcurrentHashMap?

In a multi-threaded environment, using HashMap for put operations will cause an endless loop, resulting in CPU utilization close to 100%, so HashMap cannot be used in concurrent situations.

HashMap will cause an endless loop when executing put operations concurrently, because multithreading will cause the HashMap Entry linked list to form a ring data structure. Once the ring data structure is formed, the next node of the Entry will never be empty, and an endless loop will be generated to obtain the Entry.

The principle is: when adding elements to HashMap, it will cause the expansion of HashMap container
can refer to

ConcurrentHashMap's lock segmentation technology can effectively improve the concurrent access rate; the reason why the HashTable container shows inefficiency in a highly competitive concurrent environment is that all threads accessing the HashTable must compete for the same lock. If there are multiple locks in the container , Each lock is used to lock part of the data in the container, so when multiple threads access data in different data segments in the container, there will be no lock competition between threads, which can effectively improve the efficiency of concurrent access. This is the lock used by ConcurrentHashMap Segmentation technology.

2. The structure of ConcurrentHashMap (not 1.8 structure)

Insert picture description here
Segment is a reentrant lock (ReentrantLock), which plays the role of lock in ConcurrentHashMap;

HashEntry is used to store key-value data.

A ConcurrentHashMap contains an array of Segments.

A Segment contains a HashEntry array. Each HashEntry is an element of a linked list structure. Each Segment guards the elements in a HashEntry array. When modifying the data of the HashEntry array, you must first obtain the segment lock corresponding to it .

static class Segment<K,V> extends ReentrantLock implements Serializable {

    }

It can be seen that Segment inherits from ReentrantLock, you know, it can act as a lock

jdk1.6 ConcurrentHashMap see this article

ConcurrentHashMap structure (1.8 structure)

CAS+synchronized+hashentry

https://blog.csdn.net/programmer_at/article/details/79715177

The difference between jdk1.7 and 1.8

https://www.jianshu.com/p/e694f1e868ec
https://blog.csdn.net/xingxiupaioxue/article/details/88062163

Can ConcurrentHashMap completely replace HashTable?

Strong consistency and weak consistency

In fact, there are only two types of data consistency, strong consistency and weak consistency. Strong consistency is also called linear consistency. In addition to this, all other consistency is a special case of weak consistency. The so-called strong consistency means that the replication is synchronous, and the weak consistency means that the replication is asynchronous.

The user updates the avatar of the website. At some point, the user sends an update request to the main library, and the main library receives the request shortly after. At a certain moment, the master database will forward the data changes to its own slave database. Finally, the main library informs the user that the update was successful.

If the master library needs to wait for confirmation from the slave library before returning the "update successful" and making the new avatar visible to other users, to ensure that the slave library has received a write operation, the replication is synchronized, that is, strong consistency. If the master library writes successfully and does not wait for the slave library's response, it returns "update success" directly, then the replication is asynchronous, that is, weak consistency.

Strong consistency can ensure that the slave library has consistent data with the master library. If the main library suddenly goes down, we can still guarantee data integrity. However, if the secondary library is down or the network is blocked, the primary library cannot complete the write operation.

In practice, we usually make a slave library synchronous, while others are asynchronous. If there is a problem with this synchronous slave, make another asynchronous slave synchronize. This ensures that there are always two nodes with complete data: the master library and the synchronous slave library. This configuration is called semi-synchronous.

https://my.oschina.net/hosee/blog/675423

Published 37 original articles · won praise 6 · views 4630

Guess you like

Origin blog.csdn.net/littlewhitevg/article/details/105604100