Class ThreadMonitor

java.lang.Object
org.apache.commons.io.ThreadMonitor
All Implemented Interfaces:
Runnable

class ThreadMonitor extends Object implements 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.

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

    Fields
    Modifier and Type
    Field
    Description
    private final Thread
     
    private final Duration
     
  • Constructor Summary

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

    Modifier and Type
    Method
    Description
    void
    run()
    Sleep until the specified timeout amount and then interrupt the thread being monitored.
    private static void
    sleep(Duration duration)
    Sleeps for a guaranteed minimum duration unless interrupted.
    (package private) static Thread
    start(Thread thread, Duration timeout)
    Start monitoring the specified thread.
    (package private) static Thread
    start(Duration timeout)
    Start monitoring the current thread.
    (package private) static void
    stop(Thread thread)
    Stop monitoring the specified thread.

    Methods inherited from class java.lang.Object

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

    • thread

      private final Thread thread
    • timeout

      private final Duration timeout
  • Constructor Details

    • ThreadMonitor

      private ThreadMonitor(Thread thread, Duration timeout)
      Constructs a new monitor.
      Parameters:
      thread - The thread to monitor
      timeout - The timeout amount in milliseconds
  • Method Details

    • start

      static Thread start(Duration timeout)
      Start monitoring the current thread.
      Parameters:
      timeout - The timeout amount in milliseconds 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 Thread start(Thread thread, Duration timeout)
      Start monitoring the specified thread.
      Parameters:
      thread - The thread The thread to monitor
      timeout - The timeout amount in milliseconds 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(Thread thread)
      Stop monitoring the specified thread.
      Parameters:
      thread - The monitor thread, may be null
    • run

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

      private static void sleep(Duration duration) throws InterruptedException
      Sleeps for a guaranteed minimum duration unless interrupted. This method exists because Thread.sleep(100) can sleep for 0, 70, 100 or 200ms or anything else it deems appropriate. Read the docs on Thread.sleep for further interesting details.
      Parameters:
      duration - the sleep duration.
      Throws:
      InterruptedException - if interrupted