33 import java.util.concurrent.ConcurrentHashMap;
34 import java.util.concurrent.ConcurrentMap;
35 import java.util.concurrent.Executor;
36 import java.util.concurrent.Executors;
37 import java.util.concurrent.ForkJoinPool;
38 import java.util.concurrent.SynchronousQueue;
39 import java.util.concurrent.ThreadFactory;
40 import java.util.concurrent.ThreadPoolExecutor;
41 import java.util.concurrent.TimeUnit;
42 import java.util.stream.IntStream;
43 import java.util.stream.Stream;
44
45 import sun.misc.InnocuousThread;
46
47 import static java.security.AccessController.doPrivileged;
48
49 /**
50 * ProcessHandleImpl is the implementation of ProcessHandle.
51 *
52 * @see Process
53 * @since 1.9
54 */
55 final class ProcessHandleImpl implements ProcessHandle {
56 /**
57 * Cache the ProcessHandle of this process.
58 */
59 private static final ProcessHandleImpl current;
60
61 /**
62 * Map of pids to ExitCompletions.
63 */
64 private static final ConcurrentMap<Long, ExitCompletion>
65 completions = new ConcurrentHashMap<>();
66
67 static {
68 initNative();
69 long pid = getCurrentPid0();
70 current = new ProcessHandleImpl(pid, isAlive0(pid));
71 }
72
73 private static native void initNative();
321 public boolean destroy() {
322 return destroyProcess(false);
323 }
324
325 @Override
326 public boolean destroyForcibly() {
327 return destroyProcess(true);
328 }
329
330
331 @Override
332 public boolean supportsNormalTermination() {
333 return ProcessImpl.SUPPORTS_NORMAL_TERMINATION;
334 }
335
336 /**
337 * Tests whether the process represented by this {@code ProcessHandle} is alive.
338 *
339 * @return {@code true} if the process represented by this
340 * {@code ProcessHandle} object has not yet terminated.
341 * @since 1.9
342 */
343 @Override
344 public boolean isAlive() {
345 long start = isAlive0(pid);
346 return (start >= 0 && (start == startTime || start == 0 || startTime == 0));
347 }
348
349 /**
350 * Returns the process start time depending on whether the pid is alive.
351 * This must not reap the exitValue.
352 *
353 * @param pid the pid to check
354 * @return the start time in milliseconds since 1970,
355 * 0 if the start time cannot be determined,
356 * -1 if the pid does not exist.
357 */
358 private static native long isAlive0(long pid);
359
360 @Override
361 public Stream<ProcessHandle> children() {
|
33 import java.util.concurrent.ConcurrentHashMap;
34 import java.util.concurrent.ConcurrentMap;
35 import java.util.concurrent.Executor;
36 import java.util.concurrent.Executors;
37 import java.util.concurrent.ForkJoinPool;
38 import java.util.concurrent.SynchronousQueue;
39 import java.util.concurrent.ThreadFactory;
40 import java.util.concurrent.ThreadPoolExecutor;
41 import java.util.concurrent.TimeUnit;
42 import java.util.stream.IntStream;
43 import java.util.stream.Stream;
44
45 import sun.misc.InnocuousThread;
46
47 import static java.security.AccessController.doPrivileged;
48
49 /**
50 * ProcessHandleImpl is the implementation of ProcessHandle.
51 *
52 * @see Process
53 * @since 9
54 */
55 final class ProcessHandleImpl implements ProcessHandle {
56 /**
57 * Cache the ProcessHandle of this process.
58 */
59 private static final ProcessHandleImpl current;
60
61 /**
62 * Map of pids to ExitCompletions.
63 */
64 private static final ConcurrentMap<Long, ExitCompletion>
65 completions = new ConcurrentHashMap<>();
66
67 static {
68 initNative();
69 long pid = getCurrentPid0();
70 current = new ProcessHandleImpl(pid, isAlive0(pid));
71 }
72
73 private static native void initNative();
321 public boolean destroy() {
322 return destroyProcess(false);
323 }
324
325 @Override
326 public boolean destroyForcibly() {
327 return destroyProcess(true);
328 }
329
330
331 @Override
332 public boolean supportsNormalTermination() {
333 return ProcessImpl.SUPPORTS_NORMAL_TERMINATION;
334 }
335
336 /**
337 * Tests whether the process represented by this {@code ProcessHandle} is alive.
338 *
339 * @return {@code true} if the process represented by this
340 * {@code ProcessHandle} object has not yet terminated.
341 * @since 9
342 */
343 @Override
344 public boolean isAlive() {
345 long start = isAlive0(pid);
346 return (start >= 0 && (start == startTime || start == 0 || startTime == 0));
347 }
348
349 /**
350 * Returns the process start time depending on whether the pid is alive.
351 * This must not reap the exitValue.
352 *
353 * @param pid the pid to check
354 * @return the start time in milliseconds since 1970,
355 * 0 if the start time cannot be determined,
356 * -1 if the pid does not exist.
357 */
358 private static native long isAlive0(long pid);
359
360 @Override
361 public Stream<ProcessHandle> children() {
|