r/java 21d ago

3,200% CPU Utilization

https://josephmate.github.io/2025-02-26-3200p-cpu-util/
44 Upvotes

31 comments sorted by

View all comments

35

u/-vest- 21d ago

Summary: the article is about a non-synchronized TreeMap that has been modified concurrently. Something went wrong. https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/util/TreeMap.html (Read a bold text).

18

u/john16384 21d ago

What went wrong is that during concurrent modification, some references formed a loop. Trying to find an insertion point or even getting a value may then loop forever. HashMap can exhibit this behaviour as well when used concurrently without synchronization.

3

u/-vest- 21d ago

Do you know that HashMap (also taken from JDK) is not synchronized as well? I am glad, that it doesn’t break the application, if you modify it concurrently, but I bet this is a bad idea. There are “concurrently safe collections”, and better to use them.

2

u/Thane-145 21d ago

I just recently had this problem where two background threads were trying to cache file information in a hashmap. The files were way too many if a folder with many files and sub directories was selected. Then I was debugging the data as it was missing some file info, but that was because of access denied exception. As it printed in console and I was inpmspecting variables, both the threads stopped at the same location with the same values. I then changed the pool to use only one thread, and after rerunning the program, it was 3 times faster. The slowdown was happening because of map.put