How to connect the moment when the stream was destroyed

Is there any place to connect when the thread was killed? Sort of:

onDestroy{ //do something... } 

EDIT: Sorry. I should have stated this more clearly.

The termination of the flow occurs not because the entire task is completed, but because it was killed by the client code using ThreadGroup.destroy() . Since my singleton is created in client code, it will belong to the client's TradeGroup code and will be killed as a result. (Actually, I'm not very sure about the last sentence ...)

+4
source share
5 answers

You can wrap both an action and a hook in this way.

 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(); 
+7
source

There is no general way to add an asynchronous listener to a thread that receives a notification when the thread dies. You can use the join method to wait for a stream, but this is synchronous. Or you can create a stream class extension that calls listeners at the end of the method for starting it, for example:

 public abstract class NotifyingThread extends Thread { private final List<ThreadListeners> listeners; protected abstract void doRun(); public void run() { doRun(); notifyListeners(); } } 
+5
source

Killed or interrupted?

If you observe an attempt to interrupt your thread, the easiest way to do this 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 } } 
+3
source

@Jeff is correct.

... and someone will be erroneously destroyed.

The threads are not "killed" or "destroyed." They cease as a result of actions that they take themselves. Two flow completion scenarios:

  • The run() method is returned. This usually happens by design; those. because the method completed everything that it was intended to be executed.

  • The run() method throws or throws an exception. This usually happens due to some error in the thread that throws an unexpected exception.

(I ignore the possibility that some JVMs may implement the deprecated Thread.destroy() method, and someone might be so crazy as to call it.)

This does not mean that stopping the monitoring flow is not a useful task. Just the reason for the termination of the flow is different from what you put forward in your question / comments.

+2
source

Perhaps you could start a thread in another thread group. This way your code will not run in the Xlet thread group, so they will not be able to terminate the thread.

0
source

All Articles