Methods Summary |
---|
public boolean | add(java.lang.Process process)Returns true if the specified Process was
successfully added to the list of processes to destroy upon VM exit.
synchronized (processes) {
// if this list is empty, register the shutdown hook
if (processes.size() == 0) {
addShutdownHook();
}
processes.addElement(process);
return processes.contains(process);
}
|
private void | addShutdownHook()Registers this ProcessDestroyer as a shutdown hook,
uses reflection to ensure pre-JDK 1.3 compatibility.
if (addShutdownHookMethod != null && !running) {
destroyProcessThread = new ProcessDestroyerImpl();
Object[] args = {destroyProcessThread};
try {
addShutdownHookMethod.invoke(Runtime.getRuntime(), args);
added = true;
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
Throwable t = e.getTargetException();
if (t != null && t.getClass() == IllegalStateException.class) {
// shutdown already is in progress
running = true;
} else {
e.printStackTrace();
}
}
}
|
public boolean | isAddedAsShutdownHook()Returns whether or not the ProcessDestroyer is registered as
as shutdown hook
return added;
|
public boolean | remove(java.lang.Process process)Returns true if the specified Process was
successfully removed from the list of processes to destroy upon VM exit.
synchronized (processes) {
boolean processRemoved = processes.removeElement(process);
if (processRemoved && processes.size() == 0) {
removeShutdownHook();
}
return processRemoved;
}
|
private void | removeShutdownHook()Removes this ProcessDestroyer as a shutdown hook,
uses reflection to ensure pre-JDK 1.3 compatibility
if (removeShutdownHookMethod != null && added && !running) {
Object[] args = {destroyProcessThread};
try {
Boolean removed =
(Boolean) removeShutdownHookMethod.invoke(
Runtime.getRuntime(),
args);
if (!removed.booleanValue()) {
System.err.println("Could not remove shutdown hook");
}
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
Throwable t = e.getTargetException();
if (t != null && t.getClass() == IllegalStateException.class) {
// shutdown already is in progress
running = true;
} else {
e.printStackTrace();
}
}
// start the hook thread, a unstarted thread may not be
// eligible for garbage collection
// Cf.: http://developer.java.sun.com/developer/bugParade/bugs/4533087.html
destroyProcessThread.setShouldDestroy(false);
if (!destroyProcessThread.getThreadGroup().isDestroyed()) {
// start() would throw IllegalThreadStateException from
// ThreadGroup.add if it were destroyed
destroyProcessThread.start();
}
// this should return quickly, since it basically is a NO-OP.
try {
destroyProcessThread.join(20000);
} catch (InterruptedException ie) {
// the thread didn't die in time
// it should not kill any processes unexpectedly
}
destroyProcessThread = null;
added = false;
}
|
public void | run()Invoked by the VM when it is exiting.
synchronized (processes) {
running = true;
Enumeration e = processes.elements();
while (e.hasMoreElements()) {
((Process) e.nextElement()).destroy();
}
}
|