简体   繁体   English

Java Object作为监视器的澄清

[英]Java Object as a monitor clarification

I used following resources to get to a summary of Java monitors, 我使用以下资源来获取Java监视器的摘要,

http://www.informit.com/store/concurrent-programming-in-java-design-principles-and-9780201310092 http://www.informit.com/store/concurrent-programming-in-java-design-principles-and-9780201310092

http://www.artima.com/insidejvm/ed2/threadsynch.html http://www.artima.com/insidejvm/ed2/threadsynch.html

And following is the summary from my understanding in the context of Java , 以下是我在Java上下文中的理解摘要,

Monitor is an object that is awarded about Threads. Monitor是一个授予线程的对象。 That >means when saying in Java that all objects are monitors >that means each object has the trait of thread awareness. 那>意味着在Java中说所有对象都是监视器>这意味着每个对象都具有线程感知特征。

Thread is an active object . 线程是一个活动对象。 And other objects are passive >but are aware about threads .Hence monitors! 而其他对象是被动的>但是知道线程.Hence监视器! In other >words passive objects have been smartened. 在其他方面,被动物体已经变得灵活。

Why call "monitor" , because it monitors itself ! 为什么叫“监视器”,因为它监视自己! From what ? 从何而来 ? >From active objects ie Threads >来自活动对象,即线程

What awareness a monitor has? 监视器有什么意识?

a)It knows what thread currently access it.{owner} a)它知道当前访问它的线程。{owner}

b)It knows what threads are waiting on it till a special condition > is met.{wait set} b)它知道什么线程正在等待它直到满足特殊条件> {wait set}

c)It knows who are in the queue to enter to it{entry set} c)它知道谁在队列中输入{entry set}

So how could a thread become an objects owner , it has to >acquire that object's lock ! 那么一个线程如何成为一个对象所有者,它必须>获取该对象的锁定!


1 Object IS a lock or HAS a lock ? 1对象是一个锁还是一个锁?

2 Above a,b,c are part of the state of an object or keep tracked by the JVM ( keep a record and associate it to the Object ID etc.)? 2在a,b,c之上是对象状态的一部分或由JVM跟踪(保留记录并将其与对象ID等关联)?

Each object in Java has a monitor associated with it. Java 的每个对象都有一个与之关联的监视器。 A monitor is a construct that is essentially equivalent to a reentrant lock; 监视器是一种基本上等同于重入锁的构造; the same thread can enter a monitor multiple times, and the number of entries is counted and matched against the exits. 同一个线程可以多次进入监视器,并计算条目数并与出口匹配。

Your conclusions about "active" and "passive" objects don't make much sense. 你对“主动”和“被动”物体的结论没有多大意义。 (I'm not saying they're wrong ; I'm saying I can't understand what you're trying to say.) (我不是说他们错了 ;我说我无法理解你想说的话。)

The semantics of monitors in the JVM is well-defined, but the implementation is not, and there are a number of ways that the monitor could be implemented. JVM中监视器的语义是明确定义的,但实现不是,并且可以通过多种方式实现监视器。 All of the items in your a/b/c are kept track of by the JVM in some way that the JVM author decides. 您的a / b / c中的所有项目都由JVM以JVM作者决定的某种方式跟踪。 Since most objects' monitors are never used, one strategy would be to have a table of monitors, separate from the heap, that contained a struct with the object's JVM ID, the thread currently inside the monitor, and the entry count. 由于大多数对象的监视器从未被使用过,因此一种策略是拥有一个与堆分离的监视器表,其中包含一个带有对象的JVM ID的struct ,当前位于监视器内的线程以及条目计数。

  1. In Java parlance, people tend to say that each object has a monitor. 用Java的说法,人们倾向于说每个对象都有一个监视器。 The statement synchronized( anObject ) means acquire anObject's monitor . synchronized( anObject )语句synchronized( anObject )表示acquire anObject's monitor

  2. An object doesn't "know" about the threads that currently access it. 对象不“知道”当前访问它的线程。 At a point in time, an object might be active in multiple threads. 在某个时间点,对象可能在多个线程中处于活动状态。

    From the language point of view , there is no way to list the threads that currently use an object (a), nor to list the threads that wait on it (b and c). 语言的角度来看 ,没有办法列出当前使用对象(a)的线程,也没有列出等待它的线程(b和c)。

    From the JVM point of view , the JVM must internally be able to do b and c, but not really a. JVM的角度来看 ,JVM必须在内部能够执行b和c,但实际上并非如此。

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

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