Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Two Java program communicating..

 
Sunil Kumar Gupta
Ranch Hand
Posts: 824
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All

I have a problem related to a java code. Please help me to solve the problem..


I have a java code which is say a thread which runs continously...
say

class MyRun extends Thread{
///////definition of MyRun class

//main method here
}

when i run this program, say java MyRun
Now my file is running properly.

Now i need to run another file say

class StopMyRun {
///class definition
//main method here
}

and i will run this file as

java StopMyRun

Now these two files are independent files having their own main method.

My problem is that, I have to stop MyRun Thread from my StopMyRun file.

For ex. I run MyRun program on one shell and when i run StopMyRun on other shell, The first one should stop their function.

How to do that, Any idea

 
Edwin Dalorzo
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have two different programs running on two different JVMs, you will need some sort of inter process communication. How about a socket, listening for the termination signal?
 
Sunil Kumar Gupta
Ranch Hand
Posts: 824
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Edwin Dalorzo:
You have two different programs running on two different JVMs, you will need some sort of inter process communication. How about a socket, listening for the termination signal?


Actually the first program is not generating any signal. I want to terminate that process as soon as i run another process.

Now any idea.
 
Srinivasa Raghavan
Ranch Hand
Posts: 1228
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why cant you use a flag in DB. Check that flaag at regular intervals & stop you first process ?
 
Sunil Kumar Gupta
Ranch Hand
Posts: 824
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Srinivasa Raghavan:
Why cant you use a flag in DB. Check that flaag at regular intervals & stop you first process ?


Actually i have no idea, when i have to stop that process. Whenever any user starts other process, it will automaticlly shutdown the first process. And problem is that these two process are running on different JVM's

 
Srinivasa Raghavan
Ranch Hand
Posts: 1228
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When the second process starts running it should update the flag in th DB. The first process should check this flag in a seperate thread and exit when the flag gets updated..
 
Sunil Kumar Gupta
Ranch Hand
Posts: 824
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes, i think you are right, But what if i am not using DB, Should i do this using any file instead of DB, Where i simply swicth the value , say from 1 to 2, and if 2 then exit first one..
 
Tomasz Luchowski
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is probably an overkill, but have you looked at Java Management Extensions (JMX)?
 
Ulf Dittmer
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Edwins suggestion of using sockets is probably the simplest to implement, and it does not rely on any external resources (a database or a file system).
[ April 22, 2006: Message edited by: Ulf Dittmer ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic