简体   繁体   中英

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

I have a class(MapLoader) which loads a map.

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.

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.

Is there any way only one instance of MapLoader is created by both processes . Kindly suggest.

**Kindly ignore java syntax if any, I just wrote the code for explaining my issue.

makeyour MapLoader as singleton class :

    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 :

    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..

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.

If you must use two different processes then you are going to have to do IPC. Two Java processes can not share memory (excluding some JNI corner cases). That leaves you with approaches such as RMI, named pipes, shared files, sockets...

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.

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.

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.

Having the field volatile in the thread ensures that it is not only copied into the thread but on every access updated.

A ConcurrentHashMap ensures thread-safe modification.

You can do this by doing your MapLoader as singleton class

public class MapLoader {

    private static MapLoader map;



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

also visit this link

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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