线程中的Java进程输入流 [英] Java process inputstream in thread
问题描述
我开发了Eclipse插件,但遇到了问题
I develop an Eclipse plugin and I have a problem
我的代码如下:
String run_pelda = "cmd.exe /C pelda.exe";
Runtime pelda_rt = Runtime.getRuntime();
Process pelda_proc = javacheckgen_rt.exec(run_pelda);
在我想阅读输入流之后:
And after I would like to read the inputstream:
InputStream toolstr = tool_proc.getInputStream();
InputStreamReader r = new InputStreamReader(toolstr);
BufferedReader in = new BufferedReader(r);
但是我的新Eclipse实例冻结了.我想我应该在Java线程中执行此操作,但是不幸的是,我不知道正确使用它.
But my new Eclipse instsnce freezes. I think I should do it in java threads, but unfortunatelly I don't know to use it correctly.
请给我一些想法!
推荐答案
Take a look the excellent article When Runtime.exec() won't from JavaWorld and see if it helps. In particular, this is probably your culprit:
由于某些本机平台仅为标准输入和输出流提供了有限的缓冲区大小,因此未能及时写入子进程的输入流或读取子进程的输出流可能导致子进程阻塞甚至死锁.
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.
本文提供了解决此问题的各种方法,包括StreamGobbler
类的源代码,该类在后台线程中使用stderr
和stdout
.
The article provides various ways to address this problem, including the source code for a StreamGobbler
class that consumes stderr
and stdout
in background threads.
这篇文章的出色表现令人惊讶.它最初写于2000年,但我发现几乎所有内容都是准确的.
It's amazing how well this article has held up. It was originally written in 2000 and I find just about all of it to still be accurate.
这篇关于线程中的Java进程输入流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!