简体   繁体   English

wait()如何在Java中获取Lock

[英]How does wait() get the Lock back in Java

It is an advocated paradigm that wait() should be invoked inside a while loop inside a synchronized block. 这是一个倡导的范例,应该在synchronized块内的while循环内调用wait()。

My question is how does the waiting() thread get the lock back ? 我的问题是waiting()线程如何获得锁定?

// Thread 1
    synchronized (mon) {
     while (!condition) 
          mon.wait();

    // Do something
    }

//Thread 2
    synchronized (mon) {//set condition appropriately
            mon.notify();
    }

Consider the thread 1 runs first and starts waiting for the condition. 考虑线程1首先运行并开始等待条件。 It releases the lock and the thread 2 obtains the lock sets the condition and notifies thread 1. Now thread 1 gets the lock, checks the condition and starts executing "do something". 它释放锁并且线程2获得锁设置条件并通知线程1.现在线程1获取锁,检查条件并开始执行“做某事”。

My question is when Thread 1 is notified it starts execution from the while condition, the line of code which had Synchronized(mon) is never executed again then how does thread 1 acquire the lock ? 我的问题是,当线程1被通知它从while条件开始执行时,具有Synchronized(mon)的代码行永远不再执行,那么线程1如何获得锁定? What are the internal dynamics that give the lock back to Thread 1 ? 将锁定回到线程1的内部动态是什么?

When Thread1 is notified the thread has to acquire the lock before it can exit the wait method, see the java doc for Object#wait: 当Thread1被通知时,线程必须先获取锁,然后才能退出wait方法,请参阅java doc for Object#wait:

The thread T is then removed from the wait set for this object and re-enabled for thread scheduling. 然后从该对象的等待集中删除线程T并重新启用线程调度。 It then competes in the usual manner with other threads for the right to synchronize on the object; 然后它以通常的方式与其他线程竞争,以便在对象上进行同步; once it has gained control of the object, all its synchronization claims on the object are restored to the status quo ante - that is, to the situation as of the time that the wait method was invoked. 一旦它获得了对象的控制权,它在对象上的所有同步声明都将恢复到原状 - 即,调用wait方法时的情况。 Thread T then returns from the invocation of the wait method. 线程T然后从wait方法的调用返回。 Thus, on return from the wait method, the synchronization state of the object and of thread T is exactly as it was when the wait method was invoked. 因此,从wait方法返回时,对象和线程T的同步状态与调用wait方法时的状态完全相同。

synchronized(mon) is not an expression that has to be executed. synchronized(mon)不是必须执行的表达式。

It's a syntax element in the source code that tells the compiler (and then the runtime) that the wrapped section of the code must only be executed after the lock associated with mon has been acquired by the current thread, even if you don't "come from" the line of code before the synchronized block. 它是源代码中的语法元素,告诉编译器(然后是运行时)代码的包装部分必须仅在当前线程获取与mon关联的锁之后执行,即使您没有“来自“同步块之前的代码行。

wait() releases the lock, and must reacquire it before returning. wait()释放锁,并且必须在返回之前重新获取它。

After Thread 1 is notified, it got the lock immediately and start to run //Do something . 通知线程1后,它立即获得锁定并开始运行//执行某些操作

When Thread 1 wait, it just release the lock temporarily, and when the thread is notified, it can get the lock again and needn't run synchronized(...). 当线程1等待时,它只是暂时释放锁,并且当通知线程时,它可以再次获得锁定并且不需要运行synchronized(...)。

// Thread 1
 synchronized (mon) {
 Systemout.println("I am invoked!");
 while (!condition) 
      mon.wait();

// Do something
}

//Thread 2
synchronized (mon) {//set condition appropriately
        mon.notify();
}

In the original scenario: Consider the thread 1 runs first and starts waiting for the condition. 在原始场景中:考虑线程1首先运行并开始等待条件。 It releases the lock and the thread 2 obtains the lock sets the condition and notifies thread 1. Now thread 1 gets the lock, checks the condition and starts executing "do something". 它释放锁并且线程2获得锁设置条件并通知线程1.现在线程1获取锁,检查条件并开始执行“做某事”。

If my understand the following correctly: 如果我理解以下内容:

The thread T is then removed from the wait set for this object and re-enabled for thread scheduling. 然后从该对象的等待集中删除线程T并重新启用线程调度。 It then competes in the usual manner with other threads for the right to synchronize on the object; 然后它以通常的方式与其他线程竞争,以便在对象上进行同步; once it has gained control of the object, all its synchronization claims on the object are restored to the status quo ante - that is, to the situation as of the time that the wait method was invoked. 一旦它获得了对象的控制权,它在对象上的所有同步声明都将恢复到原状 - 即,调用wait方法时的情况。 Thread T then returns from the invocation of the wait method. 线程T然后从wait方法的调用返回。 Thus, on return from the wait method, the synchronization state of the object and of thread T is exactly as it was when the wait method was invoked. 因此,从wait方法返回时,对象和线程T的同步状态与调用wait方法时的状态完全相同。

the line Systemout.println("I am invoked!"); Systemout.println行(“我被调用!”); will not be executed, as "Thus, on return from the wait method, the synchronization state of the object and of thread T is exactly as it was when the wait method was invoked." 不会被执行,因为“因此,从wait方法返回时,对象和线程T的同步状态与调用wait方法时的状态完全相同。”

Am I right? 我对吗?

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

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