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();
}
}