isAlive() 方法用于测试当前正在执行的进程是否存活。
用法
Public Boolean isAlive()
参数
NA
返回
isAlive() 方法返回两个值:true 或 false
- 如果 process 对象执行的进程尚未终止,则该方法返回 true。
- 如果 process 对象执行的进程尚未终止,则该方法返回 false。
例子1
public class Process_isAliveMethodExample1 {
public static void main(String[] args) {
try{
//A process is created.
System.out.println("A process is created:");
Process p = Runtime.getRuntime().exec("mspaint.exe");
//Waits for the process until you terminate.
p.waitFor();
//Tests whether the current process is alive or not.
System.out.println(p.isAlive());
//The process is closed manually.
System.out.println("Manually closes the notepad.");
//Finally the wait is over.
System.out.println("Wait is completed.");
}catch(Exception e)
{
System.out.println(e);
}
}}
输出:
A process is created: false Manually closes the notepad. Wait is completed.
例子2
public class Process_isAliveMethodExample2 {
public static void main(String[] args) {
try{
// At first Notepad is opened.
System.out.println("Notepad will open");
Process p1=Runtime.getRuntime().exec("notepad.exe");
// Waits for the process p1 to terminate.
p1.waitFor();
//Tests whether the process p1 is alive.
System.out.println(p1.isAlive());
//After that notepad is opened.
System.out.println("Ms paint will open");
Process p2=Runtime.getRuntime().exec("mspaint.exe");
//Again waits for the process to terminate.
p2.waitFor();
// Tests whether the process p2 is alive.
System.out.println(p2.isAlive());
}catch(Exception e)
{
System.out.println("Ohho! an exception has occurred:");
}
}
}
输出:
Notepad will open false Ms paint will open False
相关用法
- Java Process waitFor()用法及代码示例
- Java Process Destroy()用法及代码示例
- Java Process destroy()用法及代码示例
- Java Process getOutputStream()用法及代码示例
- Java Process exitValue()用法及代码示例
- Java Process getInputStream()用法及代码示例
- Java Process getErrorStream()用法及代码示例
- Java ProcessBuilder redirectErrorStream()用法及代码示例
- Java ProcessBuilder environment()用法及代码示例
- Java ProcessBuilder start()用法及代码示例
- Java ProcessBuilder directory()用法及代码示例
- Java Provider keySet()用法及代码示例
- Java Properties propertyNames()用法及代码示例
- Java Provider.Service getAttribute()用法及代码示例
- Java Provider getName()用法及代码示例
- Java Properties compute(Key, BiFunction)用法及代码示例
- Java Provider keys()用法及代码示例
- Java Properties containsKey(value)用法及代码示例
- Java Properties computeIfAbsent(Key, Function)用法及代码示例
- Java Provider.Service toString()用法及代码示例
注:本文由纯净天空筛选整理自 Java Process isAlive() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。