We are aware that HashMap in Java is not thread safe. Multithreaded usage may cause corruption of the data structures used by HashMap. This may result in loss of keys or incorrect values for keys.
I realized it's not just limited to that, following is the issue we faced in one of our legacy applications. The hash map (non-thread safe) usage resulted in an infinite loop.
I had been looking at CPU usage of the application, running on a 16 core box. CPU usage was 400%; 4 cores of 16
cores were busy. There was absolute zero external load on it. The top output of the same, is shown below
Following is the thread stack of the busy threads.
All of them are stuck in HashMap. The hash map put is in infinite loop.
JDK src code for HashMap: http://www.docjar. com/html/api/java/util/HashMap .java.html
Review of the line 494 in JDK src code (thread stack shows the same) along with the excellent blog http://mailinator.blogsp ot.in/2009/06/beautiful-race- condition.html, the cause for the infinite loop can be understood.