为什么Java.lang.Process不会在此代码中抛出InterruptedException?(Why is Java.lang.Process not throwing Interrupted

编程入门 行业动态 更新时间:2024-10-15 14:17:46
为什么Java.lang.Process不会在此代码中抛出InterruptedException?(Why is Java.lang.Process not throwing InterruptedException in this code?)

此方法使用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.

更多推荐

本文发布于:2023-08-02 20:48:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1381933.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:在此   抛出   代码   Process   lang

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!