i have two java files file1 and file2 as follows in package pak
file1:
package pak;
public class file1 {
public static int x=432;
public static void main(String[] args){
System.out.println("y is "+file2.y);
while(x==432) {
System.out.println("x is "+file1.x);
}
}
}
file 2:
package pak;
public class file2 {
public static int y=46;
public static void main(String[] args){
System.out.println("x is "+file1.x);
++file1.x;
System.out.println("x is "+file1.x);
}
}
i will run first file1 and it will be running and when i run file 2 from another shell it should increment x value and it should come out of loop in file 1
can some one pls help me ??????
You can do this with a shutdown hook.
prints
The last line occurs after I run
kill 29490in another window.This is a complex answer with too many possible solution to mention. (Shared memory, JMS, RMI, signals, Sockets, IPC etc)
Generally, this is done as a high level concept like “stop process” rather than “increment x” even if the result is the same.
The problem is that as its an advanced topic, you would need to have a good understand of interprocess communication to understand the full answer.
The simple solution is; don’t do it, find another way to do what you want.