Class Daemon

java.lang.Object
com.peterphi.std.threading.Daemon
All Implemented Interfaces:
Runnable
Direct Known Subclasses:
GuiceDaemon

public abstract class Daemon extends Object implements Runnable
Simplifies the creation of long-running daemon threads
  • Constructor Details

    • Daemon

      public Daemon()
  • Method Details

    • startThread

      public Thread startThread() throws IllegalThreadStateException
      Starts this daemon, creating a new thread for it (the name of which will be set to the SimpleName of its class)
      Returns:
      Thread The daemon's thread
      Throws:
      IllegalThreadStateException - If the daemon is still running
    • getThreadName

      protected String getThreadName()
      Return the name for this thread
      Returns:
    • getThread

      public Thread getThread()
    • startThread

      public Thread startThread(String name) throws IllegalThreadStateException
      Starts this daemon, creating a new thread for it
      Parameters:
      name - String The name for the thread
      Returns:
      Thread The daemon's thread
      Throws:
      IllegalThreadStateException - If the daemon is still running
    • getTextState

      public String getTextState()
    • getTextStateUpdated

      public long getTextStateUpdated()
    • setTextState

      protected void setTextState(String value)
    • isRunning

      public boolean isRunning()
      Returns whether the daemon is in the process of terminating
      Returns:
      boolean True if the daemon is terminated or terminating, otherwise false
    • stopThread

      public void stopThread()
      Requests the daemon terminate by setting a flag and sending an interrupt to the thread
    • isThreadRunning

      public boolean isThreadRunning()
      Determines if this daemon's thread is alive
      Returns:
      boolean True if the thread is alive, otherwise false
    • shouldStartAsDaemon

      protected boolean shouldStartAsDaemon()
      Overloading this method to return true will start this daemon's thread as a Daemon Thread
      Returns:
      boolean True if the thread should be started as a daemon, otherwise false
    • stopAll

      public static void stopAll(List<? extends Daemon> daemons)
      Stop all the daemons in a given list:
      Parameters:
      daemons - List[Daemon] The daemons to stop
    • waitForTermination

      public static boolean waitForTermination(Daemon daemon, long timeoutMillis)
    • waitForTermination

      public static boolean waitForTermination(List<? extends Daemon> daemons, long timeoutMillis)