以下是我的测试对象加锁的代码,但是对运行出来的结果不太理解
public class Test0 implements Runnable {
int b = 100;
public synchronized void m1() throws Exception {
b = 1000;
System.out.println(b+1);
Thread.sleep(5000);
System.out.println("b=" + b);
}
public synchronized void m2() throws Exception {
Thread.sleep(2500);
b = 2000;
}
@Override
public void run() {
try {
m1();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) throws Exception {
Test0 test0 = new Test0();
//System.out.println(test0.b);
Thread t = new Thread(test0);
t.start();
test0.m2();
System.out.println(test0.b);
}
}
这是输出的结果
![图片说明](https://img-ask.csdn.net/upload/201705/26/1495802572_224813.png)希望大神解释以下程序运行的过程,实在不理解,为什么输出1001后又直接输出了1000