how to hook up the moment when a Thread has been destroyed
Is there any place to hook up when a Thread has been killed? Something like :
onDestroy{
//do something...
}
EDIT: Sorry. I should have stated it more clearly.
The thread termination is not because all the job has done but because it has be开发者_如何学运维en killed by the client code using ThreadGroup.destroy()
. As my singleton is being instantiated in the client code, so it will belong to the THreadGroup of the Client code and be killed as a result. (Actually, I am not very sure of the last sentence...)
You can wrap both the action and the hook like this.
public final class HookOnDestroy implements Runnable {
private final Runnable action;
private final Runnable hook;
public HookOnDestroy(Runnable action, Runnable hook) {
this.hook = hook;
this.action = action;
}
@Override
public void run() {
try {
action.run();
} finally {
hook.run();
}
}
}
and
Runnable action = ...
Runnable hook = ...
new Thread( new HookOnDestroy(action,hook)).start();
There's no general way to add an asynchronous listener to a thread that gets notified when the thread dies. You can use the join
method to wait for a thread, but that is synchronous. Or you could create an extension of thread class that calls listeners at the end of its run method, something like:
public abstract class NotifyingThread extends Thread {
private final List<ThreadListeners> listeners;
protected abstract void doRun();
public void run() {
doRun();
notifyListeners();
}
}
Killed or interrupted?
If you are watching for an attempt to interrupt your thread, the easiest way to do so is to catch an InterruptedException. In super pseudo code:
// in my Thread
public void run() {
try {
boolean keepRunning = true;
while (keepRunning) {
// Do useful work
// Call other methods
// As long as they aren't also catching InterruptedException
}
System.err.println("Finished running since keepRunning is now false");
} catch (InterrruptedException ie) {
// This is where you would put your "onDestroy" functionality
}
}
@Jeff is correct.
... and it will be destroyed by some one mistakenly.
Threads don't get "killed" or "destroyed". They terminate as a result of actions that they take themselves. The two scenarios for a thread terminating are:
The
run()
method returns. This typically happens by design; i.e. because the method has finished whatever it was that it was designed to do.The
run()
method throws or propagates an exception. This typically happens as a result of some bug in the thread throwing an unexpected unchecked exception.
(I'm ignoring the possibility that some JVM might implement the deprecated Thread.destroy()
method, and someone might be crazy enough to call it.)
This is not to say that monitoring thread termination isn't a useful thing to do. Just that the cause of thread termination is different to what you are hypothesizing in your question / comments.
Maybe you could start a thread in a different thread group. So, your code would not be running in the Xlet thread group, therefore they would not be able to terminate the thread.
精彩评论