Package net.sourceforge.jtds.util
Class TimerThread
java.lang.Object
java.lang.Thread
net.sourceforge.jtds.util.TimerThread
- All Implemented Interfaces:
Runnable
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.3 2010-05-17 09:16:06 ickzon Exp $
- Author:
- Alin Sinpalean, Mike Hutchinson
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceInterface to be implemented by classes that request timer services.private static classInternal class associating a login or query timeout value with a targetTimerListener.Nested classes/interfaces inherited from class java.lang.Thread
Thread.Builder, Thread.State, Thread.UncaughtExceptionHandler -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static TimerThreadSingleton instance.private longTime when the first request time out should occur.private final LinkedListList ofTimerRequests to execute, ordered by time.Fields inherited from class java.lang.Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionbooleancancelTimer(Object handle) Remove a redundant timer before it expires.static TimerThreadSingleton getter.booleanhasExpired(Object handle) Check whether a timer has expired.voidrun()Execute theTimerThreadmain loop.setTimer(int timeout, TimerThread.TimerListener l) Add a timer request to the queue.static voidCompletely stops the timer and its underlying Java thread, discarding all pending timeouts.private voidInternal 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, isVirtual, join, join, join, join, ofPlatform, ofVirtual, onSpinWait, resume, setContextClassLoader, setDaemon, setDefaultUncaughtExceptionHandler, setName, setPriority, setUncaughtExceptionHandler, sleep, sleep, sleep, start, startVirtualThread, stop, suspend, threadId, toString, yield
-
Field Details
-
instance
Singleton instance. -
timerList
List ofTimerRequests to execute, ordered by time. -
nextTimeout
private long nextTimeoutTime when the first request time out should occur.
-
-
Constructor Details
-
TimerThread
public TimerThread()Construct a newTimerThreadinstance.
-
-
Method Details
-
getInstance
Singleton getter. -
run
public void run()Execute theTimerThreadmain loop. -
setTimer
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-TimerListenerto be notified on timeout- Returns:
- a handle to the timer request, that can later be used with
cancelTimer
-
cancelTimer
Remove a redundant timer before it expires.- Parameters:
handle- handle to the request to be removed from the queue (aTimerRequestinstance)- Returns:
trueif timer had not expired
-
stopTimer
public static void stopTimer()Completely stops the timer and its underlying Java thread, discarding all pending timeouts. Any subsequent invocation ofgetInstance()will restart the timer. -
hasExpired
Check whether a timer has expired.- Parameters:
handle- handle to the request to be checked for expiry (aTimerRequestinstance)- Returns:
trueif timer has expired
-
updateNextTimeout
private void updateNextTimeout()Internal method that updates the value ofnextTimeout.
-