Class ThreadSignalHandler

All Implemented Interfaces:
SignalHandler, Action

public class ThreadSignalHandler
extends AbstractSignalHandler
An implementation of a signal handler that uses the Thread class to communicate signals back to the SignalTarget. This handler only supports the Signal.ABORT signal. When this signal is received, Thread.interrupt() is called on the thread that was registered with this handler. This should typically be a worker thread for some process. The worker thread should regularly check Thread.interrupted() to see if it has been interrupted. If the thread may be waiting for a blocking call to end, it should be able to handle the resulting InterruptedException that is thrown if the thread is interrupted.

If the worker thread becomes interrupted it should stop what it is doing, cleanup any allocated resources and exit in a timely fasion. Here is a general outline:

// ... code in worker thread
boolean done = false;
boolean interrupted = false;
while (!done && !interrupted)
      done = doSomeWork(); // NOTE! This must not take forever!
      interrupted = Thread.interrupted();
   catch (InterruptedException ex)
      // NOTE! Try-catch is only needed if thread calls a blocking method 
      interrupted = true;
if (interrupted)
Last modified
$Date: 2019-06-17 09:36:09 +0200 (mån, 17 juni 2019) $
  • Field Details

    • interruptHandler

      private static ThreadLocal<InterruptHandler> interruptHandler
      Keep per-thread interupt handlers. The default handler is ExceptionInterruptHandler which simply throws a SignalException if the current thread has been interrupted.
    • logger

      private static final org.slf4j.Logger logger
      Log signals processing.
    • supported

      private static final Set<Signal> supported
    • workerThread

      private Thread workerThread
      The worker thread that should be interrupted when Signal.ABORT is received.
    • forceStop

      private boolean forceStop
      If TRUE, call Thread.stop() instead of Thread.interrupt()
    • hasAborted

      private boolean hasAborted
      If TRUE, at least one ABORT signal has been recieved.
  • Constructor Details

    • ThreadSignalHandler

      public ThreadSignalHandler()
      Create a new thread signal handler. It will interrupt the current thread when it recieves the Signal.ABORT signal.
    • ThreadSignalHandler

      public ThreadSignalHandler​(Thread workerThread)
      Create a new thread signal handler.
      workerThread - The worker thread to interrupt when it receieves the Signal.ABORT signal, or null to interrupt the current thread
  • Method Details

    • checkInterrupted

      public static void checkInterrupted()
      Utility method to check if the current thread has been interrupted and (may) throw a SignalException if it has. NOTE! Since BASE 2.16 the actual behaviour of this method is determined by the InterruptHandler that has been registered for the current thread by setInterruptHandler(InterruptHandler). The default handler always throws a SignalException.
    • setInterruptHandler

      public static void setInterruptHandler​(InterruptHandler handler)
      Register an interrupt handler with the current thread. The handler is used by the checkInterrupted() method when the thread has been interrupted. If no handler has been registered with a thread the default action is to throw a SignalException.
      handler - An interrupt handler or null to remove a previously registered handler
    • handleSignal

      public void handleSignal​(Signal signal)
      Only handles the Signal.ABORT signal. When receieved, Thread.interrupt() is called on the worker thread that was registered when this object was created. Note that this method called from a different thread. The worker thread is busy doing something else. If the worker thread has already ended, this method does nothing.
      signal - The signal, only Signal.ABORT is accepted
      UnsupportedSignalException - If signal is anything else than Signal.ABORT
    • setWorkerThread

      public void setWorkerThread​(Thread workerThread)
      Set the worker thread that should be interrupted when a signal is receiver.
      workerThread - The worker thread, or null to use the current thread
    • setForceStop

      @Deprecated public void setForceStop​(boolean forceStop)
      In 3.14. Calling this is ignored and the signal handler will always use the interrupt method.
      Thread.stop() has been removed from the Java API.
    • hasReceived

      public boolean hasReceived​(Signal signal)
      Check if the given signal has been received by this signal handler.
      signal - The signal to check for
      TRUE if the signal has been received, FALSE otherwise