[英]Does a thread exit out of every synchronized method or object upon blocking?
If I have something like this如果我有这样的事情
synchronized void doSomething() throws Exception {
synchronized (lock1) {
lock1.wait();
}
wait();
}
Once it blocks at lock1.wait()
, will some other thread be able to access doSomething()
and do work in it?一旦它在
lock1.wait()
处lock1.wait()
,其他线程是否能够访问doSomething()
并在其中工作?
What if the blocked thread gets signaled and regains entry to the method?如果被阻塞的线程收到信号并重新获得方法的入口怎么办? will it block on
wait()
or not?它会阻塞
wait()
还是不阻塞?
No, calling wait()
releases the lock on the object that you're calling wait()
on only.不,调用
wait()
只会释放您正在调用wait()
的对象上的锁。 In the example code the lock on lock1
will be released, but the lock on this
is still kept by the thread.在示例代码上的锁
lock1
将被释放,但在锁定this
仍然是由线程保持。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.