Class DaemonThread

java.lang.Object
java.lang.Thread
org.apache.sis.internal.system.DaemonThread
All Implemented Interfaces:
Runnable
Direct Known Subclasses:
DelayedExecutor, ReferenceQueueConsumer

abstract class DaemonThread extends Thread
Base class for all daemon threads in the SIS library. All DaemonThread instances are expected to run for the whole JVM lifetime (they are not executor threads). This class provides a isKillRequested() flag which shall be tested by the subclasses. It is okay to test this flag only when catching InterruptedException, as below:
Since:
0.3
Version:
0.3
  • Field Details

    • previous

      private final DaemonThread previous
      The previous element in a chain of DaemonThreads. We maintain a linked list of DaemonThread to be killed when killAll(DaemonThread, long) will be invoked. We do not rely on the thread listed by the Threads.DAEMONS group because in an OSGi context, we need to handle separately the threads created by each SIS module.
    • killRequested

      private volatile boolean killRequested
      Set to true when a kill is requested.
  • Constructor Details

    • DaemonThread

      protected DaemonThread(String name, DaemonThread lastCreatedDaemon)
      Creates a new daemon thread. This constructor sets the daemon flag to true. The thread will be allocated a small stack size on the assumption that it will execute only small and quick methods, without deep nesting levels.

      We need to maintain a list of daemon threads created by each SIS module in order to kill them at shutdown time (not strictly necessary for pure JSEE applications, but required in OSGi environment). Each module using DaemonThread shall maintain its own list (don't use the list of another module), like below:

      See ReferenceQueueConsumer for a real example.
      Parameters:
      name - the thread name.
      lastCreatedDaemon - the previous element in a chain of DaemonThreads, or null. Each SIS module shall maintain its own chain, if any.
  • Method Details

    • run

      public abstract void run()
      Must be overridden by subclass for performing the actual work.
      Specified by:
      run in interface Runnable
      Overrides:
      run in class Thread
    • isStalled

      protected boolean isStalled()
      Returns true if this thread seems to be blocked for a time long enough for suspecting a problem. The default implementation always returns false. Subclasses are encouraged to provide some problem detection mechanism here if they can. For example if the head of a queue seems to be never removed, then maybe the process consuming that queue is blocked.
      Returns:
      true if this thread seems to be stalled.
    • isKillRequested

      protected final boolean isKillRequested()
      Returns true if this daemon thread shall terminate. This happen at shutdown time.
      Returns:
      true if this daemon thread shall terminate.
    • killAll

      static void killAll(DaemonThread thread, long stopWaitingAt) throws InterruptedException
      Sends a kill signal to all threads in the chain starting by the given thread, and waits for the threads to die before to return.

      This method is for internal use by Apache SIS shutdown hooks only. Users should never invoke this method explicitly.

      Parameters:
      thread - the first thread in the chain of threads to kill.
      stopWaitingAt - a System.nanoTime() value telling when to stop waiting. This is used for preventing shutdown process to block an indefinite amount of time.
      Throws:
      InterruptedException - if another thread invoked Thread.interrupt() while we were waiting for the daemon threads to die.
      See Also:
    • listStalledThreads

      static List<Thread> listStalledThreads(DaemonThread thread)
      Returns the list of stalled or dead threads, or null if none. The returned list should always be null. A non-empty list would be a symptom for a severe problem, probably requiring an application reboot.

      This method is for internal use by Apache SIS only. Users should never invoke this method explicitly.

      Parameters:
      thread - the first thread in the chain of threads to verify.
      Returns:
      the list of stalled or dead threads, or null if none.