Package net.sourceforge.jtds.util
Class TimerThread
- java.lang.Object
-
- java.lang.Thread
-
- net.sourceforge.jtds.util.TimerThread
-
- All Implemented Interfaces:
java.lang.Runnable
public class TimerThread extends java.lang.Thread
Simple timer class used to implement login and query timeouts. This thread runs as a Daemon thread to ensure that the java VM will exit correctly when normal execution is complete. It provides both a singleton implementation and a default constructor for the case when more than one timer thread is desired.- Version:
- $Id: TimerThread.java,v 1.5.2.2 2009/10/17 13:21:27 ickzon Exp $
- Author:
- Alin Sinpalean, Mike Hutchinson
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interface
TimerThread.TimerListener
Interface to be implemented by classes that request timer services.private static class
TimerThread.TimerRequest
Internal class associating a login or query timeout value with a targetTimerListener
.
-
Field Summary
Fields Modifier and Type Field Description private static TimerThread
instance
Singleton instance.private long
nextTimeout
Time when the first request time out should occur.private java.util.LinkedList
timerList
List ofTimerRequest
s to execute, ordered by time.
-
Constructor Summary
Constructors Constructor Description TimerThread()
Construct a newTimerThread
instance.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
cancelTimer(java.lang.Object handle)
Remove a redundant timer before it expires.static TimerThread
getInstance()
Singleton getter.boolean
hasExpired(java.lang.Object handle)
Check whether a timer has expired.void
run()
Execute theTimerThread
main loop.java.lang.Object
setTimer(int timeout, TimerThread.TimerListener l)
Add a timer request to the queue.void
stopTimer()
Completely stops the timer and its underlying Java thread, discarding all pending timeouts.private void
updateNextTimeout()
Internal method that updates the value ofnextTimeout
.-
Methods inherited from class java.lang.Thread
activeCount, checkAccess, clone, countStackFrames, currentThread, dumpStack, enumerate, getAllStackTraces, getContextClassLoader, getDefaultUncaughtExceptionHandler, getId, getName, getPriority, getStackTrace, getState, getThreadGroup, getUncaughtExceptionHandler, holdsLock, interrupt, interrupted, isAlive, isDaemon, isInterrupted, join, join, join, onSpinWait, resume, setContextClassLoader, setDaemon, setDefaultUncaughtExceptionHandler, setName, setPriority, setUncaughtExceptionHandler, sleep, sleep, start, stop, suspend, toString, yield
-
-
-
-
Field Detail
-
instance
private static TimerThread instance
Singleton instance.
-
timerList
private final java.util.LinkedList timerList
List ofTimerRequest
s to execute, ordered by time.
-
nextTimeout
private long nextTimeout
Time when the first request time out should occur.
-
-
Method Detail
-
getInstance
public static TimerThread getInstance()
Singleton getter.
-
run
public void run()
Execute theTimerThread
main loop.- Specified by:
run
in interfacejava.lang.Runnable
- Overrides:
run
in classjava.lang.Thread
-
setTimer
public java.lang.Object setTimer(int timeout, TimerThread.TimerListener l)
Add a timer request to the queue. The queue is ordered by time so that the head of the list is always the first timer to expire.- Parameters:
timeout
- the interval in milliseconds after which the timer will expirel
-TimerListener
to be notified on timeout- Returns:
- a handle to the timer request, that can later be used with
cancelTimer
-
cancelTimer
public boolean cancelTimer(java.lang.Object handle)
Remove a redundant timer before it expires.- Parameters:
handle
- handle to the request to be removed from the queue (aTimerRequest
instance)- Returns:
true
if timer had not expired
-
stopTimer
public void stopTimer()
Completely stops the timer and its underlying Java thread, discarding all pending timeouts. Any subsequent invocation ofgetInstance()
will restart the timer.
-
hasExpired
public boolean hasExpired(java.lang.Object handle)
Check whether a timer has expired.- Parameters:
handle
- handle to the request to be checked for expiry (aTimerRequest
instance)- Returns:
true
if timer has expired
-
updateNextTimeout
private void updateNextTimeout()
Internal method that updates the value ofnextTimeout
.
-
-