Java中对象监视器的含义是什么?为什么用这个词? [英] What's the meaning of an object's monitor in Java? Why use this word?

查看:406
本文介绍了Java中对象监视器的含义是什么?为什么用这个词?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在阅读有关Java线程的文章时,我经常注意到这样的表达:当前线程是此对象监视器的所有者。我明白了:线程有权对对象进行操作。但我很疑惑为什么我们使用短语对象的监视器而不是对象的锁定?

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"?

简而言之,我不知道这个词的含义'monitor'
这个问题可能很奇怪而且很简单。但我希望任何人都可以帮助解决它。 3ks

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

推荐答案


但我很困惑为什么要使用对象的监视器这个词对象的锁定??

but I am puzzled why use word "the object's monitor" instend of "the object's lock"?

请参阅ulmangt的答案,了解解释在此上下文中使用的术语监视器的链接。

See ulmangt's answer for links that explain the term "monitor" as used in this context.

为什么使用术语监视器而不是锁定?严格地说,这些术语确实意味着不同的东西......特别是如果你按照它们最初打算使用它们的方式使用它们。

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.


  • lock是具有维护某些锁属性的获取和释放原语的东西;例如独占使用或单个写入器/多个读取器。

  • A "lock" is something with acquire and release primitives that maintain certain lock properties; e.g. exclusive use or single writer / multiple reader.

监视器是一种机制,可确保只有一个线程可以执行给定的部分(或部分)在任何给定时间的代码。这可以使用一个锁(和条件变量来实现,它允许线程等待或向其他线程发送通知,条件已满足),但它不仅仅是一个锁。实际上,在Java情况下,监视器使用的实际锁定不能直接访问。 (你不能说Object.lock()来阻止其他线程获取它......就像你可以使用Java Lock 实例一样。)

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是什么提供。但在实践中,这两个术语几乎可以互换使用。

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中对象监视器的含义是什么?为什么用这个词?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