• Post Reply Bookmark Topic Watch Topic
  • New Topic

Communicating between 2 VM instances  RSS feed

 
Pullox Troy
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello...,
i wrote 2 class (it's simple thread). <include bellow>
when i start
in first command prompt : c:\>java ServiceCaller start and
in second command prompt : c:\>java ServiceCaller status
i got the status is Stopped...
i think,
there are 2 instances of ServiceCaller that too across 2 VM
instances. all i want just: the 2nd instance s'd've an access to the "engine" of the first.
But I don't know how i can communicate between 2 VM instances in this case.
so when i call the status,
the program check the running thread and return Running...
regards,
Pullox
--------------------------------------------
import java.io.*;
public class ServiceCaller {
private static Service service = new Service();
private static Thread engine = new Thread(service);
public static void main(String[] args) {
String command = args[0];
if(command.equalsIgnoreCase("start")) {
engine.start();
}
if(command.equalsIgnoreCase("stop")) {
service.sendStopSignal();
}
if(command.equalsIgnoreCase("status")) {
if(engine.isAlive()) {
System.out.println("Running...");
} else {
System.out.println("Stopped...");
}
}
}
}
--------------------------------------------------
import java.io.*;
public class Service implements Runnable {
private static boolean stopSignal = false;
public Service() {}
public static void sendStopSignal() {
stopSignal = true;
}
public void run() {
while(true) {
System.out.println(Thread.currentThread().getName());
for(int i = 1; i <= 10; i++) {
System.out.println("Counter : " + i);
try {
Thread.sleep(500);
} catch(InterruptedException ie) {
}
}
if(stopSignal) {
break;
}
try {
Thread.sleep(1000);
} catch(InterruptedException ie) {
}
} // end while
System.out.println("Thread stopped...");
} // end of run()
} // end of Service
 
Jim Baiter
Ranch Hand
Posts: 532
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is not really trivial to communicate between VM's
especially with concurrency. You could use a file or database
and have one thread write to this file/db and the other read
from it. Of course, you would need to synchronize access
to the file. Maybe others have better ideas for this.
Another option would be to have your main prompt the user for
commands and just start different threads for each command. This
way everything runs within one VM.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!