How does hashmap ensure visibility?

topic description

Why can hashmap guarantee the visibility of its element node? the following code thread B can end immediately. I don"t think the source code hashmap uses volatile or locking.

related codes

/ / Please paste the code text below (do not replace the code with pictures)
public class b {

public static Map<Integer,Integer> map = new HashMap<>();
static {
    map.put(1,2);
}
public static void main(String[] args) throws Exception {
    Object o = new Object();
    new Thread(() -> {             //A
        try {
            TimeUnit.MILLISECONDS.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        map.put(1,3);
    }).start();
    new Thread(() -> {            //B
        while (true) {
            if (map.get(1)==3) {
                System.out.println("");
                break;
            }
        }
    }).start();
}

}

Feb.03,2022

there is no guarantee. You observe that once it is not "guarantee", on the contrary, it is "no guarantee", not "guarantee no".

ConcurrentHashMap is guaranteed


it takes 0.1 seconds to see it and 0.001 seconds to see it. For your experimental code, you see it right away.

Menu