I have an enterprise application which was eating so much of CPU. As dug further into thread dump, I see the threads that are eating so much CPU is stuck at TreeMap.put
. I see a similar question here due to the same problem.
I understand people mentioned there that TreeMap is not thread safe. But what I don't understand is, not having ThreadSafe might cause inconsistent results. But why it holds the CPU?
Multithreaded access to an unsynchronized TreeMap
can not only cause inconsistent data to be seen, but can actively lead to infinite loops.
Infinite loops can, of course, take infinite amounts of time. So really, really don't have multithreaded access to a TreeMap
. Use a concurrent implementation like ConcurrentSkipListMap
, or synchronize properly.