APIdia Launch Partner
Top Description Inners Fields Constructors Methods
java.lang

pack-priv final Class ProcessHandleImpl

extends Object
implements ProcessHandle
Class Inheritance
All Implemented Interfaces
java.lang.ProcessHandle, java.lang.Comparable
Annotations
@ValueBased
Imports
jdk.internal.misc.InnocuousThread, java.lang.annotation.Native, java.security.AccessController, .PrivilegedAction, java.time.Duration, .Instant, java.util.Arrays, .Optional, java.util.concurrent.CompletableFuture, .ConcurrentHashMap, .ConcurrentMap, .Executor, .Executors, .ThreadFactory, .ThreadLocalRandom, java.util.stream.IntStream, .Stream

ProcessHandleImpl is the implementation of ProcessHandle.
Since
9
See Also
Process

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
pack-priv static class
ProcessHandleImpl.Info

Implementation of ProcessHandle.Info.

Field Summary

Modifier and TypeField and Description
private static final ConcurrentMap<Long, ProcessHandleImpl.ExitCompletion>
completions

Map of pids to ExitCompletions.

private static final ProcessHandleImpl
current

Cache the ProcessHandle of this process.

private static final int
NOT_A_CHILD

Return value from waitForProcessExit0 indicating the process is not a child.

private final long
pid

The pid of this ProcessHandle.

private static final Executor
processReaperExecutor

The thread pool of "process reaper" daemon threads.

private static final long
REAPER_DEFAULT_STACKSIZE

Default size of stack for reaper processes.

private final long
startTime

The start time of this process.

private static final long
private static final long

Constructor Summary

AccessConstructor and Description
private
ProcessHandleImpl(long
the pid for this instance
pid
,
long startTime)

Private constructor.

Method Summary

Modifier and TypeMethod and Description
public Stream<ProcessHandle>
children()

Implements java.lang.ProcessHandle.children.

Returns a snapshot of the current direct children of the process.

pack-priv static Stream<ProcessHandle>

Returns:

a stream of ProcessHandles
children
(long
the pid of the process for which to find the children; 0 for all processes
pid
)

Returns a Stream of the children of a process or all processes.

public int
compareTo(ProcessHandle other)

Implements java.lang.ProcessHandle.compareTo.

Compares this ProcessHandle with the specified ProcessHandle for order.

pack-priv static CompletableFuture<Integer>
completion(long pid, boolean
true if the exit value should be reaped
shouldReap
)

Returns a CompletableFuture that completes with process exit status when the process completes.

public static ProcessHandleImpl

Returns:

The ProcessHandle for the OS process.
current
()

Hides java.lang.ProcessHandle.current.

Returns the ProcessHandle for the current native process.

public Stream<ProcessHandle>
descendants()

Implements java.lang.ProcessHandle.descendants.

Returns a snapshot of the descendants of the process.

public boolean
destroy()

Implements java.lang.ProcessHandle.destroy.

Requests the process to be killed.

private static native boolean

Returns:

true if the process was signaled without error; false otherwise
destroy0
(long
process id to kill
pid
,
long
the start time of the process
startTime
,
boolean
true to forcibly terminate (SIGKILL vs SIGTERM)
forcibly
)

Signal the process to terminate.

public boolean
destroyForcibly()

Implements java.lang.ProcessHandle.destroyForcibly.

Requests the process to be killed forcibly.

pack-priv boolean
destroyProcess(boolean
true if the process should be terminated forcibly; else false for a normal termination
force
)

Destroy the process for this ProcessHandle.

public boolean
equals(Object obj)

Overrides java.lang.Object.equals.

Implements java.lang.ProcessHandle.equals.

Returns true if other object is non-null, is of the same implementation, and represents the same system process; otherwise it returns false.

pack-priv static Optional<ProcessHandle>

Returns:

The ProcessHandle for the pid if the process is alive; or null if the process ID does not exist in the native system.
get
(long
the native process identifier
pid
)

Returns a ProcessHandle for an existing native process.

private static native long

Returns:

the pid of the current process
getCurrentPid0
()

Return the pid of the current process.

pack-priv static ProcessHandleImpl

