gpt4 book ai didi

Java在线程中处理输入流

转载 作者:行者123 更新时间:2023-12-01 05:57:24 25 4
gpt4 key购买 nike

我开发了一个 Eclipse 插件,但遇到了问题

我的代码如下:

String run_pelda = "cmd.exe /C pelda.exe";
Runtime pelda_rt = Runtime.getRuntime();
Process pelda_proc = javacheckgen_rt.exec(run_pelda);

在我想读取输入流之后:

InputStream toolstr = tool_proc.getInputStream();
InputStreamReader r = new InputStreamReader(toolstr);
BufferedReader in = new BufferedReader(r);

但是我的新 Eclipse 实例死机了。我认为我应该在java线程中执行它,但不幸的是我不知道如何正确使用它。

请给我一些想法!

最佳答案

看看优秀的文章When Runtime.exec() won't来自 JavaWorld 并看看是否有帮助。特别是,这可能是你的罪魁祸首:

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.

本文提供了解决此问题的各种方法,包括在后台线程中使用 stderrstdoutStreamGobbler 类的源代码.

令人惊讶的是这篇文章的效果如此之好。它最初写于 2000 年,我发现其中几乎所有内容仍然准确。

关于Java在线程中处理输入流,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2458520/

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