简体   繁体   English

如何通过两个不同的Java进程创建类的单个实例

[英]How to create single instance of a class by two different java processes

I have a class(MapLoader) which loads a map. 我有一个类(MapLoader)加载地图。

public class MapLoader{
statci Map aMap;
static{
 //does some processing to load this map
}

This map is being used by two different jobs which runs parallely. 该图由两个并行运行的不同作业使用。 These jobs are unix batch jobs which calls two different jar files. 这些作业是unix批处理作业,它调用两个不同的jar文件。

Jar 1:
public class ABC{
public static void main(String args[]){
//uses MapLoader.aMap

}

Jar 2:
public class XYZ{
public static void main(String args[]){
//uses MapLoader.aMap

}

These jar files uses that map of class MapLoader. 这些jar文件使用MapLoader类的映射。

Is there any way only one instance of MapLoader is created by both processes . 有没有办法通过两个进程仅创建一个MapLoader实例 Kindly suggest. 请提示。

**Kindly ignore java syntax if any, I just wrote the code for explaining my issue. **请忽略Java语法(如果有的话),我只是编写了解释我的问题的代码。

makeyour MapLoader as singleton class : 将您的MapLoader作为单例类:

    private static MapLoader ml;
    public static MapLoader create ()

       {
           if (ml==null)
           {
              ml=new MapLoader ();
           }
           return (ml);
       }

create one new class where you create one instance of MapLoader : 创建一个新类,在其中创建一个MapLoader实例:

    class OneInstance
       {
         public static returnOneInstance()
           {
               MapLoader mapLoader = MapLoader.create();
           };

Now, extend OneInstance class in your ABC and XYZ class, then it should solve your purpose.. 现在,在ABC和XYZ类中扩展OneInstance类,然后它应该可以解决您的目的。

This is going to be way harder than you'd like! 这将比您想要的要难!

If you're wanting to share objects in this manner then the best approach is to use threads rather than processes. 如果您希望以这种方式共享对象,那么最好的方法是使用线程而不是进程。 Getting a Java based scheduler such as Quartz to do the scheduling amongst threads will be easier than getting IPC working between two Java processes. 与使IPC在两个Java进程之间工作相比,让Quartz这样的基于Java的调度程序在线程之间进行调度要容易得多。

If you must use two different processes then you are going to have to do IPC. 如果必须使用两个不同的过程,则必须进行IPC。 Two Java processes can not share memory (excluding some JNI corner cases). 两个Java进程不能共享内存(某些JNI极端情况除外)。 That leaves you with approaches such as RMI, named pipes, shared files, sockets... 这给您留下了诸如RMI,命名管道,共享文件,套接字等方法。

This map is being used by two different jobs which runs parallely 该图由两个并行运行的不同作业使用

...by both processes ... 通过这两个过程

You're using "process" in the OS sense, right? 您是从操作系统的角度使用“进程”,对吗? No, you can't share objects between JVM instances like that. 不,您不能像这样在JVM实例之间共享对象。

This sounds like a shared cache, so you might be better off using Memcached and having the first process to load "lock," then initialize it. 这听起来像是共享缓存,所以最好使用Memcached并让第一个进程加载“锁”,然后对其进行初始化。

One pattern for concurrent initialisation is the enum. 并发初始化的一种模式是枚举。 The other is an inner class (as that is guaranteed to load the inner class on first usage). 另一个是内部类(因为可以保证在首次使用时加载内部类)。

The usage will have to be adapted, as no longer a static field can be used. 由于不再可以使用静态字段,因此必须调整用法。

public enum MapLoader {

     INSTANCE;

     public Map aMap;
     {
         //does some processing to load this map
         aMap = new ConcurrentHashMap();
     }
}

volatile aMap = MapLoader.INSTANCE.aMap;

This creates a singleton. 这将创建一个单例。 I personally generally pass the shared instance to every thread in their constructor, or rely on some container (as in a web application) to provide a singleton. 我个人通常将共享实例传递给其构造函数中的每个线程,或依靠某个容器(如Web应用程序)来提供单例。

Having the field volatile in the thread ensures that it is not only copied into the thread but on every access updated. 具有现场volatile的线程保证它不仅复制到线程,但在每次访问更新。

A ConcurrentHashMap ensures thread-safe modification. ConcurrentHashMap确保线程安全的修改。

You can do this by doing your MapLoader as singleton class 您可以通过将MapLoader作为单例类来完成此操作

public class MapLoader {

    private static MapLoader map;



    public static MapLoader getInstance(){
        if(map== null){
            map= new MapLoader ();
        }
        return map;
    }
}

also visit this link 也访问此链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM