一个关于线程同步的问题
JAVA CODE:
public class TestLock1 implements Runnable {
int b=100;
public synchronized void m1() throws Exception {
b=1000;
Thread.sleep(5000);
System.out.println("b="+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 Exception{
TestLock1 tl = new TestLock1();
Thread t = new Thread(tl);
t.start();
Thread.sleep(1000);
tl.m2();
}
}
输出结果:1000
b=1000
我觉得流程是这样的:首先主线程睡眠,线程t1起来,执行到b=1000;时,线程t1开始睡眠,主线程开始执行m2()方法,但我不明白m2()方法体内部是怎么访问到b=1000的,m1()不是已经被锁住了吗?