简体   繁体   English

BufferedReader.readline()挂起

[英]BufferedReader.readline() hangs

I'm trying to run /usr/bin/perl -e 'for(my $i=0;$i<1000;$i++){print 1x1000;print STDERR 2x1000;}' (which works in terminal) with my program. 我正在尝试用我的程序运行/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";
}

If I try to run this it hangs on while ((line = incommandbuf.readLine()) != null) . 如果我尝试运行此程序,则它会在while ((line = incommandbuf.readLine()) != null)时挂起。 The program runs if I change the command to /usr/bin/perl -e 'for(my $i=0;$i<64;$i++){print 1x1000;print STDERR 2x1000;}' . 如果我将命令更改为/usr/bin/perl -e 'for(my $i=0;$i<64;$i++){print 1x1000;print STDERR 2x1000;}' If it goes up to 65 and higher it doesn't work. 如果上升到65或更高,则不起作用。 At first I thought I just have to change the size of the my BufferedReaders but it didn't help. 起初,我以为我只需要更改BufferedReaders的大小即可,但无济于事。 Any clue on what is causing this? 关于什么原因的任何线索? I will provide any additional info if needed. 如果需要,我将提供任何其他信息。 Thanks. 谢谢。

You are reading one stream at a time. 您一次正在读取一个流。 When the other stream fills up the buffer, your Process will stop waiting for you to read it. 当另一个流填满缓冲区时,您的进程将停止等待您读取它。 The solution is to either read the streams in different threads or use ProcessBuilder.redirectErrorStream 解决方案是读取不同线程中的流或使用ProcessBuilder.redirectErrorStream

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM