topic description
when volatile decorates objects and arrays, it only ensures the visibility of their reference addresses, but why does the following code print "over" immediately after I add volatile? if you don"t add volatile to the array, it will never print. What are the detailed steps for a thread to manipulate its fields or elements when volatile modifies objects and arrays? Ask the Great God for advice
related codes
/ / Please paste the code text below (do not replace the code with pictures)
public class b {
public static volatile int[] ints = new int[5];
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();
}
ints[0] = 2;
}).start();
new Thread(() -> { //B
while (true) {
if (ints[0] == 2) {
System.out.println("");
break;
}
}
}).start();
}
}