Returns:

a ProcessHandle corresponding to an existing Process instance
getInternal
(long
of the known to exist process
pid
)

Returns a ProcessHandle for an existing native process known to be alive.

private static native int

Returns:

if greater than or equal to zero is the number of pids in the array; if greater than the length of the arrays, the arrays are too small
getProcessPids0
(long
if pid equals zero, then all known processes are returned; otherwise only direct child process pids are returned
pid
,
long[]
an allocated long array to receive the pids
pids
,
long[]
an allocated long array to receive the parent pids; may be null
ppids
,
long[]
an allocated long array to receive the child start times; may be null
starttimes
)

Returns the number of pids filled in to the array.

public int
hashCode()

Overrides java.lang.Object.hashCode.

Implements java.lang.ProcessHandle.hashCode.

Returns a hash code value for this ProcessHandle.

public ProcessHandle.Info
info()

Implements java.lang.ProcessHandle.info.

Returns a snapshot of information about the process.

private static native void
private static Executor
public boolean

Returns:

true if the process represented by this ProcessHandle object has not yet terminated.
isAlive
()

Implements java.lang.ProcessHandle.isAlive.

Tests whether the process represented by this ProcessHandle is alive.

private static native long

Returns:

the start time in milliseconds since 1970, 0 if the start time cannot be determined, -1 if the pid does not exist.
isAlive0
(long
the pid to check
pid
)

Returns the process start time depending on whether the pid is alive.

public CompletableFuture<ProcessHandle>
onExit()

Implements java.lang.ProcessHandle.onExit.

Returns a CompletableFuture<ProcessHandle> for the termination of the process.

public Optional<ProcessHandle>

Returns:

a ProcessHandle of the parent process; null is returned if the child process does not have a parent
parent
()

Implements java.lang.ProcessHandle.parent.

Returns a ProcessHandle for the parent process.

private static native long

Returns:

the parent of the native pid; if any, otherwise -1
parent0
(long
the process id
pid
,
long
the startTime of the process
startTime
)

Returns the parent of the native pid argument.

public long

Returns:

the native process ID
pid
()

Implements java.lang.ProcessHandle.pid.

Returns the native process ID. A long is used to be able to fit the system specific binary values for the process.

public boolean
supportsNormalTermination()

Implements java.lang.ProcessHandle.supportsNormalTermination.

Returns true if the implementation of destroy normally terminates the process.

private static void
swap(long[] array, int x, int y)

public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.

private static native int

Returns:

the value or -1 if an error occurs
waitForProcessExit0
(long
the processId
pid
,
boolean
if true, the value is retrieved, else return the value and leave the process waitable
reapvalue
)

Wait for the process to exit, return the value.

Inherited from java.lang.Object:
clonefinalizegetClassnotifynotifyAllwaitwaitwait

Field Detail

completionsback to summary
private static final ConcurrentMap<Long, ProcessHandleImpl.ExitCompletion> completions

Map of pids to ExitCompletions.

currentback to summary
private static final ProcessHandleImpl current

Cache the ProcessHandle of this process.

NOT_A_CHILDback to summary
private static final int NOT_A_CHILD

Return value from waitForProcessExit0 indicating the process is not a child.

Annotations
@Native
pidback to summary
private final long pid

The pid of this ProcessHandle.

processReaperExecutorback to summary
private static final Executor processReaperExecutor

The thread pool of "process reaper" daemon threads.

REAPER_DEFAULT_STACKSIZEback to summary
private static final long REAPER_DEFAULT_STACKSIZE

Default size of stack for reaper processes.

startTimeback to summary
private final long startTime

The start time of this process. If STARTTIME_ANY, the start time of the process is not available from the os. If greater than zero, the start time of the process.

STARTTIME_ANYback to summary
private static final long STARTTIME_ANY
STARTTIME_PROCESS_UNKNOWNback to summary
private static final long STARTTIME_PROCESS_UNKNOWN

Constructor Detail

ProcessHandleImplback to summary
private ProcessHandleImpl(long pid, long startTime)

Private constructor. Instances are created by the get(long) factory.

Parameters
pid:long

