process.waitFor() 从不返回 [英] process.waitFor() never returns

查看:56
本文介绍了process.waitFor() 从不返回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Process process = Runtime.getRuntime().exec("tasklist");
BufferedReader reader = 
    new BufferedReader(new InputStreamReader(process.getInputStream()));
process.waitFor();

推荐答案

waitFor() 不返回的原因有很多.

There are many reasons that waitFor() doesn't return.

但通常归结为执行的命令没有退出的事实.

But it usually boils down to the fact that the executed command doesn't quit.

同样,这可能有很多原因.

This, again, can have many reasons.

一个常见的原因是该过程产生了一些输出,而您没有从适当的流中读取.这意味着一旦缓冲区已满,进程就会被阻塞,并等待您的进程继续读取.您的进程反过来等待另一个进程完成(它不会,因为它等待您的进程,......).这是典型的死锁情况.

One common reason is that the process produces some output and you don't read from the appropriate streams. This means that the process is blocked as soon as the buffer is full and waits for your process to continue reading. Your process in turn waits for the other process to finish (which it won't because it waits for your process, ...). This is a classical deadlock situation.

您需要不断从进程输入流中读取数据以确保它不会阻塞.

You need to continually read from the processes input stream to ensure that it doesn't block.

有一篇很好的文章解释了 Runtime.exec() 的所有陷阱并展示了绕过它们的方法,称为 "When Runtime.exec() won't"(是的,文章是2000年的,但内容依然适用!)

There's a nice article that explains all the pitfalls of Runtime.exec() and shows ways around them called "When Runtime.exec() won't" (yes, the article is from 2000, but the content still applies!)

这篇关于process.waitFor() 从不返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