简体   繁体   English

线程是否在阻塞时退出每个同步方法或对象?

[英]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.

相关问题 线程调用同步方法是否使用相同的对象抢占另一个线程但是以非同步的方式? - Does a thread invoking, synchronized method, pre-empt another thread using same object but in a non synchronized manner? synchronized块没有阻塞对象 - synchronized block not blocking the object Thread.sleep也阻塞其他线程,处理其他方法,同时在synchronized方法中调用 - Thread.sleep is blocking other thread also, working on other method, along with itself callled inside synchronized method 如果在synchronized方法中调用,thread.yield()是否会丢失对象的锁定? - Does thread.yield() lose the lock on object if called inside a synchronized method? 同步方法中的线程锁定 - Thread locking in synchronized method 线程子类中的同步方法 - Synchronized method in thread subclass IllegalMonitorStateException: 对象在 wait() 之前未被线程锁定,使用同步静态方法 - IllegalMonitorStateException: object not locked by thread before wait(), using synchronized static method 同步方法覆盖线程在哪个对象上获取锁? - synchronized method override- thread acquires lock on which object? Android线程阻止UI,除非使用同步 - Android Thread Blocking UI Unless Using Synchronized 同步,锁定和等待阻止主UI线程 - Synchronized, lock and wait blocking main UI thread
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM