0

I was trying to understand the underlying flow of the put(K key, V value) method of ConcurrentHashMap in OpenJDK 11, tried running it in debug mode with the below sample code how put(K key, V value) method flow works internally, instead of understanding the flow got confused looking at high no of Hit count in IntelliJ debug Overhead tab [Refer screenshot] putVal(K key, V value, boolean onlyIfAbsent) hit count showing for 131 hits. Just commented 2 put calls, and kept only map2.put(100, "A") still 129 no of times Hit count it shows.

Maybe I am asking a stupid question without understanding its internals (as it is so complex to understand step-by-step even with debug mode) but curious to understand a bit of underlying flow.

Experts! can anyone put a light on my brain with a simplified explanation of the flow of how it works?

Sample Code:

import java.util.concurrent.ConcurrentHashMap;

public class Main {

    static ConcurrentHashMap<Integer, String> map2 = new ConcurrentHashMap<>();

    public static void main(String[] args) {
        map2.put(100, "A");
        map2.put(101, "B");
        map2.put(102, "C");

        for (Object o : map2.entrySet()) {
            System.out.println(o);
        }
        System.out.println(map2);
    }
}

Console Output: enter image description here

Console output text for your reference:

C:\Software\jdk-11\bin\java.exe -agentlib:jdwp=transport=dt_socket,address=127.0.0.1:59187,suspend=y,server=n -javaagent:C:\Users\ravi\AppData\Local\JetBrains\IdeaIC2021.2\captureAgent\debugger-agent.jar -Dfile.encoding=UTF-8 -classpath "C:\POCRepo\Gateway\ConcurrentHashMapInternalFlowTest\out\production\ConcurrentHashMapInternalFlowTest;C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2021.1.1\lib\idea_rt.jar" com.company.Main
Connected to the target VM, address: '127.0.0.1:59187', transport: 'socket'
100=A
101=B
102=C
{100=A, 101=B, 102=C}
Disconnected from the target VM, address: '127.0.0.1:59187', transport: 'socket'

Process finished with exit code 0

Debugger Tab: enter image description here

ravibeli
  • 484
  • 9
  • 30

1 Answers1

0

Your breakpoint it's on ConcurrentHashMap.putVal(). putVal() is a private method of your Java!

My guess! The number of hits you see is not related to your code. It's another ConcurrentHashMap instance used by your IDE during the debug.

Try to enable the breakpoint ConcurrentHashMap.putVal() only when reaching the static initialisation of ConcurrentHashMap and see the number of hits is only three as expected.

  • I specifically put the break point at putVal(..) method to understand internals to run through step-by-step execution. But got into a loop of much internal logic and got confused. Not point here is not to get no of hits to get for my put method, rather internals I want to understand. My intent is to understand flow in my mind how the internal logic goes while the put and get method is called. Gone through a few blogs flow explained is not matching to the source code of OpenJDK11 ConcurrentHashMap implementation. – ravibeli Aug 09 '21 at 16:32