当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java Process isAlive()用法及代码示例


isAlive() 方法用于测试当前正在执行的进程是否存活。

用法

Public Boolean isAlive()

参数

NA

返回

isAlive() 方法返回两个值:true 或 false

  1. 如果 process 对象执行的进程尚未终止,则该方法返回 true。
  2. 如果 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 isAlive() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。