本文介绍了什么是 JVM(热点)中的“监视器",一个特定的对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Java 语言规范中,第 17.1 节:同步,它说

Java 中的每个对象都与一个监视器相关联,线程可以锁定或解锁监视器.

第 17.2 节:

每个对象,除了有一个关联的监视器之外,还有一个关联的等待集.等待集是一组线程.

当一个对象第一次被创建时,它的等待集是空的.向等待集中添加线程和从等待集中删除线程的基本操作是原子的.等待集仅通过 Object.waitObject.notifyObject.notifyAll 方法操作.

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,好像是一个包含wait set的对象?

A question here is, what is monitor, seems it is an object which contain a wait set?

我查看了一个类似的问题 Java 中的监视器是什么?在 stackoverflow 上,但答案不是那么清楚.

I have take a look at a similar question What's a monitor in Java? on stackoverflow, but the answers were not so clearly.

监视器是控制对对象的并发访问的机制.

监视器是一个拥有锁和等待集的实体.在 Java 中,任何对象都可以作为监视器.

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

我获得了有关 Hotspot 运行时概览的更多信息

I got some more info on Hotspot runtime overview

每个对象的同步状态编码在 VM 对象表示的第一个字(所谓的标记字)中.对于几个状态,标记词被复用以指向额外的同步元数据.(顺便说一句,另外,mark word也被复用为包含GC年龄数据,以及对象的身份hashCode值.)状态为:

  • 中立:解锁

    • Neutral: Unlocked

      偏向:锁定/解锁+非共享

      Biased: Locked/Unlocked + Unshared

      Stack-Locked: Locked + Shared but uncontended 标记指向所有者线程堆栈上的置换标记字.

      Stack-Locked: Locked + Shared but uncontended The mark points todisplaced mark word on the owner thread's stack.

      Inflated:锁定/解锁+共享和竞争线程被阻塞在 monitorenter 或 wait() 中.标记指向重量级对象监视器"结构.[8]

      Inflated: Locked/Unlocked + Shared and contended Threads are blockedin monitorenter or wait(). The mark points to heavy-weight"objectmonitor" structure.[8]

      我猜 monitor 是否是 objectmonitor 结构?但是objectmonitor一开始并没有创建,只有在争用重锁时才使用.

      I guess if a monitor is a objectmonitor structure? But objectmonitor does not created at first, only used when heavy weight lock is used due to contention.

      推荐答案

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

      A monitor is a concept on which you can perform certain operations. Anything that implements the abstract operations of the concept of a monitor is a good implementation.

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

      The concept is implemented in HotSpot in the mark word plus everything described in the text that you quoted about the mark word. It is not a single data structure.

      这篇关于什么是 JVM(热点)中的“监视器",一个特定的对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-18 06:32