JVM是否为每个对象创建一个互斥体,以实现'synchronized'关键字?如果不是,怎么样? [英] Does the JVM create a mutex for every object in order to implement the 'synchronized' keyword? If not, how?

查看:147
本文介绍了JVM是否为每个对象创建一个互斥体,以实现'synchronized'关键字?如果不是,怎么样?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为一个更熟悉Java的C ++程序员,我看到语言级支持锁定任意对象,没有任何类型的声明支持这种锁定是有点奇怪。为每个对象创建互斥体似乎是一个沉重的成本自动选择加入。除了内存使用,互斥体是一些操作系统有限的资源在一些平台上。如果互斥体不可用,则可以自旋锁,但是其性能特征明显不同,我预计会损害可预测性。

As a C++ programmer becoming more familiar with Java, it's a little odd to me to see language level support for locking on arbitrary objects without any kind of declaration that the object supports such locking. Creating mutexes for every object seems like a heavy cost to be automatically opted into. Besides memory usage, mutexes are an OS limited resource on some platforms. You could spin lock if mutexes aren't available but the performance characteristics of that are significantly different, which I would expect to hurt predictability.

JVM是否足够聪明case以识别特定的对象永远不会是synchronized关键字的目标,从而避免创建互斥?互斥体可以懒惰地创建,但是这提出了一个引导问题,它本身需要一个互斥体,即使这样做,我认为仍然有一些开销跟踪是否已经创建了互斥体。所以我假设这样的优化是可能的,它必须在编译时或启动时完成。在C ++中,这样的优化是不可能的,由于编译模型(你不知道一个对象的锁是否将跨库使用),但我不知道Java的编译和链接知道

Is the JVM smart enough in all cases to recognize that a particular object will never be the target of the synchronized keyword and thus avoid creating the mutex? The mutexes could be created lazily, but that poses a bootstrapping problem that itself necessitates a mutex, and even if that were worked around I assume there's still going to be some overhead for tracking whether a mutex has already been created or not. So I assume if such an optimization is possible, it must be done at compile time or startup. In C++ such an optimization would not be possible due to the compilation model (you couldn't know if the lock for an object was going to be used across library boundaries), but I don't know enough about Java's compilation and linking to know if the same limitations apply.

推荐答案

作为一个人,看看一些JVM实现锁定的方式...

Speaking as someone who has looked at the way that some JVMs implement locks ...

正常的做法是从对象的标题字中用几个保留位开始。如果对象从未被锁定,或者如果它被锁定,但没有争用,它保持这种方式。如果在锁定的对象上发生争用,JVM会将充斥到完全成熟的互斥数据结构中,并在对象的生命周期中保持这种状态。

The normal approach is to start out with a couple of reserved bits in the object's header word. If the object is never locked, or if it is locked but there is no contention it stays that way. If and when contention occurs on a locked object, the JVM inflates the lock into a full-blown mutex data structure, and it stays that way for the lifetime of the object.

编辑 - 我只是注意到OP正在谈论操作系统支持的互斥。在我看过的例子中,使用CAS指令等直接实现膨胀的互斥体,而不是使用pthread库函数等。

EDIT - I just noticed that the OP was talking about OS-supported mutexes. In the examples that I've looked at, the inflated mutexes were implemented directly using CAS instructions and the like, rather than using pthread library functions, etc.

这篇关于JVM是否为每个对象创建一个互斥体,以实现'synchronized'关键字?如果不是,怎么样?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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