gpt4 book ai didi

java - 为什么 Java.lang.Process 没有在此代码中抛出 InterruptedException?

转载 作者:行者123 更新时间:2023-11-29 03:01:51 25 4
gpt4 key购买 nike

此方法使用 ProcessBuilder 通过我的 Java 应用程序运行外部 C 程序。我已将其设置为等待 1000 毫秒。我正在向它传递一个进入无限循环的代码。但是这个过程永远不会抛出 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);
}

我还注意到该方法返回了控件,但该进程一直在后台运行。为什么会这样?

最佳答案

来自Documentation of Process :

Returns: true if the subprocess has exited and false if the waiting time elapsed before the subprocess has exited

因此,除了监听 InterruptedException,您应该检查 waitFor() 的返回值。

关于java - 为什么 Java.lang.Process 没有在此代码中抛出 InterruptedException?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34358392/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com