如何在Java中创建一个跨进程的Singleton类
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了如何在Java中创建一个跨进程的Singleton类,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2250字,纯文字阅读大概需要4分钟。
内容图文
![如何在Java中创建一个跨进程的Singleton类](/upload/InfoBanner/zyjiaocheng/951/c308c03dfd544cfaab982bdfa0aac85a.jpg)
是否可以创建一个通用的Singleton类,在任何给定的时间,只有一个实例在多个Java进程之间共享?
解决方法:
多个Java进程不共享同一个虚拟机.
因此,最终会有一个托管单例的JVM实例,然后每个进程一个JVM实例使用远程方法调用访问单例,如@Little Bobby Tables所示.
无论如何考虑When is a Singleton not a Singleton:
Multiple Singletons in Two or More Virtual Machines
When copies of the Singleton class run in multiple VMs, an instance is created for each machine. That each VM can hold its own Singleton might seem obvious but, in distributed systems such as those using EJBs, Jini, and RMI, it’s not so simple. Since intermediate layers can hide the distributed technologies, to tell where an object is really instantiated may be difficult.
For example, only the EJB container decides how and when to create EJB objects or to recycle existing ones. The EJB may exist in a different VM from the code that calls it. Moreover, a single EJB can be instantiated simultaneously in several VMs. For a stateless session bean, multiple calls to what appears, to your code, to be one instance could actually be calls to different instances on different VMs. Even an entity EJB can be saved through a persistence mechanism between calls, so that you have no idea what instance answers your method calls. (The primary key that is part of the entity bean spec is needed precisely because referential identity is of no use in identifying the bean.)
The EJB containers’ ability to spread the identity of a single EJB instance across multiple VMs causes confusion if you try to write a Singleton in the context of an EJB. The instance fields of the Singleton will not be globally unique. Because several VMs are involved for what appears to be the same object, several Singleton objects might be brought into existence.
Systems based on distributed technologies such as EJB, RMI, and Jini should avoid Singletons that hold state. Singletons that do not hold state but simply control access to resources are also not appropriate for EJBs, since resource management is the role of the EJB container. However, in other distributed systems, Singleton objects that control resources may be used on the understanding that they are not unique in the distributed system, just in the particular VM.
内容总结
以上是互联网集市为您收集整理的如何在Java中创建一个跨进程的Singleton类全部内容,希望文章能够帮你解决如何在Java中创建一个跨进程的Singleton类所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。