Today, a problem in the production environment, the CPU usage of Java applications is very high, resulting in CPU utilization of the entire machine up to 90%, under normal circumstances is about 20%.
Export the thread dump and use the IBM Thread Analyzer for Java tool for analysis. There are more than 60 online threads, of which 15 threads are executing the same code in the same file, and the topmost call is Hashmap.get ().
The underlying data structure of HASHMAP is the storage of array + linked list, which is used to deal with the situation of hash collisions. Under normal circumstances the link is a linear list, when the data structure is contaminated in the concurrency situation, resulting in a ring linked list, it will lead to the infinite loop of the program.
It is unusual for 15 threads to execute the same line of code at the same time. When a thread calls Hashmap.get, when it searches on a linked list, it happens to encounter a ring link that can explain the anomaly.
About HASHMAP non-thread-safe, in the case of multi-threaded concurrent access, the introduction of ring linked list reference:
A Beautiful Race Condition
Explain the timing causing hashmap.put () to execute an infinite loop
Attached: About HashMap source Reading notes
Map/hashmap-Source Code Learning notes
[Java] High CPU usage due to HASHMAP