Class ThreadMonitor

  • All Implemented Interfaces:
    java.lang.Runnable

    final class ThreadMonitor
    extends java.lang.Object
    implements java.lang.Runnable
    Monitors a thread, interrupting it if it reaches the specified timeout.

    This works by sleeping until the specified timeout amount and then interrupting the thread being monitored. If the thread being monitored completes its work before being interrupted, it should interrupt() the monitor thread.

     Duration timeout = Duration.ofSeconds(1);
     try {
         Thread monitor = ThreadMonitor.start(timeout);
         // do some work here
         ThreadMonitor.stop(monitor);
     } catch (InterruptedException e) {
         // timed amount was reached
     }
     
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private java.lang.Thread thread  
      private java.time.Duration timeout  
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private ThreadMonitor​(java.lang.Thread thread, java.time.Duration timeout)
      Constructs a new monitor.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void run()
      Sleeps until the specified timeout amount and then interrupt the thread being monitored.
      (package private) static java.lang.Thread start​(java.lang.Thread thread, java.time.Duration timeout)
      Starts monitoring the specified thread.
      (package private) static java.lang.Thread start​(java.time.Duration timeout)
      Starts monitoring the current thread.
      (package private) static void stop​(java.lang.Thread thread)
      Stops monitoring the specified thread.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • thread

        private final java.lang.Thread thread
      • timeout

        private final java.time.Duration timeout
    • Constructor Detail

      • ThreadMonitor

        private ThreadMonitor​(java.lang.Thread thread,
                              java.time.Duration timeout)
        Constructs a new monitor.
        Parameters:
        thread - The thread to monitor.
        timeout - The timeout amount.
    • Method Detail

      • start

        static java.lang.Thread start​(java.time.Duration timeout)
        Starts monitoring the current thread.
        Parameters:
        timeout - The timeout amount. or no timeout if the value is zero or less.
        Returns:
        The monitor thread or null if the timeout amount is not greater than zero.
      • start

        static java.lang.Thread start​(java.lang.Thread thread,
                                      java.time.Duration timeout)
        Starts monitoring the specified thread.
        Parameters:
        thread - The thread to monitor
        timeout - The timeout amount. or no timeout if the value is zero or less.
        Returns:
        The monitor thread or null if the timeout amount is not greater than zero.
      • stop

        static void stop​(java.lang.Thread thread)
        Stops monitoring the specified thread.
        Parameters:
        thread - The monitor thread, may be null.
      • run

        public void run()
        Sleeps until the specified timeout amount and then interrupt the thread being monitored.
        Specified by:
        run in interface java.lang.Runnable
        See Also:
        Runnable.run()