Let me explain my software. What my software simply does is that it creates 10 threads and assigns a number of tasks to each thread. Each thread then creates a Runtime Process that will start a cmd batch file which in turn will start a program that will telnet to a device (I have about 200 of them) to poll its configuration. Here is the code for my process creation:
Process p1 = java.lang.Runtime.getRuntime().exec("cmd /c start /b /wait " + batchFile);
int returnVal = p1.waitFor();
batchFile is the full path of the batch file. Don’t get me wrong, the software works fine up to 100% of the execution and it hanged ONLY ONE TIME at about 95%, so I’m trying to find a solution for that. Why it hanged is not my issue now, but rather how to deal with hangups later on..!
Now the problem is that I need to wait for the process to finish because my telnet client will write to a file that I will read later on in the thread; and hence the use of the .waitFor() . My question is how can I get the thread to understand that the external program hanged? In other words, can I give the external program some time limit to finish; and if it does not the thread will kill the process?
Also I have read about reading the error and output streams; however, I don’t think it is applicable here, or is it?
Almost certainly yes, it is applicable. Try using
ProcessBuilderinstead ofRuntime.execand read all the output before callingwaitFor. For example (exception handling omitted, in particularwaitFormay throwInterruptedExceptionbefore the process actually exited)(IOUtils and NullOutputStream are from Apache commons-io).
To answer the actual question in the title, if your process still hangs even after properly reading its output, you may need to use
p.destroy()to forcibly terminate the process. You could define a timer task something likeand then do
This will cause the process to be killed if it has been running for more than 30 seconds (adjust the
schedulecall to change the timeout).