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
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
相关文章推荐
- java.lang.OutOfMemoryError thrown from the UncaughtExceptionHandler in thread 报错解决
- java.lang.OutOfMemoryError thrown from the UncaughtExceptionHandler in thread "javamelody jenkins
- Each child in an array or iterator should have a unique "key" prop. Check the render method of `Tabl
- [未解决]Exception in thread "main" java.lang.IllegalArgumentException: offset (0) + length (8) exceed the capacity of the array: 6
- Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
- C++: How is the process of function calling in C++
- eclipse Exception in thread "http-bio-8080-exec-2" java.lang.OutOfMemoryError: PermGen space
- Exception in thread "http-bio-8080-exec-2" java.lang.OutOfMemoryError: PermGen space
- Could not find the required version of the Java(TM)2 Runtime Environment in '(null)' 解决办法
- Exception in thread "main" java.lang.OutOfMemoryError: PermGen space
- Exception in thread "http-apr-8080-exec-6" java.lang.OutOfMemoryError: PermGen space 解决!
- It is not possible to run two different versions of ASP.NET in the same IIS process
- The process of installing debain in my T61.
- Class JavaLaunchHelper is implemented in both … libinstrument.dylib. One of the two will be used. Wh
- 本地提交SVN报错 提交文件 is not known to exist in the repository and is not part of the commit, yet its child
- Exception in thread "Timer-0" java.lang.OutOfMemor
- Failed to generate a user instance of SQL Server due to a failure in starting the process for the user instance
- ADF_FACES-30108:The view state of the page has expired because of inactivity. Reload the page
- This generally means that another instance of this process was already running or is hung in the deb
- java.lang.RuntimeException: Date pattern must be set for column update_time in the schema of component