为什么 `synchronized (lock)` 被不同的线程输入了两次?
Why `synchronized (lock)` was entered twice by different threads?
在这个简单的例子中,我有两个 synchronized (theLock)
被不同的线程访问
public class Main {
public static void main(String[] args) throws InterruptedException {
System.out.println("start");
final Object theLock = new Object();
synchronized (theLock) {
System.out.println("main thread id : " + Thread.currentThread().getId());
new Thread(() -> {
System.out.println("new thread id : " + Thread.currentThread().getId() + ". Inside thread");
// before entering this section new thread should be blocked as `theLock` is already acquired
synchronized (theLock) {
System.out.println("inside synchronized");
theLock.notify();
}
}).start();
theLock.wait();
}
System.out.println("end");
}
}
为什么新创建的线程可以访问里面的synchronized (theLock)
段?据我了解, theLock
已经被主线程获取,新线程应该永远阻塞。相反,我看到它也进入了 synchronized
。
这是一个输出
start
main thread id : 1
new thread id : 13. Inside thread
inside synchronized
end
对wait()
的调用释放了锁。 Per wait()
Javadoc(加粗我的):
Causes the current thread to wait until another thread invokes the
notify()
method or the notifyAll()
method for this object. In
other words, this method behaves exactly as if it simply performs the
call wait(0)
.
The current thread must own this object's monitor. The thread
releases ownership of this monitor and waits until another thread
notifies threads waiting on this object's monitor to wake up either
through a call to the notify
method or the notifyAll
method. The
thread then waits until it can re-obtain ownership of the monitor and
resumes execution.
在这个简单的例子中,我有两个 synchronized (theLock)
被不同的线程访问
public class Main {
public static void main(String[] args) throws InterruptedException {
System.out.println("start");
final Object theLock = new Object();
synchronized (theLock) {
System.out.println("main thread id : " + Thread.currentThread().getId());
new Thread(() -> {
System.out.println("new thread id : " + Thread.currentThread().getId() + ". Inside thread");
// before entering this section new thread should be blocked as `theLock` is already acquired
synchronized (theLock) {
System.out.println("inside synchronized");
theLock.notify();
}
}).start();
theLock.wait();
}
System.out.println("end");
}
}
为什么新创建的线程可以访问里面的synchronized (theLock)
段?据我了解, theLock
已经被主线程获取,新线程应该永远阻塞。相反,我看到它也进入了 synchronized
。
这是一个输出
start
main thread id : 1
new thread id : 13. Inside thread
inside synchronized
end
对wait()
的调用释放了锁。 Per wait()
Javadoc(加粗我的):
Causes the current thread to wait until another thread invokes the
notify()
method or thenotifyAll()
method for this object. In other words, this method behaves exactly as if it simply performs the callwait(0)
.The current thread must own this object's monitor. The thread releases ownership of this monitor and waits until another thread notifies threads waiting on this object's monitor to wake up either through a call to the
notify
method or thenotifyAll
method. The thread then waits until it can re-obtain ownership of the monitor and resumes execution.