High CPU due to multiple Java threads accessing HashMap simultaneously

Solution Verified - Updated

Environment

  • Java
  • JBoss Enterprise Application Platform (EAP)

Issue

  • High cpu and threads consuming the CPU have HashMap access at the top of the stack trace. For example:
"RMI TCP Connection(4468)-160.57.110.76" daemon prio=6 tid=0x5a436400 nid=0x9e4 runnable [0x6068d000]
   java.lang.Thread.State: RUNNABLE
     at java.util.HashMap.get(HashMap.java:303)
     at com.example.service.getWidget(Component.java:796)
...

"WorkManager(3)-122" daemon prio=6 tid=0x5b496000 nid=0x18fc runnable [0x70b2e000]
   java.lang.Thread.State: RUNNABLE
     at java.util.HashMap.get(HashMap.java:303)
     at com.example.service.getWidget(Component.java:796)
...

Resolution

  • Replace HashMap with ConcurrentHashMap.

Root Cause

Diagnostic Steps

Category

This solution is part of Red Hat’s fast-track publication program, providing a huge library of solutions that Red Hat engineers have created while supporting our customers. To give you the knowledge you need the instant it becomes available, these articles may be presented in a raw and unedited form.