假如你要调用某个对象的wait()方法,一般都要放在synchronized声明里面,why? 下面是答案。
Suppose d
is the object we're using to invoke wait
. When a thread invokes d.wait
, it must own the intrinsic lock for d
— otherwise an error is thrown. Invoking wait
inside a synchronized method is a simple way to acquire the intrinsic lock.
When wait
is invoked, the thread releases the lock and suspends execution. At some future time, another thread will acquire the same lock and invoke Object.notifyAll
, informing all threads waiting on that lock that something important has happened。