public class Sync implements Runnable{
int b = 100;
public synchronized void m1() throws Exception {
b = 1000;
Thread.sleep(3000);
System.out.println("sync " + b);
}
public void m2() {
System.out.println(b);
}
public void run() {
try {
m1();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws InterruptedException {
Sync s = new Sync();
Thread t = new Thread(s);
t.start();
Thread.sleep(3000);
s.m2();
}
}
这个程序为什么输出的是下面这个啊?m2方法输出的应该是100啊,把b赋值为1000的语句被synchronized锁定了,其他对象明明是不能访问的啊
1000
sync 1000
道临
相关分类