代码如下所示:
package thread;
public class TT implements Runnable{
int b = 100;
/**
* @param args
*/
public static void main(String[] args) throws Exception {
TT tt = new TT();
Thread t = new Thread(tt);
t.start();
Thread.sleep(1000);
tt.m2();
}
public synchronized void m1() throws Exception{
b = 100000;
Thread.sleep(5000);
System.out.println("b="+b);
}
public void m2(){
System.out.println("b= "+b);
b=1;
}
@Override
public void run() {
try {
m1();
} catch (Exception e) {
e.printStackTrace();
}
}
}
输出:
b= 100000
b=1
能说明这个锁方法并没有锁住当前变量,导致被其他方法修改么?
如何修改类,使得该变量被锁定?