Java中对象监视器的含义是什么?为什么要用这个词?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9848616/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What's the meaning of an object's monitor in Java? Why use this word?
提问by jiafu
When reading articles about Java threads, I often notice the expression: "current thread is the owner of this object's monitor". I get the meaning: the thread gets the right to operate on the object. But I am puzzled why we use the phrase "the object's monitor" instead of "the object's lock"?
在阅读有关 Java 线程的文章时,我经常注意到这样的表述:“当前线程是该对象监视器的所有者”。我明白了:线程有权对对象进行操作。但我很困惑为什么我们使用“对象的监视器”而不是“对象的锁”?
In brief, I don't know the meaning of the word 'monitor' The question may be strange and simple. But I wish anybody can help to solve it. 3ks
简而言之,我不知道“监视器”这个词的含义。这个问题可能很奇怪也很简单。但我希望任何人都可以帮助解决它。3 秒
采纳答案by Stephen C
but I am puzzled why use word "the object's monitor" instend of "the object's lock"?
但我很困惑为什么在“对象的锁”中使用“对象的监视器”这个词?
See ulmangt's answer for links that explain the term "monitor" as used in this context. Note that:
有关解释在此上下文中使用的术语“监视器”的链接,请参阅 ulmangt 的回答。注意:
"Monitors were invented by Per Brinch Hansen and C. A. R. Hoare, and were first implemented in Brinch Hansen's Concurrent Pascal language."
“监视器是由 Per Brinch Hansen 和 CAR Hoare 发明的,最初是用 Brinch Hansen 的 Concurrent Pascal 语言实现的。”
(Source: Wikipedia)
(来源:维基百科)
Why use the term "monitor" rather than "lock"? Well strictly speaking, the terms do mean different things ... especially if you use them in the way that they were originally intended to be used.
为什么使用术语“监视器”而不是“锁定”?严格来说,这些术语确实意味着不同的东西……特别是如果您以它们最初打算使用的方式使用它们。
A "lock" is something with acquire and release primitives that maintain certain lock properties; e.g. exclusive use or single writer / multiple reader.
A "monitor" is a mechanism that ensures that only one thread can be executing a given section (or sections) of code at any given time. This can be implemented using a lock (and "condition variables" that allow threads to wait for or send notifications to other threads that the condition is fulfilled), but it is more than just a lock. Indeed, in the Java case, the actual lock used by a monitor is not directly accessible. (You just can't say "Object.lock()" to prevent other threads from acquiring it ... like you can with a Java
Lock
instance.)
“锁”是具有保持某些锁属性的获取和释放原语的东西;例如独占使用或单个写入器/多个读取器。
“监视器”是一种机制,可确保在任何给定时间只有一个线程可以执行给定的一段(或多段)代码。这可以使用锁(和“条件变量”,允许线程等待或向其他线程发送条件满足的通知)来实现,但它不仅仅是一个锁。事实上,在 Java 的情况下,监视器使用的实际锁是不能直接访问的。(你不能说“Object.lock()”来阻止其他线程获取它......就像你可以用Java
Lock
实例一样。)
In short, if one were to be pedantic "monitor" is actually a better term than "lock" for characterizing what Java is providing. But in practice, both terms are used almost interchangeably.
简而言之,如果要学究,“监视器”实际上是比“锁定”更好的术语,用于表征 Java 提供的内容。但实际上,这两个术语几乎可以互换使用。
回答by ulmangt
A monitor is simply a term for an object whose methods can be safely used in a multithreaded environment.
监视器只是一个对象的术语,它的方法可以在多线程环境中安全地使用。
There's a great Wikipedia article on Monitors:
有一篇关于监视器的很棒的维基百科文章:
http://en.wikipedia.org/wiki/Monitor_(synchronization)
http://en.wikipedia.org/wiki/Monitor_(同步)
If you scroll down, it's even got a section explicitly about Java.
如果你向下滚动,它甚至有一个明确的关于 Java的部分。
回答by abbas
A synchronized
block around an object
is its monitor, which controls a lock on the object. Here an example
甲synchronized
围绕块object
是它的显示器,其控制对象上的锁。这里有一个例子
synchronized (object) {
while (<condition does not hold>)
object.wait(timeout);
... // Perform action appropriate to condition
}
回答by adityalad
The Java Virtual Machine uses monitors to support multithreading. Monitors achieve this through two concepts - Mutual exclusion while running the threads (here is where 'locking' comes into picture) and coordination as a means of inter thread communication (here is where object's wait and notify methods come into picture).
Java 虚拟机使用监视器来支持多线程。监视器通过两个概念来实现这一点 - 运行线程时的互斥(这里是“锁定”出现的地方)和作为线程间通信手段的协调(这里是对象的等待和通知方法出现的地方)。
Reading the following part from "Inside JVM" will clear this doubt, is it very nicely explained over here (Chapter 20, Thread synchronization) -
阅读“Inside JVM”中的以下部分将消除这个疑问,是否在这里解释得很好(第 20 章,线程同步)-
回答by Aman
Even though it is late to answer this question, I thought to just add in-case it is useful.
Here is a synchronized block of Java code inside an unsynchronized Java method
尽管回答这个问题为时已晚,但我想添加以防万一它有用。
这是未同步 Java 方法中的同步 Java 代码块
public void add(int value){
synchronized(this){
this.count += value;
}
}
In the example "this" is used, which is the instance the add method is called on.
A synchronized instance method uses the object it belongs to as monitor object.
=> Only one thread can execute inside a Java code block synchronized on the same monitor object.
在示例中使用了“this”,它是调用 add 方法的实例。同步实例方法使用它所属的对象作为监视器对象。
=> 只有一个线程可以在同一个监视器对象上同步的 Java 代码块内执行。
回答by Zane XY
Quote from Inside the Java Virtual Machine
引用自Java 虚拟机内部
A thread in the Java virtual machine requests a lock when it arrives at the beginning of a monitor region. In Java, there are two kinds of monitor regions: synchronized statements and synchronized methods.
Java 虚拟机中的线程在到达监视区域的开头时请求锁定。在 Java 中,有两种监视区域:同步语句和同步方法。
Monitor
监视器
A monitor is like a building that contains one special room that can be occupied by only one thread at a time. The room usually contains some data. From the time a thread enters this room to the time it leaves, it has exclusive access to any data in the room. Entering the monitor building is called "entering the monitor." Entering the special room inside the building is called "acquiring the monitor." Occupying the room is called "owning the monitor," and leaving the room is called "releasing the monitor." Leaving the entire building is called "exiting the monitor."
In addition to being associated with a bit of data, a monitor is associated with one or more bits of code, which in this book will be called monitor regions.
As mentioned earlier, the language provides two built-in ways to identify monitor regions in your programs: synchronized statements and synchronized methods. These two mechanisms, which implement the mutual exclusion aspect of synchronization, are supported by the Java virtual machine's instruction set.
监视器就像一座建筑物,里面有一个特殊的房间,一次只能被一个线程占用。房间通常包含一些数据。从一个线程进入这个房间到它离开的时间,它可以独占访问房间中的任何数据。进入监控楼叫“进入监控”。进入建筑物内的特殊房间称为“获取监视器”。占据房间叫“拥有显示器”,离开房间叫“释放显示器”。离开整栋楼叫做“退出监控”。
除了与一位数据相关联之外,监视器还与一位或多位代码相关联,在本书中将其称为监视器区域。
如前所述,该语言提供了两种内置方法来标识程序中的监视区域:同步语句和同步方法。Java 虚拟机的指令集支持这两种机制,它们实现了同步的互斥方面。
Lock
锁
To implement the mutual exclusion capability of monitors, the Java virtual machine associates a lock (sometimes called a mutex) with each object and class. A lock is like a privilege that only one thread can "own" at any one time.
A single thread is allowed to lock the same object multiple times. For each object, the Java virtual machine maintains a count of the number of times the object has been locked. An unlocked object has a count of zero. When a thread acquires the lock for the first time, the count is again incremented to one. Each time the thread acquires a lock on the same object, the count is again incremented.
为了实现监视器的互斥能力,Java 虚拟机为每个对象和类关联一个锁(有时称为互斥锁)。锁就像一种特权,在任何时候都只有一个线程可以“拥有”。
单个线程可以多次锁定同一个对象。对于每个对象,Java 虚拟机维护一个对象被锁定的次数的计数。解锁对象的计数为零。当线程第一次获得锁时,计数再次增加到 1。每次线程获得同一个对象的锁时,计数再次增加。