当前位置: 首页 > 工具软件 > until-destroy > 使用案例 >

java exit for_java.lang.Process.exitValue()和waitFor()和destroy()

刘兴修
2023-12-01

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()

 类似资料: