开发者

Singletons in Multiple Classloader and VM cases

开发者 https://www.devze.com 2023-03-23 23:43 出处:网络
How to safely code Singletons in case of two or more Classloaders and VMs. I see this problem explained at: http://java.sun.com/developer/technicalArticles/Programming/singletons/

How to safely code Singletons in case of two or more Classloaders and VMs. I see this problem explained at: http://java.sun.com/developer/technicalArticles/Programming/singletons/

Solutions开发者_JAVA技巧 ?


If you want share a singleton between ClassLoaders, you have a common parent load the singleton, or you can have each ClassLoader explicitly define which classes Singleton you want that Classloader to use.


The article clearly states that distributed applications should stay away from Singletons. Any particular problem you are trying to solve here? Also, for singletons which hold true constants which are never modified during the application run, multiple "instances" of your "singleton" shouldn't pose a problem. In other cases, you are better off injecting your configuration (the dependency injection way of doing things) in the classes which need it.


Attention: if the computer has multiple interfaces (IPs) then the same socket (8765) can be bound to all of them (so it is not that foolproof). File is better although there are cases when they can be hidden too (mount over, for example)


1) network socket survives multiple VMs

    Port busy 8765 java.net.BindException: Address already in use

2) so does file

    WARNING: Creating a new instance of CacheManager using the diskStorePath

safely code Singletons in case of two or more Classloaders and VMs based on these resources (if you really badly need to stick with this anti-pattern)

0

精彩评论

暂无评论...
验证码 换一张
取 消