日期:2014-05-20 浏览次数:21810 次
Runtime runtime = Runtime.getRuntime();
Process process = null;
try {
process = runtime.exec("cmd /c Tasklist");
BufferedReader in = new BufferedReader(new InputStreamReader(
process.getInputStream()));
String s = "";
while ((s = in.readLine()) != null) {
//看看有没有想要的进程
}
}
------解决方案--------------------
如果process启动的程序也是你自己写的,那么就设置一个flag,程序即行结束时修改flag,timer就一直监视flag的值,如果程序不是你写的,那就像LZ说的,读取系统的任务list看看存在不存在你的进程(linux的话可以用ps -ef | grep来判断)
------解决方案--------------------
掉一个线程去监听 进程 的运行。
public class ProcessState extends Thread {
public ProcessState(Process p) {
process = p;
}
@Override
public void run() {
try {
process.waitFor();
} catch (InterruptedException ex) {
throw new RuntimeException();
}
}
public void startListen() {
start();
}
public boolean isExit() {
switch (getState()) {
case NEW:
throw new RuntimeException();
case TERMINATED:
return true;
default:
return false;
}
}
private Process process;
}