the pid for this instance

Method Detail

childrenback to summary
public Stream<ProcessHandle> children()

Implements java.lang.ProcessHandle.children.

Doc from java.lang.ProcessHandle.children.

Returns a snapshot of the current direct children of the process. The parent of a direct child process is the process. Typically, a process that is not alive has no children.

Note that processes are created and terminate asynchronously. There is no guarantee that a process is alive.

Annotations
@Override
childrenback to summary
pack-priv static Stream<ProcessHandle> children(long pid)

Returns a Stream of the children of a process or all processes.

Parameters
pid:long

the pid of the process for which to find the children; 0 for all processes

Returns:Stream<ProcessHandle>

a stream of ProcessHandles

compareToback to summary
public int compareTo(ProcessHandle other)

Implements java.lang.ProcessHandle.compareTo.

Doc from java.lang.ProcessHandle.compareTo.

Compares this ProcessHandle with the specified ProcessHandle for order. The order is not specified, but is consistent with Object#equals, which returns true if and only if two instances of ProcessHandle are of the same implementation and represent the same system process. Comparison is only supported among objects of same implementation. If attempt is made to mutually compare two different implementations of ProcessHandles, ClassCastException is thrown.

Annotations
@Override
completionback to summary
pack-priv static CompletableFuture<Integer> completion(long pid, boolean shouldReap)

Returns a CompletableFuture that completes with process exit status when the process completes.

Parameters
shouldReap:boolean

true if the exit value should be reaped

currentback to summary
public static ProcessHandleImpl current()

Hides java.lang.ProcessHandle.current.

Returns the ProcessHandle for the current native process.

Returns:ProcessHandleImpl

The ProcessHandle for the OS process.

descendantsback to summary
public Stream<ProcessHandle> descendants()

Implements java.lang.ProcessHandle.descendants.

Doc from java.lang.ProcessHandle.descendants.

Returns a snapshot of the descendants of the process. The descendants of a process are the children of the process plus the descendants of those children, recursively. Typically, a process that is not alive has no children.

Note that processes are created and terminate asynchronously. There is no guarantee that a process is alive.

Annotations
@Override
destroyback to summary
public boolean destroy()

Implements java.lang.ProcessHandle.destroy.

Doc from java.lang.ProcessHandle.destroy.

Requests the process to be killed. Whether the process represented by this ProcessHandle object is normally terminated or not is implementation dependent. Forcible process destruction is defined as the immediate termination of the process, whereas normal termination allows the process to shut down cleanly. If the process is not alive, no action is taken. The operating system access controls may prevent the process from being killed.

The java.util.concurrent.CompletableFuture from onExit is completed when the process has terminated.

Note

The process may not terminate immediately. For example, isAlive() may return true for a brief period after destroy() is called.

Annotations
@Override
destroy0back to summary
private static native boolean destroy0(long pid, long startTime, boolean forcibly)

Signal the process to terminate. The process is signaled only if its start time matches the known start time.

Parameters
pid:long

process id to kill

startTime:long

the start time of the process

forcibly:boolean

true to forcibly terminate (SIGKILL vs SIGTERM)

Returns:boolean

true if the process was signaled without error; false otherwise

destroyForciblyback to summary
public boolean destroyForcibly()

Implements java.lang.ProcessHandle.destroyForcibly.

Doc from java.lang.ProcessHandle.destroyForcibly.

Requests the process to be killed forcibly. The process represented by this ProcessHandle object is forcibly terminated. Forcible process destruction is defined as the immediate termination of the process, whereas normal termination allows the process to shut down cleanly. If the process is not alive, no action is taken. The operating system access controls may prevent the process from being killed.

The java.util.concurrent.CompletableFuture from onExit is completed when the process has terminated.

Note

The process may not terminate immediately. For example, isAlive() may return true for a brief period after destroyForcibly() is called.

Annotations
@Override
destroyProcessback to summary
pack-priv boolean destroyProcess(boolean force)

Destroy the process for this ProcessHandle. The native code checks the start time before sending the termination request.

Parameters
force:boolean

true if the process should be terminated forcibly; else false for a normal termination

