As per the question, what are the specific business usage scenarios of high-performance classes like ConcurrentHashMap?
Usually when we write code, we usually use HashMap the most, and we are accustomed to it
But there are other high-performance Maps in Java, and I feel like I don’t usually use them
After searching on Baidu, I only saw the difference between HashMap and ConcurrentHashMap
But I don’t know which specific business usage scenarios will use these classes
So I feel a little confused, I hope seniors can give me some guidance, thank you
ConcurrentHashMap is a tool class under the java.util.concurrent package to prevent concurrency. For business use, your own concurrency class can also implement ThreadLocal. . .
Mainly used in multi-threading. Earlier versions of Java used synchronized synchronization blocks, which was not easy to solve problems such as locking and releasing. Now that everything is officially written in Java, just look at multi-threading. . .
Business scenario: For example, the company's personnel information is stored in a multi-threaded database. If you use java.util.List, the data of each thread will be inconsistent. In this case, you need to use a concurrency tool class. The main thing to note is that when
writing
data, reading data does not matter. . .ConcurrentHashMap is specially designed for access by multiple threads. For example:
When there are many users logging in and out at the same time,
onUserSignIn()
andonUserSignOut()
will be called by many threads at the same time.Thanks for the invitation.
You have to know first:
Performing operations on an unprotected object in multiple threads will result in inconsistent status for everyone.
You can open several threads to put and remove an ordinary hashMap, provided that it is under certain conditions, such as the elements inside meet certain requirements, greater than or equal to a certain number, etc.
ConcurrentHashMap
is a concurrent map provided in the jdk concurrency package, which can effectively prevent object copy inconsistency when multiple threads operate an object.You can search on Baidu for the scene.
Needless to say the difference. You should already know these.
In order to ensure thread safety, we generally use the Synchronize keyword. With ConcurrentHashMap, you no longer need to use the cumbersome Synchronize method. In addition, ConcurrentHashMap is generally used in multi-threaded situations Read more and write less situation. Not all multi-threads can use this concurrency tool class.