B thread reads two global variables, A thread writes two global variables.
How could second
much bigger than first
?
public class ThreadTest {
private static volatile int first = 0;
private static volatile int second = 0;
private static class ReaderThread extends Thread {
@Override
public void run() {
while (first < 10000) {
if (first != second) {
System.out.println("ThreadB------first = " + first + ", second = " + second);
}
}
System.out.println("ThreadB------first = " + first + ", second = " + second);
}
}
public static void main(String[] args) throws InterruptedException {
new ReaderThread().start();
Thread.sleep(1000L);
while (first < 10000){
first++;
second++;
}
}
}
ThreadB------first = 3296, second = 3535
ThreadB------first = 7615, second = 7663
ThreadB------first = 8179, second = 8192
ThreadB------first = 8635, second = 8662
ThreadB------first = 9151, second = 9177
ThreadB------first = 10000, second = 10000
ThreadB------first = 10000, second = 10000
So what happened, and how to fix this problem?