I'm so confused about this code :
public class SynchronizedTest implements Runnable {
private int b = 100;
public synchronized void test01() throws InterruptedException {
b = 1000;
Thread.sleep(5000);
}
public synchronized void test02() throws InterruptedException {
b = 2000;
Thread.sleep(2500);
//System.out.println("test02 end !");
}
@Override
public void run() {
try {
test01();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws InterruptedException {
SynchronizedTest test = new SynchronizedTest();
Thread thread01 = new Thread(test);
thread01.start();
test.test02();
System.out.println(test.b);
}
}
If I don't add this code System.out.println("test02 end !");
in above code.
The operation result will be print 1000 , but if put the code there the operation result will change to "test02 end !" and 2000;
I don't know why, how to explaint it ?