内部锁实际上对Java类意味着什么? [英] What does intrinsic lock actually mean for a Java class?

查看:150
本文介绍了内部锁实际上对Java类意味着什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了正确理解Java中的并发问题和解决方案,我正在阅读官方Java教程。在其中一个页面中,他们定义了内部锁定和同步 链接。在这个页面中,他们说:

In order to properly understand the issues and solutions for concurrency in Java, I was going through the official Java tutorial. In one of the pages they defined Intrinsic Locks and Synchronization link. In this page, they say that:


只要一个线程拥有一个内部锁,没有其他线程可以
获得同样的锁。另一个线程会在尝试
获取锁定时阻止。

As long as a thread owns an intrinsic lock, no other thread can acquire the same lock. The other thread will block when it attempts to acquire the lock.

此外,他们在部分提到在同步方法中锁定


当线程调用synchronized方法时,它会自动获取
本机锁对于该方法的对象,并在
方法返回时释放它。即使由于未捕获的异常导致
返回,也会发生锁定释放。

When a thread invokes a synchronized method, it automatically acquires the intrinsic lock for that method's object and releases it when the method returns. The lock release occurs even if the return was caused by an uncaught exception.

对我而言,这意味着一旦我打电话一个来自其中一个线程的同步方法,我将保持线程的内在锁定,因为

For me this means that once I call a synchronized method from one of the threads, I will have hold of the intrinsic lock of the thread and since


内部锁定起作用同步的两个方面:
强制执行对象状态的独占访问,并建立
发生之前 - 对可见性至关重要的关系。

Intrinsic locks play a role in both aspects of synchronization: enforcing exclusive access to an object's state and establishing happens-before relationships that are essential to visibility.

另一个线程是否会无法调用同一个类的另一个同步方法?如果是,则使同步方法的整个目的失败。不是吗?

would another thread be unable to call another synchronized method of the same class? If yes, then the whole purpose of having synchronized methods is defeated. Isn't it?

推荐答案

所以只是重复我上面的评论作为答案。内部锁定意味着您不必创建对象来同步您的方法。相比之下,您可以通过调用 synchronized(myLock){...} 来使用外部锁定。

So just to repeat my comment above as an answer. Intrinsic locking means that you don't have to create an object to synchronize your methods on. In comparison you can use an extrinsic lock by calling synchronized(myLock) {...}.

这是 Java并发实践一书的摘录:每个对象都有一个内置锁的事实只是一个方便,所以你不需要显式创建锁定对象

This is an excerpt from the book Java concurrency in practice: "The fact that every object has a built-in lock is just a convenience so that you needn't explicitly create lock objects"

这本书还说:


对象的内部锁定
与其状态之间没有固有的关系;一个对象的字段不需要被其固有的
锁保护,尽管这是一个完全有效的锁定约定,许多类使用
。获取与对象关联的锁不会
阻止其他线程访问该对象唯一的
获取锁定阻止任何其他线程进行操作就是获取
相同的锁。每个对象都有一个内置锁的事实只是
便利,所以你不需要显式创建锁对象。 [9]
您可以自行构建锁定协议或同步
策略,以便安全地访问共享状态,并在整个程序中始终如一地使用

There is no inherent relationship between an object's intrinsic lock and its state; an object's fields need not be guarded by its intrinsic lock, though this is a perfectly valid locking convention that is used by many classes. Acquiring the lock associated with an object does not prevent other threads from accessing that objectthe only thing that acquiring a lock prevents any other thread from doing is acquiring that same lock. The fact that every object has a built-in lock is just a convenience so that you needn't explicitly create lock objects. [9] It is up to you to construct locking protocols or synchronization policies that let you access shared state safely, and to use them consistently throughout your program.

但在脚注中它说:


[9] In回想起来,这个设计决定可能是一个糟糕的决定:不是
只会令人困惑,但它迫使JVM实现者在对象大小和锁定性能之间进行
权衡。

[9] In retrospect, this design decision was probably a bad one: not only can it be confusing, but it forces JVM implementors to make tradeoffs between object size and locking performance.

并回答你的上一个问题:你将无法从另一个线程调用同步方法,但你可以继续从同一个线程进入(本机锁是重新-entrant)。所以你必须想象在这种情况下锁定来自不同调用者线程的序列化方法访问。

And to answer your last questions: you won't be able to call the synchronized methods from another thread, but you can keep entering from the same thread (intrinsic locks are re-entrant). So you have to imagine locking in this case as serializing method access from different caller threads.

如果你使用不正确的锁定然后引入活体危险,那么是的它被击败了。这就是为什么你必须确保你的并发线程不会相互竞争太多。

If you use locking improperly and then you introduce liveness hazards, then yes it is defeated. That's why you have to make sure that your concurrent threads are not contending with each other too hard.

as Brian Goetz在此博客条目中添加


在调整应用程序的同步使用时,我们应该尝试使用
来减少实际争用的数量,而不是简单地尝试
以避免同时使用同步

In tuning an application's use of synchronization, then, we should try hard to reduce the amount of actual contention, rather than simply try to avoid using synchronization at all

这篇关于内部锁实际上对Java类意味着什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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