为什么所有Java对象都有wait()和notify(),这是否会导致性能下降? [英] Why do all Java Objects have wait() and notify() and does this cause a performance hit?

查看:457
本文介绍了为什么所有Java对象都有wait()和notify(),这是否会导致性能下降?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每个Java 对象都有方法 wait() notify()(和其他变种)。我从来没有使用过这些,我怀疑其他人没有。为什么这些如此基本以至于每个对象都必须拥有它们并且在使用它们时会有性能损失(可能是某些状态存储在它们中)?

Every Java Object has the methods wait() and notify() (and additional variants). I have never used these and I suspect many others haven't. Why are these so fundamental that every object has to have them and is there a performance hit in having them (presumably some state is stored in them)?

编辑以强调这个问题。如果我有一个带有100,000个元素的列表< Double> ,那么每个 Double 都有这些方法,因为它是从<$扩展的C $ C>对象。但似乎所有这些都不太可能知道管理 List 的线程。

EDIT to emphasize the question. If I have a List<Double> with 100,000 elements then every Double has these methods as it is extended from Object. But it seems unlikely that all of these have to know about the threads that manage the List.

编辑优秀而有用的答案。 @Jon有一篇非常好的博文,结合了我的直觉。我也完全赞同@Bob_Cross你应该在担心之前表现出性能问题。 (同样作为成功语言的第n定律,如果它已经被性能击中,那么Sun或某人会修复它)。

EDIT excellent and useful answers. @Jon has a very good blog post which crystallised my gut feelings. I also agree completely with @Bob_Cross that you should show a performance problem before worrying about it. (Also as the nth law of successful languages if it had been a performance hit then Sun or someone would have fixed it).

推荐答案

嗯,这确实意味着每个对象都必须可能有一个与之关联的监视器。同一个监视器用于 synchronized 。如果您同意能够同步任何对象的决定,那么 wait() notify() don不再添加每个对象状态。 JVM可以懒惰地分配实际的监视器(我知道.NET会这样做)但是必须有一些存储空间可以说明哪个监视器与该对象相关联。不可否认,这可能是一个非常小的数量(例如3个字节),由于填充剩余的对象开销而无法实际保存任何内存 - 您必须查看每个单独的JVM如何处理内存来说肯定。

Well, it does mean that every object has to potentially have a monitor associated with it. The same monitor is used for synchronized. If you agree with the decision to be able to synchronize on any object, then wait() and notify() don't add any more per-object state. The JVM may allocate the actual monitor lazily (I know .NET does) but there has to be some storage space available to say which monitor is associated with the object. Admittedly it's possible that this is a very small amount (e.g. 3 bytes) which wouldn't actually save any memory anyway due to padding of the rest of the object overhead - you'd have to look at how each individual JVM handled memory to say for sure.

请注意,只有额外的方法不会影响性能(除了非常轻微,因为代码明显存在某处)。它不像每个对象,甚至每个类型都有自己的代码副本 wait() notify() 。根据vtable的工作方式,每个类型可能最终为每个继承的方法添加一个额外的vtable条目 - 但这仍然只是基于每个类型,而不是基于每个对象。与实际对象本身的大部分存储相比,这基本上会在噪音中迷失。

Note that just having extra methods doesn't affect performance (other than very slightly due to the code obvious being present somewhere). It's not like each object or even each type has its own copy of the code for wait() and notify(). Depending on how the vtables work, each type may end up with an extra vtable entry for each inherited method - but that's still only on a per type basis, not a per object basis. That's basically going to get lost in the noise compared with the bulk of the storage which is for the actual objects themselves.

就我个人而言,我觉得.NET和Java都是通过将监视器与每个对象相关联的错误 - 我宁愿使用显式同步对象。我在博文中写了更多内容关于重新设计java.lang.Object / System.Object

Personally, I feel that both .NET and Java made a mistake by associating a monitor with every object - I'd rather have explicit synchronization objects instead. I wrote a bit more on this in a blog post about redesigning java.lang.Object/System.Object.

这篇关于为什么所有Java对象都有wait()和notify(),这是否会导致性能下降?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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