equalsback to summary
public boolean equals(Object obj)

Overrides java.lang.Object.equals.

Implements java.lang.ProcessHandle.equals.

Doc from java.lang.ProcessHandle.equals.

Returns true if other object is non-null, is of the same implementation, and represents the same system process; otherwise it returns false.

Annotations
@Override
getback to summary
pack-priv static Optional<ProcessHandle> get(long pid)

Returns a ProcessHandle for an existing native process.

Parameters
pid:long

the native process identifier

Returns:Optional<ProcessHandle>

The ProcessHandle for the pid if the process is alive; or null if the process ID does not exist in the native system.

getCurrentPid0back to summary
private static native long getCurrentPid0()

Return the pid of the current process.

Returns:long

the pid of the current process

getInternalback to summary
pack-priv static ProcessHandleImpl getInternal(long pid)

Returns a ProcessHandle for an existing native process known to be alive. The startTime of the process is retrieved and stored in the ProcessHandle. It does not perform a security check since it is called from ProcessImpl.

Parameters
pid:long

of the known to exist process

Returns:ProcessHandleImpl

a ProcessHandle corresponding to an existing Process instance

getProcessPids0back to summary
private static native int getProcessPids0(long pid, long[] pids, long[] ppids, long[] starttimes)

Returns the number of pids filled in to the array.

Parameters
pid:long

if pid equals zero, then all known processes are returned; otherwise only direct child process pids are returned

pids:long[]

an allocated long array to receive the pids

ppids:long[]

an allocated long array to receive the parent pids; may be null

starttimes:long[]

an allocated long array to receive the child start times; may be null

Returns:int

if greater than or equal to zero is the number of pids in the array; if greater than the length of the arrays, the arrays are too small

hashCodeback to summary
public int hashCode()

Overrides java.lang.Object.hashCode.

Implements java.lang.ProcessHandle.hashCode.

Doc from java.lang.ProcessHandle.hashCode.

Returns a hash code value for this ProcessHandle. The hashcode value follows the general contract for Object#hashCode(). The value is a function of the pid() value and may be a function of additional information to uniquely identify the process. If two ProcessHandles are equal according to the equals method, then calling the hashCode method on each of the two objects must produce the same integer result.

Annotations
@Override
infoback to summary
public ProcessHandle.Info info()

Implements java.lang.ProcessHandle.info.

Doc from java.lang.ProcessHandle.info.

Returns a snapshot of information about the process.

A ProcessHandle.Info instance has accessor methods that return information about the process if it is available.

Annotations
@Override
initNativeback to summary
private static native void initNative()
initReaperback to summary
private static Executor initReaper()
isAliveback to summary
public boolean isAlive()

Implements java.lang.ProcessHandle.isAlive.

Tests whether the process represented by this ProcessHandle is alive.

Returns:boolean

true if the process represented by this ProcessHandle object has not yet terminated.

Annotations
@Override
Since
9
isAlive0back to summary
private static native long isAlive0(long pid)

Returns the process start time depending on whether the pid is alive. This must not reap the exitValue.

Parameters
pid:long

the pid to check

Returns:long

the start time in milliseconds since 1970, 0 if the start time cannot be determined, -1 if the pid does not exist.

onExitback to summary
public CompletableFuture<ProcessHandle> onExit()

Implements java.lang.ProcessHandle.onExit.

Doc from java.lang.ProcessHandle.onExit.

Returns a CompletableFuture<ProcessHandle> for the termination of the process. The java.util.concurrent.CompletableFuture provides the ability to trigger dependent functions or actions that may be run synchronously or asynchronously upon process termination. When the process has terminated the CompletableFuture is completed regardless of the exit status of the process. The onExit method can be called multiple times to invoke independent actions when the process exits.

Calling onExit().get() waits for the process to terminate and returns the ProcessHandle. The future can be used to check if the process is done or to wait for it to terminate. Cancelling the CompletableFuture does not affect the Process.

Annotations
@Override
parentback to summary
public Optional<ProcessHandle> parent()

Implements java.lang.ProcessHandle.parent.

Returns a ProcessHandle for the parent process.

