在下面的代码中,我试图找出线程处理同步块上的执行:
public class ThreadExp {
public static void main(String[] args) throws InterruptedException {
MyRunnable r = new MyRunnable();
Thread t1 = new Thread(r, "Thread1");
Thread t2 = new Thread(r, "Thread2");
t1.start();
t2.start();
}
}
class MyRunnable implements Runnable {
@Override
public void run() {
callFn();
}
private void callFn() {
System.out.println(Thread.currentThread().getName() + ": entered callSync()");
synchronized (Thread.currentThread()) {
System.out.println(Thread.currentThread().getName() + ": inside sync");
try {
Thread.currentThread().sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + " leaving sync");
}
System.out.println(Thread.currentThread().getName() + ": leaving callSync()");
}
}
实际输出:
Thread1: entered callFn()
Thread2: entered callFn()
Thread1: inside sync block
Thread2: inside sync block
// 5000 millisec sleep
Thread2 leaving sync block
Thread1 leaving sync block
Thread2: leaving callFn()
Thread1: leaving callFn()
而我期望的是:
Thread1: entered callFn()
Thread2: entered callFn()
Thread1: inside sync block
// 5000 millisec sleep
Thread1 leaving sync block
Thread1: leaving callFn()
Thread2: inside sync block
Thread2 leaving sync block
Thread2: leaving callFn()
总的来说,我认为 Thread1 将获取锁然后进入睡眠状态。并且只有在 Thread1 完成后,Thread2 才能进入同步块。