JVM(Hotspot)中的`monitor`是什么,一个特定的对象?

What is a `monitor` in JVM(Hotspot), a specific object?

Java Language Spec, Section 17.1: Synchronization中表示

Each object in Java is associated with a monitor, which a thread can lock or unlock.

第 17.2 节:

Every object, in addition to having an associated monitor, has an associated wait set. A wait set is a set of threads.

When an object is first created, its wait set is empty. Elementary actions that add threads to and remove threads from wait sets are atomic. Wait sets are manipulated solely through the methods Object.wait, Object.notify, and Object.notifyAll.

这里有一个问题是,什么是monitor,好像是一个包含等待集的对象?

我在Whosebug上看过一个类似的问题What's a monitor in Java?,但答案不是很清楚。

A monitor is mechanism to control concurrent access to an object.

A monitor is an entity that possesses both a lock and a wait set. In Java, any Object can serve as a monitor.

我在 Hotspot runtime overview

上获得了更多信息

Per-object synchronization state is encoded in the first word (the so-called mark word) of the VM's object representation. For several states, the mark word is multiplexed to point to additional synchronization metadata. (As an aside, in addition, the mark word is also multiplexed to contain GC age data, and the object's identity hashCode value.) The states are:

我猜 monitor 是不是 objectmonitor 结构?但是objectmonitor一开始并没有创建,只有在因为争用使用重量级锁时才使用。

监视器是一个概念,您可以在其上执行某些操作。任何实现监视器概念的抽象操作的东西都是一个很好的实现。

这个概念在 HotSpot 中的标记词以及您引用的有关标记词的文本中描述的所有内容中实现。它不是一个单一的数据结构。