简体   繁体   English

java线程中的“Monitor”

[英]“Monitor” in java threads

I have read different things in different blogs about monitors. 我在不同的博客中读过有关显示器的不同内容。 So I'm a bit confused now. 所以我现在有点困惑。

As much as I understand, monitor is a somebody who would make sure that only one thread is executing the code in the critical section. 据我所知,monitor是一个确保只有一个线程在关键部分执行代码的人。 So is it like if we have 3 synchronized methods/blocks then we would have 3 monitors to make sure that only one thread is in the critical section? 那么如果我们有3个同步的方法/块,那么我们将有3个监视器来确保只有一个线程在临界区?

If the above is true then why it is said that in Java every object has a monitor associated with it? 如果以上是真的那么为什么说在Java中每个对象都有一个与之关联的监视器? It should be every synchronized block is associated with a monitor. 它应该是每个同步块与监视器相关联。

What is a monitor? 什么是显示器?

A monitor is something a thread can grab and hold, preventing all other threads from grabbing that same monitor and forcing them to wait until the monitor is released. 监视器是线程可以抓取并保持的东西,防止所有其他线程抓取同一个监视器并强制它们等到监视器被释放。 This is what a synchronized block does. 这就是synchronized块的功能。

Where do these monitors come from in the first place? 这些显示器首先来自哪里?

The answer is: from any Java object. 答案是:来自任何Java对象。 When you write: 当你写:

Object foo = new Object();
synchronized (foo) {
  System.out.println("Hello world.");
}

...what this means is: the current thread will first grab the monitor associated with the object stored in variable foo and hold it while it prints "Hello world", then releases it. ...这意味着:当前线程将首先获取与存储在变量foo的对象关联的监视器,并在打印“Hello world”时保持它,然后释放它。

Why does every Java object have a monitor associated with it? 为什么每个Java对象都有一个与之关联的监视器?

There is no technical reason for it to be that way. 它没有技术上的原因。 It was a design decision made in the early versions of Java and it's too late to change now (even though it is confusing at first and it does cause problems if people aren't careful). 这是在Java的早期版本中做出的设计决定,现在改变为时已晚(即使它最初很混乱,如果人们不小心它确实会引起问题)。

When using synchronized with blocks, you specify an object to lock on. 使用与块synchronized时,指定要锁定的对象。 In that case, the monitor of that object is used for locking. 在这种情况下,该对象的监视器用于锁定。

When using synchronized with methods, you don't specify an object to lock on, and instead this object is implied. 使用synchronized with methods时,不指定要锁定的对象,而是隐含this对象。 Again, the monitor of this is used for locking. 再次,在监视器this用于锁定。

So, objects have monitors, and synchronized methods/blocks do not have their own monitors, but instead they use the monitors of specific objects. 因此,对象具有监视器,并且同步的方法/块没有自己的监视器,而是使用特定对象的监视器。

In the context of Java programming, the monitor is the intrinsic lock (where intrinsic means "built-in") on a Java object. 在Java编程的上下文中,监视器是Java对象上的内部锁(其中内在意味着“内置”)。 For a thread to enter any synchronized instance method on an object it must first acquire the intrinsic lock on that object. 对于要在对象上输入任何同步实例方法的线程,它必须首先获取该对象上的内部锁。 For a thread to enter any synchronized static method on a class it must first acquire the intrinsic lock on that class. 对于要在类上输入任何同步静态方法的线程,它必须首先获取该类的内部锁。

This is how monitor is defined in the Java tutorial : 这就是Java教程中定义监视器的方式:

Synchronization is built around an internal entity known as the intrinsic lock or monitor lock. 同步是围绕称为内部锁或监视器锁的内部实体构建的。 (The API specification often refers to this entity simply as a "monitor.") (API规范通常将此实体简称为“监视器”。)

So "monitor" in Java is a synonym for "intrinsic lock". 因此,Java中的“监视器”是“内部锁定”的同义词。

There is a good reason that the monitor belongs to an object, and not to an individual block: the monitor is there to protect the state of the object. 监视器属于一个对象,而不是一个单独的块是有充分理由的:监视器用于保护对象的状态。 Objects should be designed to be cohesive , making it likely that instance variables will end up being referenced by multiple methods; 对象应该被设计为具有凝聚力 ,使得实例变量最终可能被多个方法引用; the safe thing to do, in order to guarantee that the object is always in a consistent state, is to allow only one synchronized method on that object to execute at a time. 为了保证对象始终处于一致状态,安全的做法是一次只允许该对象上的一个同步方法执行。

The term "monitor" comes from Concurrent Pascal , although the meaning of it seems significantly different. 术语“监视器”来自Concurrent Pascal ,虽然它的含义似乎有很大不同。 See Per Brinch Hansen's paper "Java's Insecure Parallelism" , which argues that Java doesn't actually implement monitors: 请参阅Per Brinch Hansen的论文“Java的不安全并行” ,该论文认为Java实际上并没有实现监视器:

Gosling (1996, p. 399) claims that Java uses monitors to synchronize threads. Gosling(1996,p.399)声称Java使用监视器来同步线程。 Unfortunately, a closer inspection reveals that Java does not support a monitor concept: 不幸的是,仔细检查发现Java不支持监视器概念:

  • Unless they are declared as synchronized, Java class methods are unsynchronized. 除非它们被声明为synchronized,否则Java类方法是不同步的。

  • Unless they are declared as private, Java class variables are public (within a package) 除非它们被声明为私有,否则Java类变量是公共的(在包中)

Another quote from the same paper: 同一篇论文的另一个引用:

The failure to give an adequate meaning to thread interaction is a very deep flaw of Java that vitiates the conceptual integrity of the monitor concept. 未能为线程交互提供足够的意义是Java的一个非常深刻的缺陷,它破坏了监视器概念的概念完整性。

I try to avoid the term "monitor", preferring "intrinsic lock" as more exact. 我试图避免使用术语“监视器”,而更喜欢“固有锁定”。 But "monitor" appears in the JLS and can't be avoided. 但是“监视器”出现在JLS中并且无法避免。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM