java - What are the specific business usage scenarios of high-performance classes such as ConcurrentHashMap?
给我你的怀抱
给我你的怀抱 2017-06-28 09:23:10
0
4
1190

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

给我你的怀抱
给我你的怀抱

reply all(4)
某草草

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:

// 在线用户管理类
public class UserManager {
    private Map<String, User> userMap = new ConcurrentHashMap<>();
    
    // 当用户登入时调用
    public void onUserSignIn(String sessionId, User user) {
        this.userMap.put(sessionId, user);
    }
    
    // 当用户登出或超时时调用
    public void onUserSignOut(String sessionId) {
        this.userMap.remove(sessionId);
    }
    
    public getUser(String sessionId) {
        return this.userMap.get(sessionId);
    }
}

When there are many users logging in and out at the same time, onUserSignIn() and onUserSignOut() 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.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template