Search code examples
javaprocessbuildermultiple-instances

How to create two instances of same java class running in same process?


I am learning Java Process and I am trying to create two instances of same java class running in same process which is a requirement.

class Chat {
    public void getMessage() { * * * some implementation
    }
}

class ProcessMain {
     public static void main(String args[]) {
         Chat c1 = new Chat();
         Chat c2 = new Chat();
         ProcessBuilder pb = new ProcessBuilder(c1); * * * * here is where I am stuck.
         Two instances of same class should run in same process
     }
 }

Can anyone give me a lead?


Solution

  • By default Java will run the main class in a single process, on the same thread. If you want to have two classes communicate with one another you can pass c1 into a method inside of c2 and alter c1 that way.