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.

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b3abf4-2b9b6.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b3abf4-2b9b6.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?