Returns:Optional<ProcessHandle>

a ProcessHandle of the parent process; null is returned if the child process does not have a parent

parent0back to summary
private static native long parent0(long pid, long startTime)

Returns the parent of the native pid argument.

Parameters
pid:long

the process id

startTime:long

the startTime of the process

Returns:long

the parent of the native pid; if any, otherwise -1

pidback to summary
public long pid()

Implements java.lang.ProcessHandle.pid.

Returns the native process ID. A long is used to be able to fit the system specific binary values for the process.

Returns:long

the native process ID

Annotations
@Override
supportsNormalTerminationback to summary
public boolean supportsNormalTermination()

Implements java.lang.ProcessHandle.supportsNormalTermination.

Doc from java.lang.ProcessHandle.supportsNormalTermination.

Returns true if the implementation of destroy normally terminates the process. Returns false if the implementation of destroy forcibly and immediately terminates the process.

Annotations
@Override
swapback to summary
private static void swap(long[] array, int x, int y)
toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object. Satisfying this method's contract implies a non-null result must be returned.

Annotations
@Override
waitForProcessExit0back to summary
private static native int waitForProcessExit0(long pid, boolean reapvalue)

Wait for the process to exit, return the value. Conditionally reap the value if requested

Parameters
pid:long

the processId

reapvalue:boolean

if true, the value is retrieved, else return the value and leave the process waitable

Returns:int

the value or -1 if an error occurs

java.lang back to summary

private Class ProcessHandleImpl.ExitCompletion

extends CompletableFuture<Integer>
Class Inheritance

Field Summary

Modifier and TypeField and Description
pack-priv final boolean

Constructor Summary

AccessConstructor and Description
pack-priv
ExitCompletion(boolean isReaping)

Method Summary

Inherited from java.util.concurrent.CompletableFuture:
acceptEitheracceptEitherAsyncacceptEitherAsyncallOfanyOfapplyToEitherapplyToEitherAsyncapplyToEitherAsynccancelcompletecompleteAsynccompleteAsynccompletedFuturecompletedStagecompleteExceptionallycompleteOnTimeoutcopydefaultExecutordelayedExecutordelayedExecutorexceptionallyexceptionallyAsyncexceptionallyAsyncexceptionallyComposeexceptionallyComposeAsyncexceptionallyComposeAsyncexceptionNowfailedFuturefailedStagegetgetgetNowgetNumberOfDependentshandlehandleAsynchandleAsyncisCancelledisCompletedExceptionallyisDonejoinminimalCompletionStagenewIncompleteFutureobtrudeExceptionobtrudeValueorTimeoutresultNowrunAfterBothrunAfterBothAsyncrunAfterBothAsyncrunAfterEitherrunAfterEitherAsyncrunAfterEitherAsyncrunAsyncrunAsyncstatesupplyAsyncsupplyAsyncthenAcceptthenAcceptAsyncthenAcceptAsyncthenAcceptBoththenAcceptBothAsyncthenAcceptBothAsyncthenApplythenApplyAsyncthenApplyAsyncthenCombinethenCombineAsyncthenCombineAsyncthenComposethenComposeAsyncthenComposeAsyncthenRunthenRunAsyncthenRunAsynctoCompletableFuturetoStringwhenCompletewhenCompleteAsyncwhenCompleteAsync

Field Detail

isReapingback to summary
pack-priv final boolean isReaping

Constructor Detail

ExitCompletionback to summary
pack-priv ExitCompletion(boolean isReaping)
java.lang back to summary

pack-priv Class ProcessHandleImpl.Info

extends Object
implements Info
Class Inheritance
All Implemented Interfaces
java.lang.ProcessHandle.Info

Implementation of ProcessHandle.Info. Information snapshot about a process. The attributes of a process vary by operating system and are not available in all implementations. Additionally, information about other processes is limited by the operating system privileges of the process making the request. If a value is not available, either a null or -1 is stored. The accessor methods return null if the value is not available.

Field Summary

Modifier and TypeField and Description
pack-priv String[]
pack-priv String
pack-priv String
pack-priv long
pack-priv long
pack-priv String

