Java Singleton vs static - 是否有真正的性能优势? [英] Java Singleton vs static - is there a real performance benefit?

查看:135
本文介绍了Java Singleton vs static - 是否有真正的性能优势?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在合并一个CVS分支,其中一个较大的变化是替代,它发生在具有静态初始化块和所有静态方法的抽象类的Singleton模式中。


$ b $这是值得保留的东西,因为它需要合并很多的冲突,我将会看到什么样的情况,这个重构是值得的?



我们正在Weblogic 8.1下运行这个程序(所以JDK 1.4.2)






对不起托马斯,让我澄清一下..



HEAD版本具有传统的单例模式(私有构造函数getInstance()等)



分支版本没有构造函数,是一个公共抽象类,并将对象上的所有方法修改为static。用于存在于私有构造函数中的代码被移动到一个静态块中。



然后,该类的所有用法都被更改,从而导致合并中的多个冲突。

解决方案

从严格的运行时性能观点,差异真的可以忽略不计。两者之间的主要区别在于静态生命周期与类加载器相关联,而对于单例,它是一个常规的实例生命周期。通常最好远离ClassLoader业务,避免一些棘手的问题,特别是当您尝试重新加载Web应用程序时。


I am merging a CVS branch and one of the larger changes is the replacement wherever it occurs of a Singleton pattern with abstract classes that have a static initialisation block and all static methods.

Is this something that's worth keeping since it will require merging a lot of conflicts, what sort of situation would I be looking at for this refactoring to be worthwhile?

We are running this app under Weblogic 8.1 (so JDK 1.4.2)


sorry Thomas, let me clarify..

the HEAD version has the traditional singleton pattern (private constructor, getInstance() etc)

the branch version has no constructor, is a 'public abstract class' and modified all the methods on the object to be 'static'. The code that used to exist in the private constructor is moved into a static block.

Then all usages of the class are changed which causes multiple conflicts in the merge.

There are a few cases where this change was made.

解决方案

From a strict runtime performance point of view, the difference is really negligible. The main difference between the two lies down in the fact that the "static" lifecycle is linked to the classloader, whereas for the singleton it's a regular instance lifecycle. Usually it's better to stay away from the ClassLoader business, you avoid some tricky problems, especially when you try to reload the web application.

这篇关于Java Singleton vs static - 是否有真正的性能优势?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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