此方法使用ProcessBuilder通过我的Java应用程序运行外部C程序。 我把它设置为等待1000毫秒。 我正在传递一个进入无限循环的代码。 但是进程永远不会抛出interruptException。
public void execute(String sourceFileName, long timeout,String inputFile,String outputFile,String errorFile) throws IOException, InterruptedException { ProcessBuilder builder=new ProcessBuilder(sourceFileName+executableFileExtension); Process process=builder.start(); process.waitFor(timeout,TimeUnit.MILLISECONDS); }我还注意到该方法返回控件但该进程在后台继续运行。 为什么会这样?
This method uses ProcessBuilder to run an external C program through my Java application. I have set it to wait for 1000 ms. I'm passing it a code that goes into infinite loop. But the process never throws interruptedException.
public void execute(String sourceFileName, long timeout,String inputFile,String outputFile,String errorFile) throws IOException, InterruptedException { ProcessBuilder builder=new ProcessBuilder(sourceFileName+executableFileExtension); Process process=builder.start(); process.waitFor(timeout,TimeUnit.MILLISECONDS); }I also noticed that the method returns the control but the process keeps running in the background. Why does this happen?
最满意答案
方法返回控件,但进程在后台继续运行 - 因为您已指定超时限制,到那时,您的C程序尚未完成。 你自己说它是一个无限循环。
我正在传递一个进入无限循环的代码。 但是进程永远不会抛出interruptException。
等待()
文件说
抛出:InterruptedException - 如果当前线程在等待时被另一个线程中断,则等待结束并抛出InterruptedException。
您当前的线程不会被任何其他线程中断,因此它不会抛出InterruptedException。
你的程序的行为似乎并不错误。
method returns the control but the process keeps running in the background - because you have specified a timeout limit and by that time, your C program has not finished. You yourself saying that its an infinite loop.
I'm passing it a code that goes into infinite loop. But the process never throws interruptedException.
waitFor()
documentation says that
Throws: InterruptedException - if the current thread is interrupted by another thread while it is waiting, then the wait is ended and an InterruptedException is thrown.
Your current thread is not interrupted by any other Thread so it doesn't throw InterruptedException.
Your program's behavior doesn't seem erroneous.
更多推荐
发布评论