jdk/src/share/classes/java/lang/Process.java
changeset 13149 27d52f97a5cc
parent 5506 202f599c92aa
child 24577 b3bf9c82a050
equal deleted inserted replaced
13033:365efcc2d50c 13149:27d52f97a5cc
     1 /*
     1 /*
     2  * Copyright (c) 1995, 2008, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 1995, 2012, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     7  * published by the Free Software Foundation.  Oracle designates this
    24  */
    24  */
    25 
    25 
    26 package java.lang;
    26 package java.lang;
    27 
    27 
    28 import java.io.*;
    28 import java.io.*;
       
    29 import java.util.concurrent.TimeUnit;
    29 
    30 
    30 /**
    31 /**
    31  * The {@link ProcessBuilder#start()} and
    32  * The {@link ProcessBuilder#start()} and
    32  * {@link Runtime#exec(String[],String[],File) Runtime.exec}
    33  * {@link Runtime#exec(String[],String[],File) Runtime.exec}
    33  * methods create a native process and return an instance of a
    34  * methods create a native process and return an instance of a
    89      * output stream to be buffered.
    90      * output stream to be buffered.
    90      *
    91      *
    91      * @return the output stream connected to the normal input of the
    92      * @return the output stream connected to the normal input of the
    92      *         subprocess
    93      *         subprocess
    93      */
    94      */
    94     abstract public OutputStream getOutputStream();
    95     public abstract OutputStream getOutputStream();
    95 
    96 
    96     /**
    97     /**
    97      * Returns the input stream connected to the normal output of the
    98      * Returns the input stream connected to the normal output of the
    98      * subprocess.  The stream obtains data piped from the standard
    99      * subprocess.  The stream obtains data piped from the standard
    99      * output of the process represented by this {@code Process} object.
   100      * output of the process represented by this {@code Process} object.
   115      * input stream to be buffered.
   116      * input stream to be buffered.
   116      *
   117      *
   117      * @return the input stream connected to the normal output of the
   118      * @return the input stream connected to the normal output of the
   118      *         subprocess
   119      *         subprocess
   119      */
   120      */
   120     abstract public InputStream getInputStream();
   121     public abstract InputStream getInputStream();
   121 
   122 
   122     /**
   123     /**
   123      * Returns the input stream connected to the error output of the
   124      * Returns the input stream connected to the error output of the
   124      * subprocess.  The stream obtains data piped from the error output
   125      * subprocess.  The stream obtains data piped from the error output
   125      * of the process represented by this {@code Process} object.
   126      * of the process represented by this {@code Process} object.
   136      * input stream to be buffered.
   137      * input stream to be buffered.
   137      *
   138      *
   138      * @return the input stream connected to the error output of
   139      * @return the input stream connected to the error output of
   139      *         the subprocess
   140      *         the subprocess
   140      */
   141      */
   141     abstract public InputStream getErrorStream();
   142     public abstract InputStream getErrorStream();
   142 
   143 
   143     /**
   144     /**
   144      * Causes the current thread to wait, if necessary, until the
   145      * Causes the current thread to wait, if necessary, until the
   145      * process represented by this {@code Process} object has
   146      * process represented by this {@code Process} object has
   146      * terminated.  This method returns immediately if the subprocess
   147      * terminated.  This method returns immediately if the subprocess
   154      * @throws InterruptedException if the current thread is
   155      * @throws InterruptedException if the current thread is
   155      *         {@linkplain Thread#interrupt() interrupted} by another
   156      *         {@linkplain Thread#interrupt() interrupted} by another
   156      *         thread while it is waiting, then the wait is ended and
   157      *         thread while it is waiting, then the wait is ended and
   157      *         an {@link InterruptedException} is thrown.
   158      *         an {@link InterruptedException} is thrown.
   158      */
   159      */
   159     abstract public int waitFor() throws InterruptedException;
   160     public abstract int waitFor() throws InterruptedException;
       
   161 
       
   162     /**
       
   163      * Causes the current thread to wait, if necessary, until the
       
   164      * subprocess represented by this {@code Process} object has
       
   165      * terminated, or the specified waiting time elapses.
       
   166      *
       
   167      * <p>If the subprocess has already terminated then this method returns
       
   168      * immediately with the value {@code true}.  If the process has not
       
   169      * terminated and the timeout value is less than, or equal to, zero, then
       
   170      * this method returns immediately with the value {@code false}.
       
   171      *
       
   172      * <p>The default implementation of this methods polls the {@code exitValue}
       
   173      * to check if the process has terminated. Concrete implementations of this
       
   174      * class are strongly encouraged to override this method with a more
       
   175      * efficient implementation.
       
   176      *
       
   177      * @param timeout the maximum time to wait
       
   178      * @param unit the time unit of the {@code timeout} argument
       
   179      * @return {@code true} if the subprocess has exited and {@code false} if
       
   180      *         the waiting time elapsed before the subprocess has exited.
       
   181      * @throws InterruptedException if the current thread is interrupted
       
   182      *         while waiting.
       
   183      * @throws NullPointerException if unit is null
       
   184      * @since 1.8
       
   185      */
       
   186     public boolean waitFor(long timeout, TimeUnit unit)
       
   187         throws InterruptedException
       
   188     {
       
   189         long startTime = System.nanoTime();
       
   190         long rem = unit.toNanos(timeout);
       
   191 
       
   192         do {
       
   193             try {
       
   194                 exitValue();
       
   195                 return true;
       
   196             } catch(IllegalThreadStateException ex) {
       
   197                 if (rem > 0)
       
   198                     Thread.sleep(
       
   199                         Math.min(TimeUnit.NANOSECONDS.toMillis(rem) + 1, 100));
       
   200             }
       
   201             rem = unit.toNanos(timeout) - (System.nanoTime() - startTime);
       
   202         } while (rem > 0);
       
   203         return false;
       
   204     }
   160 
   205 
   161     /**
   206     /**
   162      * Returns the exit value for the subprocess.
   207      * Returns the exit value for the subprocess.
   163      *
   208      *
   164      * @return the exit value of the subprocess represented by this
   209      * @return the exit value of the subprocess represented by this
   165      *         {@code Process} object.  By convention, the value
   210      *         {@code Process} object.  By convention, the value
   166      *         {@code 0} indicates normal termination.
   211      *         {@code 0} indicates normal termination.
   167      * @throws IllegalThreadStateException if the subprocess represented
   212      * @throws IllegalThreadStateException if the subprocess represented
   168      *         by this {@code Process} object has not yet terminated
   213      *         by this {@code Process} object has not yet terminated
   169      */
   214      */
   170     abstract public int exitValue();
   215     public abstract int exitValue();
       
   216 
       
   217     /**
       
   218      * Kills the subprocess. Whether the subprocess represented by this
       
   219      * {@code Process} object is forcibly terminated or not is
       
   220      * implementation dependent.
       
   221      */
       
   222     public abstract void destroy();
   171 
   223 
   172     /**
   224     /**
   173      * Kills the subprocess. The subprocess represented by this
   225      * Kills the subprocess. The subprocess represented by this
   174      * {@code Process} object is forcibly terminated.
   226      * {@code Process} object is forcibly terminated.
   175      */
   227      *
   176     abstract public void destroy();
   228      * <p>The default implementation of this method invokes {@link #destroy}
       
   229      * and so may not forcibly terminate the process. Concrete implementations
       
   230      * of this class are strongly encouraged to override this method with a
       
   231      * compliant implementation.  Invoking this method on {@code Process}
       
   232      * objects returned by {@link ProcessBuilder#start} and
       
   233      * {@link Runtime#exec} will forcibly terminate the process.
       
   234      *
       
   235      * <p>Note: The subprocess may not terminate immediately.
       
   236      * i.e. {@code isAlive()} may return true for a brief period
       
   237      * after {@code destroyForcibly()} is called. This method
       
   238      * may be chained to {@code waitFor()} if needed.
       
   239      *
       
   240      * @return the {@code Process} object representing the
       
   241      *         subprocess to be forcibly destroyed.
       
   242      * @since 1.8
       
   243      */
       
   244     public Process destroyForcibly() {
       
   245         destroy();
       
   246         return this;
       
   247     }
       
   248 
       
   249     /**
       
   250      * Tests whether the subprocess represented by this {@code Process} is
       
   251      * alive.
       
   252      *
       
   253      * @return {@code true} if the subprocess represented by this
       
   254      *         {@code Process} object has not yet terminated.
       
   255      * @since 1.8
       
   256      */
       
   257     public boolean isAlive() {
       
   258         try {
       
   259             exitValue();
       
   260             return false;
       
   261         } catch(IllegalThreadStateException e) {
       
   262             return true;
       
   263         }
       
   264     }
   177 }
   265 }