Constructor Summary

AccessConstructor and Description
pack-priv
Info()

Method Summary

Modifier and TypeMethod and Description
public Optional<String[]>
arguments()

Implements java.lang.ProcessHandle.Info.arguments.

Returns an array of Strings of the arguments of the process.

public Optional<String>
command()

Implements java.lang.ProcessHandle.Info.command.

Returns the executable pathname of the process.

public Optional<String>
commandLine()

Implements java.lang.ProcessHandle.Info.commandLine.

Returns the command line of the process.

public static ProcessHandle.Info

Returns:

ProcessHandle.Info non-null; individual fields may be null or -1 if not available.
info
(long
the native process identifier
pid
,
long
the startTime of the process being queried
startTime
)

Returns the Info object with the fields from the process.

private native void
info0(long
of the native process
pid
)

Fill in this Info instance with information about the native process.

private static native void
initIDs()

Initialization of JNI fieldIDs.

public Optional<Instant>
startInstant()

Implements java.lang.ProcessHandle.Info.startInstant.

Returns the start time of the process.

public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.

public Optional<Duration>
totalCpuDuration()

Implements java.lang.ProcessHandle.Info.totalCpuDuration.

Returns the total cputime accumulated of the process.

public Optional<String>
user()

Implements java.lang.ProcessHandle.Info.user.

Return the user of the process.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAllwaitwaitwait

Field Detail

argumentsback to summary
pack-priv String[] arguments
commandback to summary
pack-priv String command
commandLineback to summary
pack-priv String commandLine
startTimeback to summary
pack-priv long startTime
totalTimeback to summary
pack-priv long totalTime
userback to summary
pack-priv String user

Constructor Detail

Infoback to summary
pack-priv Info()

Method Detail

argumentsback to summary
public Optional<String[]> arguments()

Implements java.lang.ProcessHandle.Info.arguments.

Doc from java.lang.ProcessHandle.Info.arguments.

Returns an array of Strings of the arguments of the process.

Annotations
@Override
commandback to summary
public Optional<String> command()

Implements java.lang.ProcessHandle.Info.command.

Doc from java.lang.ProcessHandle.Info.command.

Returns the executable pathname of the process.

Annotations
@Override
commandLineback to summary
public Optional<String> commandLine()

Implements java.lang.ProcessHandle.Info.commandLine.

Doc from java.lang.ProcessHandle.Info.commandLine.

Returns the command line of the process.

If command() and arguments() return non-empty optionals, this is simply a convenience method which concatenates the values of the two functions separated by spaces. Otherwise, it will return a best-effort, platform dependent representation of the command line.

Annotations
@Override
infoback to summary
public static ProcessHandle.Info info(long pid, long startTime)

Returns the Info object with the fields from the process. Whatever fields are provided by native are returned. If the startTime of the process does not match the provided startTime then an empty Info is returned.

Parameters
pid:long

the native process identifier

startTime:long

the startTime of the process being queried

Returns:ProcessHandle.Info

ProcessHandle.Info non-null; individual fields may be null or -1 if not available.

info0back to summary
private native void info0(long pid)

Fill in this Info instance with information about the native process. If values are not available the native code does not modify the field.

Parameters
pid:long

of the native process

initIDsback to summary
private static native void initIDs()

Initialization of JNI fieldIDs.

startInstantback to summary
public Optional<Instant> startInstant()

Implements java.lang.ProcessHandle.Info.startInstant.

Doc from java.lang.ProcessHandle.Info.startInstant.

Returns the start time of the process.

Annotations
@Override
toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object. Satisfying this method's contract implies a non-null result must be returned.

Annotations
@Override
totalCpuDurationback to summary
public Optional<Duration> totalCpuDuration()

Implements java.lang.ProcessHandle.Info.totalCpuDuration.

Doc from java.lang.ProcessHandle.Info.totalCpuDuration.

Returns the total cputime accumulated of the process.

Annotations
@Override
userback to summary
public Optional<String> user()

Implements java.lang.ProcessHandle.Info.user.

Doc from java.lang.ProcessHandle.Info.user.

Return the user of the process.

Annotations
@Override