监视器和监视器之间的区别 锁?
What's the difference between a monitor and a lock?
If a lock is simply an implementation of mutual exclusion, then is a monitor simply a way of making use of the waiting time inbetween method executions?
A good explanation would be really helpful thanks....
regards
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
例如,在 C# .NET 中,锁定语句相当于:
但是,请记住,Monitor 还可以
Wait()
和Pulse()
,这在复杂的情况下通常很有用。多线程情况。编辑:
在.NET框架的更高版本中,这被更改为:
For example in C# .NET a lock statement is equivalent to:
However, keep in mind that Monitor can also
Wait()
andPulse()
, which are often useful in complex multithreading situations.Edit:
In later versions of the .NET framework, this was changed to:
他们是相关的。 例如,在 C# 中,lock 语句是一个简单的 try-finally围绕输入监视器和完成后退出。
They're related. For example, in C# the lock statement is a simple try-finally wrapper around entering a Monitor and exiting one when done.
监视器是编译器辅助的“半自动”锁。 它们允许在类上声明同步方法等。这只是提供互斥的一种不同方法。 我发现这本书是对这些概念最彻底的解释,尽管它主要面向操作系统开发人员。
Monitors are compiler-assisted "semi-automatic" locks. They allow one to declare
synchronized
methods on classes, etc. This is just a different approach to providing mutual exclusion. I found this book to be the most thorough explanation of the concepts, even though it's mostly geared towards OS developers.锁确保互斥。
监视器将要保护的数据与保护数据访问所需的互斥和同步原语关联起来。
例如,当您需要一个线程等待事件发生(例如,等待另一个线程将一项放入队列)时,就会使用同步。
A lock ensures mutual exclusion.
A monitor associates the data to be protected and the mutual exclusion and synchronization primitives required to protect accesses to the data.
Synchronization is used e.g. when you need one thread to wait until an event occurs (e.g., wait until another thread places an item in a queue).
监视器是一种编程语言结构,其功能与半量/锁相同,但监视器通过在运行时同步来控制共享数据。 相比之下,锁仅通过“旋转”来保护共享数据,这可能会导致 CPU 利用率较低。
Monitors is a programming-language construct that does the same thing as semiphores/locks, but Monitors control the shared data by synchronizing at run time. In contrast, locks protect the shared data by just "spinning" which can lead to poor CPU utilization.
没有区别,lock 在 try/finally 块内生成 Monitor.Enter 和 Monitor.Exit。 使用 Monitor over lock 可以让您进行微调,因为它具有 Pulse 和 PulseAll。 如果您无法使用 TryEnter 获取锁,您还可以进行替代处理。
There is no difference, lock generates Monitor.Enter and Monitor.Exit within a try/finally block. Using Monitor over lock allows you to fine tune because it has Pulse and PulseAll. You can also have alternate processing should you be unable to acquire the lock with TryEnter.
Monitor是概念,Lock是实际实现。
Monitor is the concept and Lock is the actual implementation.
据我到目前为止的研究,监视器是一组线程同步的原理,而锁以及“线程协作”设施(例如等待和通知)是Java中监视器的实现方式。 因此,如果我们试图在这两个概念之间形成确切的关系,那么锁是监视器实现的一部分(另一个是等待和通知机制)。 如果我错了,请纠正我,但如果纠正非常具体,我将不胜感激。
As far as I have researched so far, monitor is a set of principles for thread synchronization, while locks are, along with "thread cooperation" facilities like wait and notify, the way monitors are implemented in Java. So effectively, if we try to form the exact relationship between the two notions, locks are one part of the implementation of monitors (the other being wait and notify mechanisms). Please correct me if I'm wrong, but I would really appreciate if the correction is very specific.
将焦点锁定在相互排斥上,但是
Monitor 自动提供互斥。
所以我们不需要担心在Monitor中使用互斥。
我们只需要在编程时关注同步,而不是 ME。
Moniter提供了更加系统化的编程方式。
因此,它是更先进的一种。
Lock focus on only mutual exculsion, but
Moniter provides mutual exclusion automatically.
So we don't need to worry of using mutual exclusion in Monitor.
Instead of ME, we need to consern of sycronzing only when we do programming.
Moniter provides more systematical way of programming.
It, therefor, is more advanced one.