gpt4 book ai didi

java - PrintWriter.print 在字符串太长时阻塞

转载 作者:行者123 更新时间:2023-11-30 06:59:22 26 4
gpt4 key购买 nike

我正在用 java 编写一个包装程序,它只是应该通过写入其他进程的标准 in 流来将参数传递给其他进程,并从他们的标准输出流中读取响应。但是,当我尝试传入的 String 太大时,PrintWriter.print 就会阻塞。没有错误,只是卡住。对此有好的解决方法吗?

相关代码

public class Wrapper {
PrintWriter writer;

public Wrapper(String command){
start(command);
}

public void call(String args){
writer.println(args); // Blocks here
writer.flush();

//Other code
}

public void start(String command) {
try {
ProcessBuilder pb = new ProcessBuilder(command.split(" "));
pb.redirectErrorStream(true);
process = pb.start();
// STDIN of the process.
writer = new PrintWriter(new OutputStreamWriter(process.getOutputStream(), "UTF-8"));
} catch (Exception e) {
e.printStackTrace();
System.out.println("Process ended catastrophically.");
}
}


}

如果我尝试使用

writer.print(args);
writer.print("\n");

它可以在卡住之前处理更大的字符串,但最终仍会锁定。有没有缓冲流的方法来解决这个问题? print 是否会阻塞有足够空间的进程流?

更新

为了回应一些回答和评论,我提供了更多信息。

  • 操作系统是Windows 7
  • BufferedWriter 减慢了运行时间,但并没有阻止它最终阻塞。
  • 字符串可能会很长,长达 100,000 个字符
  • Process 输入被消耗,但按行消耗,即 Scanner.nextLine();

测试代码

import java.io.IOException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeoutException;

import ProcessRunner.Wrapper;


public class test {

public static void main(String[] args){
System.out.println("Building...");
Wrapper w = new Wrapper("java echo");
System.out.println("Calling...");
String market = "aaaaaa";
for(int i = 0; i < 1000; i++){
try {
System.out.println(w.call(market, 1000));
} catch (InterruptedException | ExecutionException
| TimeoutException e) {

System.out.println("Timed out");
}
market = market + market;
System.out.println("Size = " + market.length());
}
System.out.println("Stopping...");
try {
w.stop();
} catch (IOException e) {

e.printStackTrace();
System.out.println("Stop failed :(");
}
}
}

测试过程:

您必须先编译这个文件,并确保.class 与测试.class 文件在同一个文件夹中

import java.util.Scanner;


public class echo {

public static void main(String[] args){
while(true){
Scanner stdIn = new Scanner(System.in);
System.out.println(stdIn.nextLine());
}
}

}

最佳答案

我怀疑这里发生的是外部进程正在写入其标准输出。由于您的 Java 代码不读取它,它最终会填充外部进程的标准输出(或错误)管道。这会阻止外部进程,这意味着它可以从其输入管道中读取....并且您的 Java 进程会卡住。


如果这是问题所在,那么使用缓冲写入器将无法解决问题。您需要读取外部进程输出或将其重定向到文件(例如 Linux 上的“/dev/null”)

关于java - PrintWriter.print 在字符串太长时阻塞,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31620863/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com