Java在线程中处理输入流

发布于 2024-08-25 08:57:12 字数 485 浏览 5 评论 0原文

我开发了一个 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线程中完成它,但不幸的是我不知道如何正确使用它。

请给我一些想法!

I develop an Eclipse plugin and I have a problem

My code is the following one:

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);

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.

Please give me some ideas!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

流绪微梦 2024-09-01 08:57:12

看看这篇优秀的文章 当 Runtime.exec() 不会 来自 JavaWorld 并查看是否有帮助。特别是,这可能是你的罪魁祸首:

由于某些原生平台只为标准输入输出流提供有限的缓冲区大小,未能及时写入子进程的输入流或读取子进程的输出流可能会导致子进程阻塞,甚至死锁。

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

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

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.

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.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文