Java 中的监视器是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3362303/
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 a monitor in Java?
提问by xdevel2000
What's a monitor referred to in concurrent programming in Java?
Java 并发编程中所指的监视器是什么?
When I read that "every object has associated a monitor" what does it meaning?
当我读到“每个对象都关联了一个监视器”是什么意思?
Is it a special object?
是特殊物品吗?
采纳答案by Pablo Santa Cruz
A monitor is mechanism to control concurrent access to an object.
监视器是控制对对象的并发访问的机制。
This allows you to do:
这允许您执行以下操作:
Thread 1:
主题 1:
public void a()
{
synchronized(someObject) {
// do something (1)
}
}
Thread 2:
主题 2:
public void b()
{
synchronized(someObject) {
// do something else (2)
}
}
This prevents Threads 1 and 2 accessing the monitored (synchronized) section at the same time. One will start, and monitor will prevent the other from accessing the region before the first one finishes.
这可以防止线程 1 和 2 同时访问受监控(同步)部分。一个将启动,并且监视器将阻止另一个在第一个完成之前访问该区域。
It's not a special object. It's synchronization mechanism placed at class hierarchy root: java.lang.Object
.
它不是一个特殊的对象。它的同步机制位于类层次结构根:java.lang.Object
.
There are also wait
and notify
methods that will also use object's monitor to communication among different threads.
也有wait
和notify
,也将使用对象的监视器通信不同的线程之间的方法。
回答by naikus
http://java.sun.com/docs/books/jvms/second_edition/html/Concepts.doc.html#33308
http://java.sun.com/docs/books/jvms/second_edition/html/Concepts.doc.html#33308
A mechanismto control access to objects one at a time
一种控制一次一个对象访问的机制
回答by JRL
A monitor is an entity that possesses both a lockand a wait set. In Java, any Object
can serve as a monitor.
监视器是一个拥有锁和等待集的实体。在 Java 中,anyObject
可以用作监视器。
For a detailed explanation of how monitors work in Java, I recommend reading the Monitor Mechanicssection of Concurrent Programming in Java(the preceding link displays the preview in Google books, and that section is available for reading).
有关 Java 中监视器如何工作的详细说明,我建议阅读 Java 中的并发编程的监视器机制部分(前面的链接显示了 Google 图书中的预览,该部分可供阅读)。
回答by mgibson
- A monitor is a concept/mechanism that's not limited to the Java Language;
- "In concurrent programming, a monitor is an object or module intended to be used safely by more than one thread";
- As every reader knows, every object in Java is a sub-class of java.lang.Object. The java folks made java.lang.Object in such a way that it has features and characteristics that enables Java programmers to use any object as a monitor. For example, every object has a wait queue,a re-entrance queue and wait and notify methods making it a monitor;
- read about monitors here.
回答by mgibson
The Java language and runtime system support thread synchronization through the use of monitors.
A monitor is associated with a specific data item (a condition variable) and functions as a lock on that data. When a thread holds the monitor for some data item, other threads are locked out and cannot inspect or modify the data.
Java 语言和运行时系统通过使用监视器来支持线程同步。
监视器与特定数据项(条件变量)相关联,并用作该数据的锁。当一个线程持有某些数据项的监视器时,其他线程被锁定并且无法检查或修改数据。
回答by Amit Rai
http://journals.ecs.soton.ac.uk/java/tutorial/java/threads/monitors.html
http://journals.ecs.soton.ac.uk/java/tutorial/java/threads/monitors.html
monitor is associate with object or data member, which is acquire when a data member or object is enter is synchronization block(critical section) and release when is exit.
监视器与对象或数据成员相关联,数据成员或对象进入同步块(临界区)时获取,退出时释放。
回答by Swati Gour
Monitor is a synchronization construct that allows threads to have both mutual exclusion and the ability to wait (block) for a certain condition to become true.
Monitor 是一种同步构造,它允许线程具有互斥和等待(阻塞)特定条件变为真的能力。
Monitors also have a mechanism for signaling other threads that their condition has been met. It is an entity that possesses both a lock and a wait set. In Java, any Object can serve as a monitor.
监视器也有一个机制来通知其他线程他们的条件已经满足。它是一个同时拥有锁和等待集的实体。在 Java 中,任何对象都可以充当监视器。
In the Java virtual machine, every object and class is logically associated with a monitor. To implement the mutual exclusion capability of monitors, a lock (sometimes called a mutex) is associated with each object and class. This is called a semaphore in operating systems terms, mutex is a binary semaphore.
在 Java 虚拟机中,每个对象和类在逻辑上都与一个监视器相关联。为了实现监视器的互斥能力,每个对象和类都关联了一个锁(有时称为互斥锁)。这在操作系统术语中称为信号量,互斥量是二进制信号量。