本帖最后由 曾欢欢 于 2014-5-7 09:12 编辑
public class TT implements Runnable {
int b = 100;
public synchronized void m1() throws Exception{
//Thread.sleep(2000);
b = 1000;
Thread.sleep(5000);
System.out.println("b = " + b);
}
public synchronized void m2() throws Exception {
b = 2000;
Thread.sleep(2500);
System.out.println("new b =" + b);
}
public void run() {
try {
m1();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws Exception {
TT tt = new TT();
Thread t = new Thread(tt);
t.start();
tt.m2();
System.out.println(tt.b);
}
}
此程序输出结果为
new b = 2000
2000
b=1000
为什么将System.out.println("new b =" + b);注释掉,输出结果变为
1000
b = 1000
为什么不是
2000
b = 1000
|
|