可以 '信号' 通过写/从程序INT/OutputStream中
读好一个过程,落实德线(见下文)
我不知道.. 。
也许这个片段有助于
public class ProcessTest {
public static void main(String[] args) {
new ProcessTest().start();
}
private void start() {
startWatchDog();
startProcess();
}
private boolean abortCondition = false;
private int watchDogTSleepTime = 3000; //3 sek
private void startWatchDog() {
Runnable r = new Runnable() {
@Override
public void run() {
while(!abortCondition){
try {
Thread.sleep(watchDogTSleepTime);
} catch (InterruptedException e) {
e.printStackTrace();
}
//check the file touch
boolean ok = checkFileTouch();
try {
//send signals to the process
outStream.write("signal".getBytes());
} catch (IOException e) {
e.printStackTrace();
}
//if you want, you might try to kill the process
process.destroy();
}
}
};
Thread watchDog = new Thread(r);
watchDog.start();
//watchDog.setDaemon(true); //maybe
}
private boolean checkFileTouch(){
//...
return false;
}
private InputStream inStream;
private OutputStream outStream;
private Process process;
private void startProcess() {
String[] cmd = new String[]{"foo.exe", "para1", "param2"};
try {
//create and start the process
process = Runtime.getRuntime().exec(cmd);
inStream = process.getInputStream();
outStream = process.getOutputStream();
} catch (IOException e) {
e.printStackTrace();
}
}
}