我正在尝试使用我的程序运行/usr/bin/perl -e 'for(my $i=0;$i<1000;$i++){print 1x1000;print STDERR 2x1000;}'
(在终端中运行)。
ProcessBuilder pb = new ProcessBuilder(go); //go is the command
process = pb.start();
BufferedReader incommandbuf = new BufferedReader(new InputStreamReader(process.getInputStream()),1024*1000);
BufferedReader errcommandbuf = new BufferedReader(new InputStreamReader(process.getErrorStream()),1024*1000);
stdString = "";
while ((line = incommandbuf.readLine()) != null)
{
stdString += line + "\n";
}
String errorstrtemp = "";
while ((line = errcommandbuf.readLine()) != null)
{
errorstrtemp += line + "\n";
}
如果我尝试运行此功能,则会挂起while ((line = incommandbuf.readLine()) != null)
。如果我将命令更改为/usr/bin/perl -e 'for(my $i=0;$i<64;$i++){print 1x1000;print STDERR 2x1000;}'
,程序将运行。如果它达到65或更高它不起作用。起初我以为我只需要改变我的BufferedReaders的大小,但它没有帮助。有什么原因导致这个问题?如果需要,我会提供任何其他信息。
感谢。
答案 0 :(得分:5)
您正在一次阅读一个流。当另一个流填满缓冲区时,您的进程将停止等待您阅读它。解决方案是读取不同线程中的流或使用ProcessBuilder.redirectErrorStream