是正确的说,监视器或锁可以拥有吗? [英] Is it correct to say a monitor or lock can be owned?

查看:143
本文介绍了是正确的说,监视器或锁可以拥有吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我相信我已经看到表达式拥有一个显示器和拥有一个锁。我想验证只有一个显示器可以拥有。而且锁是获得的,不拥有。如果这是错误的,我会感谢在Java多线程的上下文中拥有和获取的正确用法。

I believe I've seen the expressions "own a monitor", and "own a lock". I'd like to verify that only a monitor can be "owned". And that a lock is "acquired", not owned. If that's wrong, I'd appreciate the correct usage of "own" and "acquire", in the context of Java multithreading.

推荐答案

p> 锁是一种数据,它在逻辑上是堆内存上对象头的一部分。 JVM中的每个对象都有这个锁(或互斥体),任何程序都可以使用它来协调多线程访问对象。如果任何线程想要访问该对象的实例变量;那么线程必须拥有对象的锁(在锁定内存区域中设置一些标志)。尝试访问对象变量的所有其他线程必须等待拥有线程释放对象的锁定(取消设置标志)。

A lock is kind of data which is logically part of an object’s header on the heap memory. Each object in a JVM has this lock (or mutex) that any program can use to coordinate multi-threaded access to the object. If any thread want to access instance variables of that object; then thread must "own" the object’s lock (set some flag in lock memory area). All other threads that attempt to access the object’s variables have to wait until the owning thread releases the object’s lock (unset the flag).

一旦线程拥有一个锁,它可以多次请求同一个锁,但必须释放锁定相同的次数它对其他线程可用。例如,如果线程请求三次,则该线程将继续拥有该锁,直到它释放三次。

Once a thread owns a lock, it can request the same lock again multiple times, but then has to release the lock the same number of times before it is made available to other threads. If a thread requests a lock three times, for example, that thread will continue to own the lock until it has "released" it three times.

监视是一种同步结构,允许线程具有互斥(使用锁)和协作,即使线程等待某些条件为真的能力等待集)。

Monitor is a synchronization construct that allows threads to have both mutual exclusion (using locks) and cooperation i.e. the ability to make threads wait for certain condition to be true (using wait-set).

换句话说,与实现锁的数据一起,每个Java对象在逻辑上与实现等待集的数据相关联。尽管锁帮助线程在共享数据上独立工作而不彼此干扰,但是等待设置帮助线程彼此协作以一起朝向共同目标而工作。所有等待的线程将被移动到此等待集合,并且一旦锁定被释放,所有等待线程将被通知。此等待集有助于通过额外的锁定(mutex)帮助构建监视器。

In other words, along with data that implements a lock, every Java object is logically associated with data that implements a wait-set. Whereas locks help threads to work independently on shared data without interfering with one another, wait-sets help threads to cooperate with one another to work together towards a common goal e.g. all waiting threads will be moved to this wait-set and all will be notified once lock is released. This wait-set helps in building monitors with additional help of lock (mutex).

这篇关于是正确的说,监视器或锁可以拥有吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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