简体   繁体   English

线程从Object.notifyAll中被唤醒并且无法获得锁定?

[英]where are the threads waken up from Object.notifyAll and failing to get the lock?

Below is how monitor works: monitor 以下是监视器的工作原理: 监视器

  • Threads in wait set are waken up when Object.notifyAll() is called. 调用Object.notifyAll()时,唤醒等待集中的线程。
  • Only one of them get the lock, while the others are blocked again. 只有其中一个获得锁定,而其他人再次被阻止。
  • So where do the blocked threads go?Will they go back to wait set or go to entry set?Or this is managed by the os, because monitor depends on the os MutexLock? 那么被阻塞的线程在哪里?它们会回到等待集还是进入入口集?或者这是由os管理的,因为监视器依赖于os MutexLock?

The monitor is a fundamental conception that you should understand. 显示器是您应该理解的基本概念。 It is better to read about it somewhere. 最好在某个地方阅读它。

In short, I could say that there are some major principles: 简而言之,我可以说有一些主要原则:

  1. If a thread go into the synchronized block - the monitor of synchronized object is blocked and all other threads couldn't execute the synchronized block (they are in the Entry Set). 如果一个线程进入同步块 - 同步对象的监视器被阻塞,所有其他线程无法执行同步块(它们在条目集中)。
  2. If you call wait() method on synchronized object, then the thread go to Wait Set 如果在同步对象上调用wait()方法,则该线程转到Wait Set
  3. If you call notify()/notifyAll() method on synchronized object, it means that one/all thread(s) go to Entry Set from Wait Set. 如果在同步对象上调用notify()/ notifyAll()方法,则表示一个/所有线程从Wait Set转到Entry Set。

The answer to your question is - when you call notifyAll() method, all threads from Wait Set go to Entry Set. 您的问题的答案是 - 当您调用notifyAll()方法时,来自Wait Set的所有线程都转到Entry Set。

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

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