author | rriggs |
Tue, 11 Apr 2017 14:18:22 -0400 | |
changeset 44640 | 590dec7cadb4 |
parent 39061 | 0a7abfc4f4b0 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
44640
590dec7cadb4
8178347: Process and ProcessHandle getPid method name inconsistency
rriggs
parents:
39061
diff
changeset
|
2 |
* Copyright (c) 1995, 2017, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
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 |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
26 |
package java.lang; |
|
27 |
||
28 |
import java.io.*; |
|
30899
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
29 |
import java.lang.ProcessBuilder.Redirect; |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
30 |
import java.util.concurrent.CompletableFuture; |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
31 |
import java.util.concurrent.ForkJoinPool; |
13149
27d52f97a5cc
4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
5506
diff
changeset
|
32 |
import java.util.concurrent.TimeUnit; |
30899
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
33 |
import java.util.stream.Stream; |
2 | 34 |
|
35 |
/** |
|
30899
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
36 |
* {@code Process} provides control of native processes started by |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
37 |
* ProcessBuilder.start and Runtime.exec. |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
38 |
* The class provides methods for performing input from the process, performing |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
39 |
* output to the process, waiting for the process to complete, |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
40 |
* checking the exit status of the process, and destroying (killing) |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
41 |
* the process. |
2 | 42 |
* The {@link ProcessBuilder#start()} and |
43 |
* {@link Runtime#exec(String[],String[],File) Runtime.exec} |
|
44 |
* methods create a native process and return an instance of a |
|
45 |
* subclass of {@code Process} that can be used to control the process |
|
30899
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
46 |
* and obtain information about it. |
2 | 47 |
* |
48 |
* <p>The methods that create processes may not work well for special |
|
49 |
* processes on certain native platforms, such as native windowing |
|
50 |
* processes, daemon processes, Win16/DOS processes on Microsoft |
|
48
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
51 |
* Windows, or shell scripts. |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
52 |
* |
30899
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
53 |
* <p>By default, the created process does not have its own terminal |
48
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
54 |
* 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
|
55 |
* 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
|
56 |
* be accessed via the streams obtained using the methods |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
57 |
* {@link #getOutputStream()}, |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
58 |
* {@link #getInputStream()}, and |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
59 |
* {@link #getErrorStream()}. |
2 | 60 |
* The parent process uses these streams to feed input to and get output |
30899
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
61 |
* from the process. Because some native platforms only provide |
2 | 62 |
* limited buffer size for standard input and output streams, failure |
63 |
* to promptly write the input stream or read the output stream of |
|
30899
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
64 |
* the process may cause the process to block, or even deadlock. |
48
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
65 |
* |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
66 |
* <p>Where desired, <a href="ProcessBuilder.html#redirect-input"> |
30899
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
67 |
* process I/O can also be redirected</a> |
48
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
68 |
* using methods of the {@link ProcessBuilder} class. |
2 | 69 |
* |
30899
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
70 |
* <p>The process is not killed when there are no more references to |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
71 |
* the {@code Process} object, but rather the process |
2 | 72 |
* continues executing asynchronously. |
73 |
* |
|
30899
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
74 |
* <p>There is no requirement that the process represented by a {@code |
2 | 75 |
* Process} object execute asynchronously or concurrently with respect |
76 |
* to the Java process that owns the {@code Process} object. |
|
77 |
* |
|
48
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
78 |
* <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
|
79 |
* to create a {@code Process}. |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
80 |
* |
30899
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
81 |
* <p>Subclasses of Process should override the {@link #onExit()} and |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
82 |
* {@link #toHandle()} methods to provide a fully functional Process including the |
44640
590dec7cadb4
8178347: Process and ProcessHandle getPid method name inconsistency
rriggs
parents:
39061
diff
changeset
|
83 |
* {@linkplain #pid() process id}, |
590dec7cadb4
8178347: Process and ProcessHandle getPid method name inconsistency
rriggs
parents:
39061
diff
changeset
|
84 |
* {@linkplain #info() information about the process}, |
590dec7cadb4
8178347: Process and ProcessHandle getPid method name inconsistency
rriggs
parents:
39061
diff
changeset
|
85 |
* {@linkplain #children() direct children}, and |
590dec7cadb4
8178347: Process and ProcessHandle getPid method name inconsistency
rriggs
parents:
39061
diff
changeset
|
86 |
* {@linkplain #descendants() direct children plus descendants of those children} of the process. |
30899
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
87 |
* Delegating to the underlying Process or ProcessHandle is typically |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
88 |
* easiest and most efficient. |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
89 |
* |
24865
09b1d992ca72
8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents:
24577
diff
changeset
|
90 |
* @since 1.0 |
2 | 91 |
*/ |
92 |
public abstract class Process { |
|
93 |
/** |
|
30899
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
94 |
* Default constructor for Process. |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
95 |
*/ |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
96 |
public Process() {} |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
97 |
|
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
98 |
/** |
2 | 99 |
* Returns the output stream connected to the normal input of the |
30899
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
100 |
* process. Output to the stream is piped into the standard |
48
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
101 |
* input of the process represented by this {@code Process} object. |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
102 |
* |
30899
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
103 |
* <p>If the standard input of the process has been redirected using |
48
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
104 |
* {@link ProcessBuilder#redirectInput(Redirect) |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
105 |
* ProcessBuilder.redirectInput} |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
106 |
* then this method will return a |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
107 |
* <a href="ProcessBuilder.html#redirect-input">null output stream</a>. |
2 | 108 |
* |
109 |
* <p>Implementation note: It is a good idea for the returned |
|
110 |
* output stream to be buffered. |
|
111 |
* |
|
112 |
* @return the output stream connected to the normal input of the |
|
30899
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
113 |
* process |
2 | 114 |
*/ |
13149
27d52f97a5cc
4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
5506
diff
changeset
|
115 |
public abstract OutputStream getOutputStream(); |
2 | 116 |
|
117 |
/** |
|
118 |
* Returns the input stream connected to the normal output of the |
|
30899
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
119 |
* process. The stream obtains data piped from the standard |
48
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
120 |
* output of the process represented by this {@code Process} object. |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
121 |
* |
30899
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
122 |
* <p>If the standard output of the process has been redirected using |
48
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
123 |
* {@link ProcessBuilder#redirectOutput(Redirect) |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
124 |
* ProcessBuilder.redirectOutput} |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
125 |
* then this method will return a |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
126 |
* <a href="ProcessBuilder.html#redirect-output">null input stream</a>. |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
127 |
* |
30899
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
128 |
* <p>Otherwise, if the standard error of the process has been |
48
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
129 |
* redirected using |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
130 |
* {@link ProcessBuilder#redirectErrorStream(boolean) |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
131 |
* ProcessBuilder.redirectErrorStream} |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
132 |
* then the input stream returned by this method will receive the |
30899
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
133 |
* merged standard output and the standard error of the process. |
2 | 134 |
* |
135 |
* <p>Implementation note: It is a good idea for the returned |
|
136 |
* input stream to be buffered. |
|
137 |
* |
|
138 |
* @return the input stream connected to the normal output of the |
|
30899
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
139 |
* process |
2 | 140 |
*/ |
13149
27d52f97a5cc
4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
5506
diff
changeset
|
141 |
public abstract InputStream getInputStream(); |
2 | 142 |
|
143 |
/** |
|
48
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
144 |
* Returns the input stream connected to the error output of the |
30899
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
145 |
* process. The stream obtains data piped from the error output |
48
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
146 |
* of the process represented by this {@code Process} object. |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
147 |
* |
30899
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
148 |
* <p>If the standard error of the process has been redirected using |
48
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
149 |
* {@link ProcessBuilder#redirectError(Redirect) |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
150 |
* ProcessBuilder.redirectError} or |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
151 |
* {@link ProcessBuilder#redirectErrorStream(boolean) |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
152 |
* ProcessBuilder.redirectErrorStream} |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
153 |
* then this method will return a |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
154 |
* <a href="ProcessBuilder.html#redirect-output">null input stream</a>. |
2 | 155 |
* |
156 |
* <p>Implementation note: It is a good idea for the returned |
|
157 |
* input stream to be buffered. |
|
158 |
* |
|
48
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
159 |
* @return the input stream connected to the error output of |
30899
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
160 |
* the process |
2 | 161 |
*/ |
13149
27d52f97a5cc
4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
5506
diff
changeset
|
162 |
public abstract InputStream getErrorStream(); |
2 | 163 |
|
164 |
/** |
|
165 |
* Causes the current thread to wait, if necessary, until the |
|
166 |
* process represented by this {@code Process} object has |
|
30899
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
167 |
* terminated. This method returns immediately if the process |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
168 |
* has already terminated. If the process has not yet |
2 | 169 |
* terminated, the calling thread will be blocked until the |
30899
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
170 |
* process exits. |
2 | 171 |
* |
30899
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
172 |
* @return the exit value of the process represented by this |
2 | 173 |
* {@code Process} object. By convention, the value |
174 |
* {@code 0} indicates normal termination. |
|
175 |
* @throws InterruptedException if the current thread is |
|
176 |
* {@linkplain Thread#interrupt() interrupted} by another |
|
177 |
* thread while it is waiting, then the wait is ended and |
|
178 |
* an {@link InterruptedException} is thrown. |
|
179 |
*/ |
|
13149
27d52f97a5cc
4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
5506
diff
changeset
|
180 |
public abstract int waitFor() throws InterruptedException; |
27d52f97a5cc
4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
5506
diff
changeset
|
181 |
|
27d52f97a5cc
4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
5506
diff
changeset
|
182 |
/** |
27d52f97a5cc
4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
5506
diff
changeset
|
183 |
* Causes the current thread to wait, if necessary, until the |
30899
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
184 |
* process represented by this {@code Process} object has |
13149
27d52f97a5cc
4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
5506
diff
changeset
|
185 |
* terminated, or the specified waiting time elapses. |
27d52f97a5cc
4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
5506
diff
changeset
|
186 |
* |
30899
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
187 |
* <p>If the process has already terminated then this method returns |
13149
27d52f97a5cc
4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
5506
diff
changeset
|
188 |
* 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
|
189 |
* 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
|
190 |
* this method returns immediately with the value {@code false}. |
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 |
* <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
|
193 |
* 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
|
194 |
* 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
|
195 |
* efficient implementation. |
27d52f97a5cc
4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
5506
diff
changeset
|
196 |
* |
27d52f97a5cc
4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
5506
diff
changeset
|
197 |
* @param timeout the maximum time to wait |
27d52f97a5cc
4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
5506
diff
changeset
|
198 |
* @param unit the time unit of the {@code timeout} argument |
30899
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
199 |
* @return {@code true} if the process has exited and {@code false} if |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
200 |
* the waiting time elapsed before the process has exited. |
13149
27d52f97a5cc
4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
5506
diff
changeset
|
201 |
* @throws InterruptedException if the current thread is interrupted |
27d52f97a5cc
4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
5506
diff
changeset
|
202 |
* while waiting. |
27d52f97a5cc
4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
5506
diff
changeset
|
203 |
* @throws NullPointerException if unit is null |
27d52f97a5cc
4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
5506
diff
changeset
|
204 |
* @since 1.8 |
27d52f97a5cc
4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
5506
diff
changeset
|
205 |
*/ |
27d52f97a5cc
4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
5506
diff
changeset
|
206 |
public boolean waitFor(long timeout, TimeUnit unit) |
27d52f97a5cc
4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
5506
diff
changeset
|
207 |
throws InterruptedException |
27d52f97a5cc
4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
5506
diff
changeset
|
208 |
{ |
27d52f97a5cc
4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
5506
diff
changeset
|
209 |
long startTime = System.nanoTime(); |
27d52f97a5cc
4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
5506
diff
changeset
|
210 |
long rem = unit.toNanos(timeout); |
27d52f97a5cc
4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
5506
diff
changeset
|
211 |
|
27d52f97a5cc
4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
5506
diff
changeset
|
212 |
do { |
27d52f97a5cc
4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
5506
diff
changeset
|
213 |
try { |
27d52f97a5cc
4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
5506
diff
changeset
|
214 |
exitValue(); |
27d52f97a5cc
4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
5506
diff
changeset
|
215 |
return true; |
27d52f97a5cc
4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
5506
diff
changeset
|
216 |
} catch(IllegalThreadStateException ex) { |
27d52f97a5cc
4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
5506
diff
changeset
|
217 |
if (rem > 0) |
27d52f97a5cc
4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
5506
diff
changeset
|
218 |
Thread.sleep( |
27d52f97a5cc
4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
5506
diff
changeset
|
219 |
Math.min(TimeUnit.NANOSECONDS.toMillis(rem) + 1, 100)); |
27d52f97a5cc
4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
5506
diff
changeset
|
220 |
} |
27d52f97a5cc
4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
5506
diff
changeset
|
221 |
rem = unit.toNanos(timeout) - (System.nanoTime() - startTime); |
27d52f97a5cc
4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
5506
diff
changeset
|
222 |
} while (rem > 0); |
27d52f97a5cc
4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
5506
diff
changeset
|
223 |
return false; |
27d52f97a5cc
4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
5506
diff
changeset
|
224 |
} |
2 | 225 |
|
226 |
/** |
|
30899
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
227 |
* Returns the exit value for the process. |
2 | 228 |
* |
30899
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
229 |
* @return the exit value of the process represented by this |
2 | 230 |
* {@code Process} object. By convention, the value |
231 |
* {@code 0} indicates normal termination. |
|
30899
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
232 |
* @throws IllegalThreadStateException if the process represented |
2 | 233 |
* by this {@code Process} object has not yet terminated |
234 |
*/ |
|
13149
27d52f97a5cc
4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
5506
diff
changeset
|
235 |
public abstract int exitValue(); |
27d52f97a5cc
4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
5506
diff
changeset
|
236 |
|
27d52f97a5cc
4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
5506
diff
changeset
|
237 |
/** |
30899
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
238 |
* Kills the process. |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
239 |
* Whether the process represented by this {@code Process} object is |
44640
590dec7cadb4
8178347: Process and ProcessHandle getPid method name inconsistency
rriggs
parents:
39061
diff
changeset
|
240 |
* {@linkplain #supportsNormalTermination normally terminated} or not is |
13149
27d52f97a5cc
4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
5506
diff
changeset
|
241 |
* implementation dependent. |
30899
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
242 |
* Forcible process destruction is defined as the immediate termination of a |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
243 |
* process, whereas normal termination allows the process to shut down cleanly. |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
244 |
* If the process is not alive, no action is taken. |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
245 |
* <p> |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
246 |
* The {@link java.util.concurrent.CompletableFuture} from {@link #onExit} is |
44640
590dec7cadb4
8178347: Process and ProcessHandle getPid method name inconsistency
rriggs
parents:
39061
diff
changeset
|
247 |
* {@linkplain java.util.concurrent.CompletableFuture#complete completed} |
30899
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
248 |
* when the process has terminated. |
13149
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 |
public abstract void destroy(); |
2 | 251 |
|
252 |
/** |
|
30899
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
253 |
* Kills the process forcibly. The process represented by this |
2 | 254 |
* {@code Process} object is forcibly terminated. |
30899
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
255 |
* Forcible process destruction is defined as the immediate termination of a |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
256 |
* process, whereas normal termination allows the process to shut down cleanly. |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
257 |
* If the process is not alive, no action is taken. |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
258 |
* <p> |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
259 |
* The {@link java.util.concurrent.CompletableFuture} from {@link #onExit} is |
44640
590dec7cadb4
8178347: Process and ProcessHandle getPid method name inconsistency
rriggs
parents:
39061
diff
changeset
|
260 |
* {@linkplain java.util.concurrent.CompletableFuture#complete completed} |
30899
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
261 |
* when the process has terminated. |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
262 |
* <p> |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
263 |
* Invoking this method on {@code Process} objects returned by |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
264 |
* {@link ProcessBuilder#start} and {@link Runtime#exec} forcibly terminate |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
265 |
* the process. |
13149
27d52f97a5cc
4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
5506
diff
changeset
|
266 |
* |
30899
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
267 |
* @implSpec |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
268 |
* The default implementation of this method invokes {@link #destroy} |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
269 |
* and so may not forcibly terminate the process. |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
270 |
* @implNote |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
271 |
* Concrete implementations of this class are strongly encouraged to override |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
272 |
* this method with a compliant implementation. |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
273 |
* @apiNote |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
274 |
* The process may not terminate immediately. |
13149
27d52f97a5cc
4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
5506
diff
changeset
|
275 |
* 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
|
276 |
* after {@code destroyForcibly()} is called. This method |
27d52f97a5cc
4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
5506
diff
changeset
|
277 |
* may be chained to {@code waitFor()} if needed. |
27d52f97a5cc
4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
5506
diff
changeset
|
278 |
* |
27d52f97a5cc
4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
5506
diff
changeset
|
279 |
* @return the {@code Process} object representing the |
30899
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
280 |
* process forcibly destroyed |
13149
27d52f97a5cc
4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
5506
diff
changeset
|
281 |
* @since 1.8 |
2 | 282 |
*/ |
13149
27d52f97a5cc
4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
5506
diff
changeset
|
283 |
public Process destroyForcibly() { |
27d52f97a5cc
4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
5506
diff
changeset
|
284 |
destroy(); |
27d52f97a5cc
4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
5506
diff
changeset
|
285 |
return this; |
27d52f97a5cc
4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
5506
diff
changeset
|
286 |
} |
27d52f97a5cc
4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
5506
diff
changeset
|
287 |
|
27d52f97a5cc
4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
5506
diff
changeset
|
288 |
/** |
30899
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
289 |
* Returns {@code true} if the implementation of {@link #destroy} is to |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
290 |
* normally terminate the process, |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
291 |
* Returns {@code false} if the implementation of {@code destroy} |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
292 |
* forcibly and immediately terminates the process. |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
293 |
* <p> |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
294 |
* Invoking this method on {@code Process} objects returned by |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
295 |
* {@link ProcessBuilder#start} and {@link Runtime#exec} return |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
296 |
* {@code true} or {@code false} depending on the platform implementation. |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
297 |
* |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
298 |
* @implSpec |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
299 |
* This implementation throws an instance of |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
300 |
* {@link java.lang.UnsupportedOperationException} and performs no other action. |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
301 |
* |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
302 |
* @return {@code true} if the implementation of {@link #destroy} is to |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
303 |
* normally terminate the process; |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
304 |
* otherwise, {@link #destroy} forcibly terminates the process |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
305 |
* @throws UnsupportedOperationException if the Process implementation |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
306 |
* does not support this operation |
35302
e4d2275861c3
8136494: Update "@since 1.9" to "@since 9" to match java.version.specification
iris
parents:
33648
diff
changeset
|
307 |
* @since 9 |
30899
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
308 |
*/ |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
309 |
public boolean supportsNormalTermination() { |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
310 |
throw new UnsupportedOperationException(this.getClass() |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
311 |
+ ".supportsNormalTermination() not supported" ); |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
312 |
} |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
313 |
|
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
314 |
/** |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
315 |
* Tests whether the process represented by this {@code Process} is |
13149
27d52f97a5cc
4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
5506
diff
changeset
|
316 |
* alive. |
27d52f97a5cc
4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
5506
diff
changeset
|
317 |
* |
30899
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
318 |
* @return {@code true} if the process represented by this |
13149
27d52f97a5cc
4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
5506
diff
changeset
|
319 |
* {@code Process} object has not yet terminated. |
27d52f97a5cc
4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
5506
diff
changeset
|
320 |
* @since 1.8 |
27d52f97a5cc
4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
5506
diff
changeset
|
321 |
*/ |
27d52f97a5cc
4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
5506
diff
changeset
|
322 |
public boolean isAlive() { |
27d52f97a5cc
4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
5506
diff
changeset
|
323 |
try { |
27d52f97a5cc
4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
5506
diff
changeset
|
324 |
exitValue(); |
27d52f97a5cc
4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
5506
diff
changeset
|
325 |
return false; |
27d52f97a5cc
4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
5506
diff
changeset
|
326 |
} catch(IllegalThreadStateException e) { |
27d52f97a5cc
4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
5506
diff
changeset
|
327 |
return true; |
27d52f97a5cc
4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
5506
diff
changeset
|
328 |
} |
27d52f97a5cc
4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
5506
diff
changeset
|
329 |
} |
24577 | 330 |
|
331 |
/** |
|
30899
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
332 |
* Returns the native process ID of the process. |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
333 |
* The native process ID is an identification number that the operating |
24577 | 334 |
* system assigns to the process. |
335 |
* |
|
30899
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
336 |
* @implSpec |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
337 |
* The implementation of this method returns the process id as: |
44640
590dec7cadb4
8178347: Process and ProcessHandle getPid method name inconsistency
rriggs
parents:
39061
diff
changeset
|
338 |
* {@link #toHandle toHandle().pid()}. |
30899
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
339 |
* |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
340 |
* @return the native process id of the process |
24577 | 341 |
* @throws UnsupportedOperationException if the Process implementation |
30899
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
342 |
* does not support this operation |
35302
e4d2275861c3
8136494: Update "@since 1.9" to "@since 9" to match java.version.specification
iris
parents:
33648
diff
changeset
|
343 |
* @since 9 |
24577 | 344 |
*/ |
44640
590dec7cadb4
8178347: Process and ProcessHandle getPid method name inconsistency
rriggs
parents:
39061
diff
changeset
|
345 |
public long pid() { |
590dec7cadb4
8178347: Process and ProcessHandle getPid method name inconsistency
rriggs
parents:
39061
diff
changeset
|
346 |
return toHandle().pid(); |
30899
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
347 |
} |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
348 |
|
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
349 |
/** |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
350 |
* Returns a {@code CompletableFuture<Process>} for the termination of the Process. |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
351 |
* The {@link java.util.concurrent.CompletableFuture} provides the ability |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
352 |
* to trigger dependent functions or actions that may be run synchronously |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
353 |
* or asynchronously upon process termination. |
33648
9564031a20e0
8138566: (Process) java.lang.Process.allChildren specification clarification
rriggs
parents:
32764
diff
changeset
|
354 |
* When the process has terminated the CompletableFuture is |
30899
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
355 |
* {@link java.util.concurrent.CompletableFuture#complete completed} regardless |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
356 |
* of the exit status of the process. |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
357 |
* <p> |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
358 |
* Calling {@code onExit().get()} waits for the process to terminate and returns |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
359 |
* the Process. The future can be used to check if the process is |
44640
590dec7cadb4
8178347: Process and ProcessHandle getPid method name inconsistency
rriggs
parents:
39061
diff
changeset
|
360 |
* {@linkplain java.util.concurrent.CompletableFuture#isDone done} or to |
590dec7cadb4
8178347: Process and ProcessHandle getPid method name inconsistency
rriggs
parents:
39061
diff
changeset
|
361 |
* {@linkplain java.util.concurrent.CompletableFuture#get() wait} for it to terminate. |
590dec7cadb4
8178347: Process and ProcessHandle getPid method name inconsistency
rriggs
parents:
39061
diff
changeset
|
362 |
* {@linkplain java.util.concurrent.CompletableFuture#cancel(boolean) Cancelling} |
30899
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
363 |
* the CompletableFuture does not affect the Process. |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
364 |
* <p> |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
365 |
* Processes returned from {@link ProcessBuilder#start} override the |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
366 |
* default implementation to provide an efficient mechanism to wait |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
367 |
* for process exit. |
31061 | 368 |
* |
30899
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
369 |
* @apiNote |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
370 |
* Using {@link #onExit() onExit} is an alternative to |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
371 |
* {@link #waitFor() waitFor} that enables both additional concurrency |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
372 |
* and convenient access to the result of the Process. |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
373 |
* Lambda expressions can be used to evaluate the result of the Process |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
374 |
* execution. |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
375 |
* If there is other processing to be done before the value is used |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
376 |
* then {@linkplain #onExit onExit} is a convenient mechanism to |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
377 |
* free the current thread and block only if and when the value is needed. |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
378 |
* <br> |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
379 |
* For example, launching a process to compare two files and get a boolean if they are identical: |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
380 |
* <pre> {@code Process p = new ProcessBuilder("cmp", "f1", "f2").start(); |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
381 |
* Future<Boolean> identical = p.onExit().thenApply(p1 -> p1.exitValue() == 0); |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
382 |
* ... |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
383 |
* if (identical.get()) { ... } |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
384 |
* }</pre> |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
385 |
* |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
386 |
* @implSpec |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
387 |
* This implementation executes {@link #waitFor()} in a separate thread |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
388 |
* repeatedly until it returns successfully. If the execution of |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
389 |
* {@code waitFor} is interrupted, the thread's interrupt status is preserved. |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
390 |
* <p> |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
391 |
* When {@link #waitFor()} returns successfully the CompletableFuture is |
44640
590dec7cadb4
8178347: Process and ProcessHandle getPid method name inconsistency
rriggs
parents:
39061
diff
changeset
|
392 |
* {@linkplain java.util.concurrent.CompletableFuture#complete completed} regardless |
30899
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
393 |
* of the exit status of the process. |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
394 |
* |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
395 |
* This implementation may consume a lot of memory for thread stacks if a |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
396 |
* large number of processes are waited for concurrently. |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
397 |
* <p> |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
398 |
* External implementations should override this method and provide |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
399 |
* a more efficient implementation. For example, to delegate to the underlying |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
400 |
* process, it can do the following: |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
401 |
* <pre>{@code |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
402 |
* public CompletableFuture<Process> onExit() { |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
403 |
* return delegate.onExit().thenApply(p -> this); |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
404 |
* } |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
405 |
* }</pre> |
33648
9564031a20e0
8138566: (Process) java.lang.Process.allChildren specification clarification
rriggs
parents:
32764
diff
changeset
|
406 |
* @apiNote |
9564031a20e0
8138566: (Process) java.lang.Process.allChildren specification clarification
rriggs
parents:
32764
diff
changeset
|
407 |
* The process may be observed to have terminated with {@link #isAlive} |
9564031a20e0
8138566: (Process) java.lang.Process.allChildren specification clarification
rriggs
parents:
32764
diff
changeset
|
408 |
* before the ComputableFuture is completed and dependent actions are invoked. |
30899
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
409 |
* |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
410 |
* @return a new {@code CompletableFuture<Process>} for the Process |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
411 |
* |
35302
e4d2275861c3
8136494: Update "@since 1.9" to "@since 9" to match java.version.specification
iris
parents:
33648
diff
changeset
|
412 |
* @since 9 |
30899
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
413 |
*/ |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
414 |
public CompletableFuture<Process> onExit() { |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
415 |
return CompletableFuture.supplyAsync(this::waitForInternal); |
24577 | 416 |
} |
30899
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
417 |
|
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
418 |
/** |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
419 |
* Wait for the process to exit by calling {@code waitFor}. |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
420 |
* If the thread is interrupted, remember the interrupted state to |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
421 |
* be restored before returning. Use ForkJoinPool.ManagedBlocker |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
422 |
* so that the number of workers in case ForkJoinPool is used is |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
423 |
* compensated when the thread blocks in waitFor(). |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
424 |
* |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
425 |
* @return the Process |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
426 |
*/ |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
427 |
private Process waitForInternal() { |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
428 |
boolean interrupted = false; |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
429 |
while (true) { |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
430 |
try { |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
431 |
ForkJoinPool.managedBlock(new ForkJoinPool.ManagedBlocker() { |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
432 |
@Override |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
433 |
public boolean block() throws InterruptedException { |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
434 |
waitFor(); |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
435 |
return true; |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
436 |
} |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
437 |
|
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
438 |
@Override |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
439 |
public boolean isReleasable() { |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
440 |
return !isAlive(); |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
441 |
} |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
442 |
}); |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
443 |
break; |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
444 |
} catch (InterruptedException x) { |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
445 |
interrupted = true; |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
446 |
} |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
447 |
} |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
448 |
if (interrupted) { |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
449 |
Thread.currentThread().interrupt(); |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
450 |
} |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
451 |
return this; |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
452 |
} |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
453 |
|
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
454 |
/** |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
455 |
* Returns a ProcessHandle for the Process. |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
456 |
* |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
457 |
* {@code Process} objects returned by {@link ProcessBuilder#start} and |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
458 |
* {@link Runtime#exec} implement {@code toHandle} as the equivalent of |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
459 |
* {@link ProcessHandle#of(long) ProcessHandle.of(pid)} including the |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
460 |
* check for a SecurityManager and {@code RuntimePermission("manageProcess")}. |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
461 |
* |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
462 |
* @implSpec |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
463 |
* This implementation throws an instance of |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
464 |
* {@link java.lang.UnsupportedOperationException} and performs no other action. |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
465 |
* Subclasses should override this method to provide a ProcessHandle for the |
44640
590dec7cadb4
8178347: Process and ProcessHandle getPid method name inconsistency
rriggs
parents:
39061
diff
changeset
|
466 |
* process. The methods {@link #pid}, {@link #info}, {@link #children}, |
33648
9564031a20e0
8138566: (Process) java.lang.Process.allChildren specification clarification
rriggs
parents:
32764
diff
changeset
|
467 |
* and {@link #descendants}, unless overridden, operate on the ProcessHandle. |
30899
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
468 |
* |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
469 |
* @return Returns a ProcessHandle for the Process |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
470 |
* @throws UnsupportedOperationException if the Process implementation |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
471 |
* does not support this operation |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
472 |
* @throws SecurityException if a security manager has been installed and |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
473 |
* it denies RuntimePermission("manageProcess") |
35302
e4d2275861c3
8136494: Update "@since 1.9" to "@since 9" to match java.version.specification
iris
parents:
33648
diff
changeset
|
474 |
* @since 9 |
30899
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
475 |
*/ |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
476 |
public ProcessHandle toHandle() { |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
477 |
throw new UnsupportedOperationException(this.getClass() |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
478 |
+ ".toHandle() not supported"); |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
479 |
} |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
480 |
|
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
481 |
/** |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
482 |
* Returns a snapshot of information about the process. |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
483 |
* |
33648
9564031a20e0
8138566: (Process) java.lang.Process.allChildren specification clarification
rriggs
parents:
32764
diff
changeset
|
484 |
* <p> A {@link ProcessHandle.Info} instance has accessor methods |
9564031a20e0
8138566: (Process) java.lang.Process.allChildren specification clarification
rriggs
parents:
32764
diff
changeset
|
485 |
* that return information about the process if it is available. |
30899
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
486 |
* |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
487 |
* @implSpec |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
488 |
* This implementation returns information about the process as: |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
489 |
* {@link #toHandle toHandle().info()}. |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
490 |
* |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
491 |
* @return a snapshot of information about the process, always non-null |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
492 |
* @throws UnsupportedOperationException if the Process implementation |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
493 |
* does not support this operation |
35302
e4d2275861c3
8136494: Update "@since 1.9" to "@since 9" to match java.version.specification
iris
parents:
33648
diff
changeset
|
494 |
* @since 9 |
30899
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
495 |
*/ |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
496 |
public ProcessHandle.Info info() { |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
497 |
return toHandle().info(); |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
498 |
} |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
499 |
|
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
500 |
/** |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
501 |
* Returns a snapshot of the direct children of the process. |
32764
1586fc6697da
8132883: The spec of allChildren/children of j.l.Process/ProcessHandle need to be relaxed
rriggs
parents:
31061
diff
changeset
|
502 |
* The parent of a direct child process is the process. |
44640
590dec7cadb4
8178347: Process and ProcessHandle getPid method name inconsistency
rriggs
parents:
39061
diff
changeset
|
503 |
* Typically, a process that is {@linkplain #isAlive not alive} has no children. |
30899
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
504 |
* <p> |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
505 |
* <em>Note that processes are created and terminate asynchronously. |
44640
590dec7cadb4
8178347: Process and ProcessHandle getPid method name inconsistency
rriggs
parents:
39061
diff
changeset
|
506 |
* There is no guarantee that a process is {@linkplain #isAlive alive}. |
30899
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
507 |
* </em> |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
508 |
* |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
509 |
* @implSpec |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
510 |
* This implementation returns the direct children as: |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
511 |
* {@link #toHandle toHandle().children()}. |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
512 |
* |
32764
1586fc6697da
8132883: The spec of allChildren/children of j.l.Process/ProcessHandle need to be relaxed
rriggs
parents:
31061
diff
changeset
|
513 |
* @return a sequential Stream of ProcessHandles for processes that are |
1586fc6697da
8132883: The spec of allChildren/children of j.l.Process/ProcessHandle need to be relaxed
rriggs
parents:
31061
diff
changeset
|
514 |
* direct children of the process |
30899
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
515 |
* @throws UnsupportedOperationException if the Process implementation |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
516 |
* does not support this operation |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
517 |
* @throws SecurityException if a security manager has been installed and |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
518 |
* it denies RuntimePermission("manageProcess") |
35302
e4d2275861c3
8136494: Update "@since 1.9" to "@since 9" to match java.version.specification
iris
parents:
33648
diff
changeset
|
519 |
* @since 9 |
30899
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
520 |
*/ |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
521 |
public Stream<ProcessHandle> children() { |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
522 |
return toHandle().children(); |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
523 |
} |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
524 |
|
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
525 |
/** |
33648
9564031a20e0
8138566: (Process) java.lang.Process.allChildren specification clarification
rriggs
parents:
32764
diff
changeset
|
526 |
* Returns a snapshot of the descendants of the process. |
9564031a20e0
8138566: (Process) java.lang.Process.allChildren specification clarification
rriggs
parents:
32764
diff
changeset
|
527 |
* The descendants of a process are the children of the process |
9564031a20e0
8138566: (Process) java.lang.Process.allChildren specification clarification
rriggs
parents:
32764
diff
changeset
|
528 |
* plus the descendants of those children, recursively. |
44640
590dec7cadb4
8178347: Process and ProcessHandle getPid method name inconsistency
rriggs
parents:
39061
diff
changeset
|
529 |
* Typically, a process that is {@linkplain #isAlive not alive} has no children. |
30899
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
530 |
* <p> |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
531 |
* <em>Note that processes are created and terminate asynchronously. |
44640
590dec7cadb4
8178347: Process and ProcessHandle getPid method name inconsistency
rriggs
parents:
39061
diff
changeset
|
532 |
* There is no guarantee that a process is {@linkplain #isAlive alive}. |
30899
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
533 |
* </em> |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
534 |
* |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
535 |
* @implSpec |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
536 |
* This implementation returns all children as: |
33648
9564031a20e0
8138566: (Process) java.lang.Process.allChildren specification clarification
rriggs
parents:
32764
diff
changeset
|
537 |
* {@link #toHandle toHandle().descendants()}. |
30899
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
538 |
* |
33648
9564031a20e0
8138566: (Process) java.lang.Process.allChildren specification clarification
rriggs
parents:
32764
diff
changeset
|
539 |
* @return a sequential Stream of ProcessHandles for processes that |
9564031a20e0
8138566: (Process) java.lang.Process.allChildren specification clarification
rriggs
parents:
32764
diff
changeset
|
540 |
* are descendants of the process |
30899
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
541 |
* @throws UnsupportedOperationException if the Process implementation |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
542 |
* does not support this operation |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
543 |
* @throws SecurityException if a security manager has been installed and |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
544 |
* it denies RuntimePermission("manageProcess") |
35302
e4d2275861c3
8136494: Update "@since 1.9" to "@since 9" to match java.version.specification
iris
parents:
33648
diff
changeset
|
545 |
* @since 9 |
30899
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
546 |
*/ |
33648
9564031a20e0
8138566: (Process) java.lang.Process.allChildren specification clarification
rriggs
parents:
32764
diff
changeset
|
547 |
public Stream<ProcessHandle> descendants() { |
9564031a20e0
8138566: (Process) java.lang.Process.allChildren specification clarification
rriggs
parents:
32764
diff
changeset
|
548 |
return toHandle().descendants(); |
30899
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
549 |
} |
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
550 |
|
39061
0a7abfc4f4b0
8155808: Process.getInputStream().skip() method is faulty
rriggs
parents:
35302
diff
changeset
|
551 |
/** |
0a7abfc4f4b0
8155808: Process.getInputStream().skip() method is faulty
rriggs
parents:
35302
diff
changeset
|
552 |
* An input stream for a subprocess pipe that skips by reading bytes |
0a7abfc4f4b0
8155808: Process.getInputStream().skip() method is faulty
rriggs
parents:
35302
diff
changeset
|
553 |
* instead of seeking, the underlying pipe does not support seek. |
0a7abfc4f4b0
8155808: Process.getInputStream().skip() method is faulty
rriggs
parents:
35302
diff
changeset
|
554 |
*/ |
0a7abfc4f4b0
8155808: Process.getInputStream().skip() method is faulty
rriggs
parents:
35302
diff
changeset
|
555 |
static class PipeInputStream extends FileInputStream { |
30899
d2408e757489
8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
25859
diff
changeset
|
556 |
|
39061
0a7abfc4f4b0
8155808: Process.getInputStream().skip() method is faulty
rriggs
parents:
35302
diff
changeset
|
557 |
PipeInputStream(FileDescriptor fd) { |
0a7abfc4f4b0
8155808: Process.getInputStream().skip() method is faulty
rriggs
parents:
35302
diff
changeset
|
558 |
super(fd); |
0a7abfc4f4b0
8155808: Process.getInputStream().skip() method is faulty
rriggs
parents:
35302
diff
changeset
|
559 |
} |
0a7abfc4f4b0
8155808: Process.getInputStream().skip() method is faulty
rriggs
parents:
35302
diff
changeset
|
560 |
|
0a7abfc4f4b0
8155808: Process.getInputStream().skip() method is faulty
rriggs
parents:
35302
diff
changeset
|
561 |
@Override |
0a7abfc4f4b0
8155808: Process.getInputStream().skip() method is faulty
rriggs
parents:
35302
diff
changeset
|
562 |
public long skip(long n) throws IOException { |
0a7abfc4f4b0
8155808: Process.getInputStream().skip() method is faulty
rriggs
parents:
35302
diff
changeset
|
563 |
long remaining = n; |
0a7abfc4f4b0
8155808: Process.getInputStream().skip() method is faulty
rriggs
parents:
35302
diff
changeset
|
564 |
int nr; |
0a7abfc4f4b0
8155808: Process.getInputStream().skip() method is faulty
rriggs
parents:
35302
diff
changeset
|
565 |
|
0a7abfc4f4b0
8155808: Process.getInputStream().skip() method is faulty
rriggs
parents:
35302
diff
changeset
|
566 |
if (n <= 0) { |
0a7abfc4f4b0
8155808: Process.getInputStream().skip() method is faulty
rriggs
parents:
35302
diff
changeset
|
567 |
return 0; |
0a7abfc4f4b0
8155808: Process.getInputStream().skip() method is faulty
rriggs
parents:
35302
diff
changeset
|
568 |
} |
0a7abfc4f4b0
8155808: Process.getInputStream().skip() method is faulty
rriggs
parents:
35302
diff
changeset
|
569 |
|
0a7abfc4f4b0
8155808: Process.getInputStream().skip() method is faulty
rriggs
parents:
35302
diff
changeset
|
570 |
int size = (int)Math.min(2048, remaining); |
0a7abfc4f4b0
8155808: Process.getInputStream().skip() method is faulty
rriggs
parents:
35302
diff
changeset
|
571 |
byte[] skipBuffer = new byte[size]; |
0a7abfc4f4b0
8155808: Process.getInputStream().skip() method is faulty
rriggs
parents:
35302
diff
changeset
|
572 |
while (remaining > 0) { |
0a7abfc4f4b0
8155808: Process.getInputStream().skip() method is faulty
rriggs
parents:
35302
diff
changeset
|
573 |
nr = read(skipBuffer, 0, (int)Math.min(size, remaining)); |
0a7abfc4f4b0
8155808: Process.getInputStream().skip() method is faulty
rriggs
parents:
35302
diff
changeset
|
574 |
if (nr < 0) { |
0a7abfc4f4b0
8155808: Process.getInputStream().skip() method is faulty
rriggs
parents:
35302
diff
changeset
|
575 |
break; |
0a7abfc4f4b0
8155808: Process.getInputStream().skip() method is faulty
rriggs
parents:
35302
diff
changeset
|
576 |
} |
0a7abfc4f4b0
8155808: Process.getInputStream().skip() method is faulty
rriggs
parents:
35302
diff
changeset
|
577 |
remaining -= nr; |
0a7abfc4f4b0
8155808: Process.getInputStream().skip() method is faulty
rriggs
parents:
35302
diff
changeset
|
578 |
} |
0a7abfc4f4b0
8155808: Process.getInputStream().skip() method is faulty
rriggs
parents:
35302
diff
changeset
|
579 |
|
0a7abfc4f4b0
8155808: Process.getInputStream().skip() method is faulty
rriggs
parents:
35302
diff
changeset
|
580 |
return n - remaining; |
0a7abfc4f4b0
8155808: Process.getInputStream().skip() method is faulty
rriggs
parents:
35302
diff
changeset
|
581 |
} |
0a7abfc4f4b0
8155808: Process.getInputStream().skip() method is faulty
rriggs
parents:
35302
diff
changeset
|
582 |
} |
2 | 583 |
} |