简体   繁体   English

Java:如果始终在释放锁之前调用notify(),那么等待的线程将如何获取该相同的锁?

[英]java: If notify() is always called before the lock release, how could a waiting threads acquire this same lock?

I think that I already know the answer to that question, however, I would like to read your opinions to make sure that I really understand how java thread's state machine (or diagram) works. 我认为我已经知道了该问题的答案,但是,我想阅读您的意见以确保我真正了解Java线程的状态机(或图)的工作方式。

Imagine that Thread A runs the notify() just before return a given value: 想象一下,线程A在返回给定值之前运行notify()

public class baz{

    // Thread B runs this:
    public synchronized void bar(){
       wait();
    } 

    // Thread A runs this:
    public synchronized int foo(){
       notify();
       return 11;
    }
}

notify() will be called before Thread A releases the lock (that will occurs "after" the return 11 ; statement). 在线程A释放锁之前将调用notify() (这将在return 11 ;语句之后“发生”)。 So, how could a Thread B, that is waiting for this lock (via wait() method), acquire the lock that is still held by Thread A? 那么,正在等待此锁的线程B(通过wait()方法)如何获取仍由线程A持有的锁? Note that, when thread B is notified the lock has not yet been released by Thread A. 请注意,当通知线程B时,线程A尚未释放锁定。

So what I think of this situation is the following: 所以我对这种情况的看法如下:

After calling wait() , Thread B will change its state from Running to Waiting . 调用wait()之后 ,线程B将其状态从Running更改为Waiting After receiving the notification (from Thread A notify() method), Thread B will return from wait() , change its state to Runnable and try to acquire the lock. 从线程A notify()方法接收到通知后,线程B将从wait()返回,将其状态更改为Runnable并尝试获取锁。 Since the lock is not yet released by Thread A, Thread B will be blocked on the object's monitor and pass its state from Runnable to Blocked . 由于线程A尚未释放该锁,因此线程B将在对象的监视器上被阻止,并将其状态从Runnable传递给Blocked Eventually, after Thread A releases the lock, Thread B will acquire the lock and pass its state from Blocked to Running . 最终,在线程A释放锁之后,线程B将获取该锁并将其状态从Blocked传递到Running

Is this right? 这是正确的吗? What I want to understand with this question is what happens to a thread that returns from a wait() which is synchronized by an already acquired lock. 我想用这个问题来理解的是,从已经被获取的锁同步的wait()返回的线程发生什么

Yes, your explanation is correct. 是的,您的解释是正确的。

When you call wait() on an object, the calling thread will be added to the object's wait set . 当您在对象上调用wait()时,调用线程将被添加到对象的wait set中 When it is notify() ied, it will be removed from that wait set, and perform a lock action on the object (it's within a synchronized block on that object). 当它是notify() ied时,它将被从该等待集中删除,并对该对象执行锁定操作 (它在该对象的synchronized块内)。 That lock action will block the current thread until it is complete, ie. 该锁定动作将阻塞当前线程,直到完成为止。 locked the object monitor. 锁定了对象监视器。

This is the exact same behavior that two threads would have when trying to enter a synchronized block on the same object. 这是两个线程试图在同一对象上输入synchronized块时完全相同的行为。 The first thread to reach would lock the object monitor, completing the lock object immediately. 到达的第一个线程将锁定对象监视器,立即完成锁定对象。 The other thread blocks until its lock action is complete, ie. 其他线程将阻塞,直到其锁定动作完成为止。 after the first thread unlocks the monitor. 在第一个线程将监视器解锁之后。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM