gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 10:26:01 25 4
gpt4 key购买 nike

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 ,似乎它是一个包含等待集的对象?

我看过类似的问题What's a monitor in Java?在 stackoverflow 上,但答案并不那么明确。

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:

  • 中立:解锁

  • 有偏差:锁定/解锁+非共享

  • Stack-Locked:锁定+共享但无争用 该标记指向所有者线程堆栈上已移位的标记字。

  • 膨胀:锁定/解锁+共享和竞争线程被阻止在monitorenter或wait()中。标记指向重量级“objectmonitor”结构。[8]

我猜如果 monitorobjectmonitor结构?但是objectmonitor最初不创建,仅在由于争用而使用重量级锁时使用。

最佳答案

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

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

关于java - JVM(Hotspot)中的 `monitor`是什么,一个特定的对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53953344/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com