Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to troubleshoot ConcurrentHashMap source code

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces "how to troubleshoot ConcurrentHashMap source code". In daily operation, I believe many people have doubts about how to troubleshoot ConcurrentHashMap source code. The editor consulted all kinds of data and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubt of "how to troubleshoot ConcurrentHashMap source code". Next, please follow the editor to study!

ConcurrentHashMap, one Bug

Topic: ConcurrentHashMap, a Bug

When I was writing business code, I used HashMap to save something, but there may be concurrent operations, so I changed to a higher-end ConcurrentHashMap, and then I encountered a NullPointerException exception when debugging.

So I debug a bit, found a null data stored in, the code threw this exception for me. Is really high-end, I am using HashMap storage no problem, this is not good.

So check the ConcurrentHashMap source code, found in the put when there is the following line of code, this is not very clear, no need to bother.

My bug. Png

But I can't just Stop here. So I checked ConcurrentHashMap API again, as follows:

ConcurrentHashMap introduces .png

Note this: this class is similar to Hashtable, but unlike HashMap, it does not allow null to be used as a key or value.

That means that both ConcurrentHashmap and Hashtable support concurrency, and if both specify that key,value cannot be null,null, a null pointer exception will be thrown. Take another look at HashMap, which allows key and value to be null.

I know this, so why design it this way? Look up the data and see that stackoverflow:

Why-does-concurrenthashmap-prevent-null-keys-and-values

After reading English, I have a general understanding:

The main reason for not using nulls is that there is ambiguity in the case of concurrency. If map.get (key) returns null, you cannot detect whether the key created the mapping as null, or if it has not been mapped. Not in concurrent mapping, it can be checked by map.contains (key), while in concurrent mapping, the mapping between two calls may have changed.

At this point, the study on "how to troubleshoot ConcurrentHashMap source code" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report