Why processBuilder in java hangs after 5 mins?

夙愿已清 提交于 2019-12-10 11:41:21

问题


I hava command line which process more than 5 mins. when I invoke command line with ProcessBuilder, it works the command completes the job with in 5 mins.

Whereas the process hangs if it takes more than 5 mins and shows no improvement on process until I quit the process.

p = new ProcessBuilder("myprogram","with","parameter").start();
p.waitFor();

Please let me know if you doesn't understand the above question?


回答1:


The problem might be, that command "myprogram" produces some output, and you are not reading it. This means that the process is blocked as soon as the buffer is full and waits for your process to continue reading. Your process in turn waits for the other process to finish (which it won't because it waits for your process, ...). This is a classical deadlock situation.

You need to continually read from the processes input stream to ensure that it doesn't block.

Javadocs says:

Class Process

Because some native platforms only provide limited buffer size for standard input and output streams, failure to promptly write the input stream or read the output stream of the subprocess may cause the subprocess to block, and even deadlock.

Fail to clear the buffer of input stream (which pipes to the output stream of subprocess) from Process may lead to a subprocess blocking.



来源:https://stackoverflow.com/questions/38098494/why-processbuilder-in-java-hangs-after-5-mins

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!