为什么java中的processBuilder会在5分钟后挂起? [英] Why processBuilder in java hangs after 5 mins?

查看:388
本文介绍了为什么java中的processBuilder会在5分钟后挂起?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我hava命令行处理超过5分钟。当我使用ProcessBuilder调用命令行时,它可以在5分钟内完成作业。

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.

如果流程耗时超过5分钟并且在退出流程之前没有改进流程,则流程会挂起。

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?

推荐答案

问题可能是,命令myprogram产生一些输出,而你没有读它。这意味着一旦缓冲区已满,该进程就会被阻止,并等待您的进程继续读取。您的进程反过来等待其他进程完成(这不会因为它等待您的进程,...)。这是一个典型的死锁情况。

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说:

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.

这篇关于为什么java中的processBuilder会在5分钟后挂起?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