Package org.apache.commons.io
Class ThreadMonitor
- java.lang.Object
-
- org.apache.commons.io.ThreadMonitor
-
- All Implemented Interfaces:
java.lang.Runnable
class ThreadMonitor extends java.lang.Object implements java.lang.Runnable
Monitors a thread, interrupting it of it reaches the specified timout.This works by sleeping until the specified timout 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 }
- Version:
- $Id: ThreadMonitor.java 1307459 2012-03-30 15:11:44Z ggregory $
-
-
Constructor Summary
Constructors Modifier Constructor Description private
ThreadMonitor(java.lang.Thread thread, long timeout)
Construct and new monitor.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
run()
Sleep until the specified timout amount and then interrupt the thread being monitored.static java.lang.Thread
start(long timeout)
Start monitoring the current thread.static java.lang.Thread
start(java.lang.Thread thread, long timeout)
Start monitoring the specified thread.static void
stop(java.lang.Thread thread)
Stop monitoring the specified thread.
-
-
-
Method Detail
-
start
public static java.lang.Thread start(long timeout)
Start monitoring the current thread.- Parameters:
timeout
- The timout amount in milliseconds or no timeout if the value is zero or less- Returns:
- The monitor thread or
null
if the timout amount is not greater than zero
-
start
public static java.lang.Thread start(java.lang.Thread thread, long timeout)
Start monitoring the specified thread.- Parameters:
thread
- The thread The thread to monitortimeout
- The timout amount in milliseconds or no timeout if the value is zero or less- Returns:
- The monitor thread or
null
if the timout amount is not greater than zero
-
stop
public static void stop(java.lang.Thread thread)
Stop monitoring the specified thread.- Parameters:
thread
- The monitor thread, may benull
-
run
public void run()
Sleep until the specified timout amount and then interrupt the thread being monitored.- Specified by:
run
in interfacejava.lang.Runnable
- See Also:
Runnable.run()
-
-