java exit for_java.lang.Process.exitValue()和waitFor()和destroy()
currProcess=currRuntime.exec(cmd);这个返回一个process对象destroy,exitValue,waitFor有三个方法看官方文档waitForpublic abstract int waitFor()throws InterruptedExceptionCauses the current thread to wait, if necessary, unti
currProcess=currRuntime.exec(cmd);
这个返回一个process对象
destroy,exitValue,waitFor
有三个方法
看官方文档
waitFor
public abstract int waitFor()
throws InterruptedException
Causes the current thread to wait, if necessary, until the process represented by this Process object has terminated. This method returns immediately if the subprocess has already terminated. If the subprocess has not yet terminated, the calling thread will
be blocked until the subprocess exits.
Returns:
the exit value of the subprocess represented by this Process object. By convention, the value 0 indicates normal termination.
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.
一直挂起,直到子进程执行结束
返回值0表示正常退出
exitValue
public abstract int exitValue()
Returns the exit value for the subprocess.
Returns:
the exit value of the subprocess represented by this Process object. By convention, the value 0 indicates normal termination.
Throws:
IllegalThreadStateException - if the subprocess represented by this Process object has not yet terminated
返回0是正常
但是如果子进程还没结束
会报异常
destroy
public abstract void destroy()
Kills the subprocess. The subprocess represented by this Process object is forcibly terminated.
强制关掉子进程
总体来说
还是调用 waitfor()==0
进行判断
既可以让子进程跑完
还可以判断子进程的执行状态
最佳的选择就是使用这个! waitfor()
魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐


所有评论(0)