Sory I am doing Java code after many many years so might be doing something wrong and need help in resolving this issue.
My main thread (or main program) is reading a file and for each entry in file it is invoking a sub-thread that perform series of action like doing ping etc.
In each child thread I am invoking process to execute certain tasks or execute batch file to make connection to remote server and perform activity.
There are certain server where commands\tasks are not returning and hence compelete program goes into hung state. I want to kill those process that triggered those commands\tasks and trying to use process.destroy. But tt is not working at all.
Please suggest what could be the issue as I have tried almost everything that I can think of and how to fix it. Thank a ton in advance.
There are two obvious things wrong with your code fragments. First, though you call p.waitFor() you totally ignore the return code which may or may not contain diagnostic information and second you ignore the process 'stderr'! The most common 'hang up' scenario I have met when using Runtime.exec() is caused by failure to handle 'stderr' properly when the process fails. In this situation one can get a deadlock when the 'stderr' buffer becomes full and the process suspends until more buffer space is available to write more of 'stderr'. If the Java side does not read the 'stderr' stream the buffer never ever ever has space available - a deadlock!
All this and more is detailed in the ancient but still very relevant article - http://www.javaworld.com/jw-12-2000/jw-1229-traps.html . You should read this and implement all the recommendations. Don't be tempted to miss out anything because you think it cannot apply to you because it can. Note - pay particular attention to the use of separate threads for handling the various Process streams.