public class TT extends Thread {
int b = 100;
public synchronized void m1()throws Exception{
b=1000;
Thread.sleep(3000);
System.out.println("m1:"+"b="+b);
}
public void m2(){
System.out.println("m2"+b);
}
public void run(){
try {
m1();
} catch (Exception e) {
// TODO: handle exception
}
}
public static void main(String[] args) throws Exception {
TT tt = new TT();
Thread t = new Thread(tt);
t.start();
Thread.sleep(100);
tt.m2();
}
}
答案是 先输出m2的1000 在输出m1的1000,这里我就不懂了,int b = 100;前面没有加static,为什么被改变了?我以为m2应该输出还是100的,公共变量,只要下面哪个方法里改了,这个变量本身值就真的改了吗?不需要加static的? |
|