jdk/src/share/classes/java/lang/Process.java
author jfranck
Mon, 30 Sep 2013 11:18:18 +0200
changeset 20481 2735b307d256
parent 13149 27d52f97a5cc
child 24577 b3bf9c82a050
permissions -rw-r--r--
8007072: Update Core Reflection for Type Annotations to match latest spec 8022324: j.l.Class.getAnnotatedInterfaces() for array type returns wrong value 8024915: j.l.r.Executable.getAnnotatedReceiverType() should return null for static methods Summary: Update javadoc and implementation of reflection for type annotations to match latest spec Reviewed-by: darcy
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
13149
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 5506
diff changeset
     2
 * Copyright (c) 1995, 2012, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.lang;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.*;
13149
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 5506
diff changeset
    29
import java.util.concurrent.TimeUnit;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * The {@link ProcessBuilder#start()} and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * {@link Runtime#exec(String[],String[],File) Runtime.exec}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * methods create a native process and return an instance of a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * subclass of {@code Process} that can be used to control the process
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * and obtain information about it.  The class {@code Process}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * provides methods for performing input from the process, performing
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * output to the process, waiting for the process to complete,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * checking the exit status of the process, and destroying (killing)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * the process.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * <p>The methods that create processes may not work well for special
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * processes on certain native platforms, such as native windowing
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * processes, daemon processes, Win16/DOS processes on Microsoft
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
    45
 * Windows, or shell scripts.
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
    46
 *
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
    47
 * <p>By default, the created subprocess does not have its own terminal
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
    48
 * or console.  All its standard I/O (i.e. stdin, stdout, stderr)
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
    49
 * operations will be redirected to the parent process, where they can
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
    50
 * be accessed via the streams obtained using the methods
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
    51
 * {@link #getOutputStream()},
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
    52
 * {@link #getInputStream()}, and
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
    53
 * {@link #getErrorStream()}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * The parent process uses these streams to feed input to and get output
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * from the subprocess.  Because some native platforms only provide
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * limited buffer size for standard input and output streams, failure
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * to promptly write the input stream or read the output stream of
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
    58
 * the subprocess may cause the subprocess to block, or even deadlock.
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
    59
 *
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
    60
 * <p>Where desired, <a href="ProcessBuilder.html#redirect-input">
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
    61
 * subprocess I/O can also be redirected</a>
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
    62
 * using methods of the {@link ProcessBuilder} class.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * <p>The subprocess is not killed when there are no more references to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * the {@code Process} object, but rather the subprocess
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * continues executing asynchronously.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * <p>There is no requirement that a process represented by a {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * Process} object execute asynchronously or concurrently with respect
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * to the Java process that owns the {@code Process} object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 *
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
    72
 * <p>As of 1.5, {@link ProcessBuilder#start()} is the preferred way
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
    73
 * to create a {@code Process}.
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
    74
 *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * @since   JDK1.0
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
public abstract class Process {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * Returns the output stream connected to the normal input of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * subprocess.  Output to the stream is piped into the standard
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
    81
     * input of the process represented by this {@code Process} object.
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
    82
     *
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
    83
     * <p>If the standard input of the subprocess has been redirected using
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
    84
     * {@link ProcessBuilder#redirectInput(Redirect)
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
    85
     * ProcessBuilder.redirectInput}
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
    86
     * then this method will return a
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
    87
     * <a href="ProcessBuilder.html#redirect-input">null output stream</a>.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * <p>Implementation note: It is a good idea for the returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * output stream to be buffered.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * @return the output stream connected to the normal input of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     *         subprocess
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     */
13149
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 5506
diff changeset
    95
    public abstract OutputStream getOutputStream();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * Returns the input stream connected to the normal output of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * subprocess.  The stream obtains data piped from the standard
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   100
     * output of the process represented by this {@code Process} object.
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   101
     *
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   102
     * <p>If the standard output of the subprocess has been redirected using
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   103
     * {@link ProcessBuilder#redirectOutput(Redirect)
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   104
     * ProcessBuilder.redirectOutput}
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   105
     * then this method will return a
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   106
     * <a href="ProcessBuilder.html#redirect-output">null input stream</a>.
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   107
     *
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   108
     * <p>Otherwise, if the standard error of the subprocess has been
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   109
     * redirected using
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   110
     * {@link ProcessBuilder#redirectErrorStream(boolean)
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   111
     * ProcessBuilder.redirectErrorStream}
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   112
     * then the input stream returned by this method will receive the
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   113
     * merged standard output and the standard error of the subprocess.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * <p>Implementation note: It is a good idea for the returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * input stream to be buffered.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * @return the input stream connected to the normal output of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     *         subprocess
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     */
13149
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 5506
diff changeset
   121
    public abstract InputStream getInputStream();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    /**
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   124
     * Returns the input stream connected to the error output of the
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   125
     * subprocess.  The stream obtains data piped from the error output
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   126
     * of the process represented by this {@code Process} object.
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   127
     *
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   128
     * <p>If the standard error of the subprocess has been redirected using
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   129
     * {@link ProcessBuilder#redirectError(Redirect)
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   130
     * ProcessBuilder.redirectError} or
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   131
     * {@link ProcessBuilder#redirectErrorStream(boolean)
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   132
     * ProcessBuilder.redirectErrorStream}
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   133
     * then this method will return a
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   134
     * <a href="ProcessBuilder.html#redirect-output">null input stream</a>.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * <p>Implementation note: It is a good idea for the returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * input stream to be buffered.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     *
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   139
     * @return the input stream connected to the error output of
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     *         the subprocess
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     */
13149
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 5506
diff changeset
   142
    public abstract InputStream getErrorStream();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * Causes the current thread to wait, if necessary, until the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * process represented by this {@code Process} object has
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * terminated.  This method returns immediately if the subprocess
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * has already terminated.  If the subprocess has not yet
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * terminated, the calling thread will be blocked until the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * subprocess exits.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * @return the exit value of the subprocess represented by this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     *         {@code Process} object.  By convention, the value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     *         {@code 0} indicates normal termination.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * @throws InterruptedException if the current thread is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     *         {@linkplain Thread#interrupt() interrupted} by another
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     *         thread while it is waiting, then the wait is ended and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     *         an {@link InterruptedException} is thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     */
13149
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 5506
diff changeset
   160
    public abstract int waitFor() throws InterruptedException;
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 5506
diff changeset
   161
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 5506
diff changeset
   162
    /**
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 5506
diff changeset
   163
     * Causes the current thread to wait, if necessary, until the
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 5506
diff changeset
   164
     * subprocess represented by this {@code Process} object has
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 5506
diff changeset
   165
     * terminated, or the specified waiting time elapses.
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 5506
diff changeset
   166
     *
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 5506
diff changeset
   167
     * <p>If the subprocess has already terminated then this method returns
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 5506
diff changeset
   168
     * immediately with the value {@code true}.  If the process has not
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 5506
diff changeset
   169
     * terminated and the timeout value is less than, or equal to, zero, then
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 5506
diff changeset
   170
     * this method returns immediately with the value {@code false}.
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 5506
diff changeset
   171
     *
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 5506
diff changeset
   172
     * <p>The default implementation of this methods polls the {@code exitValue}
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 5506
diff changeset
   173
     * to check if the process has terminated. Concrete implementations of this
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 5506
diff changeset
   174
     * class are strongly encouraged to override this method with a more
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 5506
diff changeset
   175
     * efficient implementation.
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 5506
diff changeset
   176
     *
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 5506
diff changeset
   177
     * @param timeout the maximum time to wait
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 5506
diff changeset
   178
     * @param unit the time unit of the {@code timeout} argument
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 5506
diff changeset
   179
     * @return {@code true} if the subprocess has exited and {@code false} if
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 5506
diff changeset
   180
     *         the waiting time elapsed before the subprocess has exited.
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 5506
diff changeset
   181
     * @throws InterruptedException if the current thread is interrupted
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 5506
diff changeset
   182
     *         while waiting.
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 5506
diff changeset
   183
     * @throws NullPointerException if unit is null
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 5506
diff changeset
   184
     * @since 1.8
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 5506
diff changeset
   185
     */
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 5506
diff changeset
   186
    public boolean waitFor(long timeout, TimeUnit unit)
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 5506
diff changeset
   187
        throws InterruptedException
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 5506
diff changeset
   188
    {
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 5506
diff changeset
   189
        long startTime = System.nanoTime();
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 5506
diff changeset
   190
        long rem = unit.toNanos(timeout);
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 5506
diff changeset
   191
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 5506
diff changeset
   192
        do {
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 5506
diff changeset
   193
            try {
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 5506
diff changeset
   194
                exitValue();
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 5506
diff changeset
   195
                return true;
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 5506
diff changeset
   196
            } catch(IllegalThreadStateException ex) {
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 5506
diff changeset
   197
                if (rem > 0)
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 5506
diff changeset
   198
                    Thread.sleep(
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 5506
diff changeset
   199
                        Math.min(TimeUnit.NANOSECONDS.toMillis(rem) + 1, 100));
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 5506
diff changeset
   200
            }
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 5506
diff changeset
   201
            rem = unit.toNanos(timeout) - (System.nanoTime() - startTime);
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 5506
diff changeset
   202
        } while (rem > 0);
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 5506
diff changeset
   203
        return false;
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 5506
diff changeset
   204
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * Returns the exit value for the subprocess.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * @return the exit value of the subprocess represented by this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     *         {@code Process} object.  By convention, the value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     *         {@code 0} indicates normal termination.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * @throws IllegalThreadStateException if the subprocess represented
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     *         by this {@code Process} object has not yet terminated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     */
13149
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 5506
diff changeset
   215
    public abstract int exitValue();
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 5506
diff changeset
   216
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 5506
diff changeset
   217
    /**
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 5506
diff changeset
   218
     * Kills the subprocess. Whether the subprocess represented by this
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 5506
diff changeset
   219
     * {@code Process} object is forcibly terminated or not is
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 5506
diff changeset
   220
     * implementation dependent.
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 5506
diff changeset
   221
     */
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 5506
diff changeset
   222
    public abstract void destroy();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * Kills the subprocess. The subprocess represented by this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * {@code Process} object is forcibly terminated.
13149
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 5506
diff changeset
   227
     *
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 5506
diff changeset
   228
     * <p>The default implementation of this method invokes {@link #destroy}
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 5506
diff changeset
   229
     * and so may not forcibly terminate the process. Concrete implementations
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 5506
diff changeset
   230
     * of this class are strongly encouraged to override this method with a
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 5506
diff changeset
   231
     * compliant implementation.  Invoking this method on {@code Process}
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 5506
diff changeset
   232
     * objects returned by {@link ProcessBuilder#start} and
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 5506
diff changeset
   233
     * {@link Runtime#exec} will forcibly terminate the process.
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 5506
diff changeset
   234
     *
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 5506
diff changeset
   235
     * <p>Note: The subprocess may not terminate immediately.
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 5506
diff changeset
   236
     * i.e. {@code isAlive()} may return true for a brief period
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 5506
diff changeset
   237
     * after {@code destroyForcibly()} is called. This method
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 5506
diff changeset
   238
     * may be chained to {@code waitFor()} if needed.
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 5506
diff changeset
   239
     *
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 5506
diff changeset
   240
     * @return the {@code Process} object representing the
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 5506
diff changeset
   241
     *         subprocess to be forcibly destroyed.
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 5506
diff changeset
   242
     * @since 1.8
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     */
13149
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 5506
diff changeset
   244
    public Process destroyForcibly() {
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 5506
diff changeset
   245
        destroy();
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 5506
diff changeset
   246
        return this;
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 5506
diff changeset
   247
    }
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 5506
diff changeset
   248
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 5506
diff changeset
   249
    /**
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 5506
diff changeset
   250
     * Tests whether the subprocess represented by this {@code Process} is
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 5506
diff changeset
   251
     * alive.
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 5506
diff changeset
   252
     *
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 5506
diff changeset
   253
     * @return {@code true} if the subprocess represented by this
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 5506
diff changeset
   254
     *         {@code Process} object has not yet terminated.
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 5506
diff changeset
   255
     * @since 1.8
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 5506
diff changeset
   256
     */
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 5506
diff changeset
   257
    public boolean isAlive() {
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 5506
diff changeset
   258
        try {
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 5506
diff changeset
   259
            exitValue();
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 5506
diff changeset
   260
            return false;
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 5506
diff changeset
   261
        } catch(IllegalThreadStateException e) {
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 5506
diff changeset
   262
            return true;
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 5506
diff changeset
   263
        }
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 5506
diff changeset
   264
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
}