您的位置:首页 > 编程语言 > Java开发

Check the state of child process./thread in java

2014-01-03 01:59 453 查看
To run a child process/thread, you always need to check whether the process/thread is timeout or not.

Two ways to determine whether the process/thread is finished or not:

1) Process p=new ....
p.waitFor() // a blocked waiting for the exit of the process

2) Process p=new ..
p.exitValue() // actually, it is a function to check the return value of the process
// but we can use it to determine whether the process has been exit or not
// exitValue() will cause IllegalThreadStateException is the process has not been exit

User the following exception handling method to check:

while (true) {
try {
retVal = proc.exitValue();
// process finished!
break;
} catch (IllegalThreadStateException e) {
// process is still running...
if (System.currentTimeMillis() >= endtime) {
// Process timed out
timedOut = true;
proc.destroy();
retVal = -1;
break;
} else {
try {
Thread.sleep(10000);
} catch (InterruptedException e1) {
throw new RuntimeException(e1);
}
}
}
}
本文出自 “maxwell” 博客,请务必保留此出处http://drmaxwell.blog.51cto.com/394635/1347900
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