|
xmlBlaster 1.6.2 API | ||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||||
java.lang.Objectjava.lang.Thread
org.xmlBlaster.util.Timeout
Allows you be called back after a given delay.
Note that this class should be called Timer, but with JDK 1.3 there will be a java.util.Timer. There is a single background thread that is used to execute the I_Timeout.timeout() callback. Timer callbacks should complete quickly. If a timeout() takes excessive time to complete, it "hogs" the timer's task execution thread. This can, in turn, delay the execution of subsequent tasks, which may "bunch up" and execute in rapid succession when (and if) the offending task finally completes. This singleton is thread-safe. This class does not offer real-time guarantees, but usually notifies you within ~ 20 milliseconds of the scheduled time. Adding or removing a timer is good performing, also when huge amounts of timers (> 1000) are used.
public class MyClass implements I_Timeout {
...
Timeout timeout = new Timeout("TestTimer");
Timestamp timeoutHandle = timeout.addTimeoutListener(this, 4000L, "myTimeout");
...
public void timeout(Object userData) {
// userData contains String "myTimeout"
System.out.println("Timeout happened");
...
// If you want to activate the timer again:
timeoutHandle = timeout.addTimeoutListener(this, 4000L, "myTimeout");
}
...
// if you want to refresh the timer:
timeoutHandle = timeout.refreshTimeoutListener(timeoutHandle, 1500L);
...
}
Or a short form:
Timeout timeout = new Timeout("TestTimer");
Timestamp timeoutHandle = timeout.addTimeoutListener(new I_Timeout() {
public void timeout(Object userData) {
System.out.println("Timeout happened");
System.exit(0);
}
}, 2000L, null);
JDK 1.2 or higher only.
TimeoutTest| Field Summary | |
private boolean |
debug
Switch on debugging output |
private java.util.TreeMap |
map
Sorted map |
private boolean |
mapHasNewEntry
To avoid sync |
private static java.lang.String |
ME
Name for logging output |
private boolean |
ready
On creation wait until thread started |
private boolean |
running
Start/Stop the Timeout manager thread |
private boolean |
useWeakReference
Hold only weak reference on callback object? |
| Fields inherited from class java.lang.Thread |
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY |
| Constructor Summary | |
Timeout()
Create a timer thread with a strong reference on the callback objects. |
|
Timeout(java.lang.String name)
Create a timer thread with a strong reference on the callback objects. |
|
Timeout(java.lang.String name,
boolean useWeakReference)
|
|
| Method Summary | |
Timestamp |
addOrRefreshTimeoutListener(I_Timeout listener,
long delay,
java.lang.Object userData,
Timestamp key)
Checks if key is null -> addTimeoutListener else refreshTimeoutListener() in a thread save way. |
Timestamp |
addTimeoutListener(I_Timeout listener,
long delay,
java.lang.Object userData)
Add a listener which gets informed after 'delay' milliseconds. |
long |
elapsed(Timestamp key)
How long am i running. |
int |
getSize()
Get number of current used timers. |
long |
getTimeout(Timestamp key)
Access the end of life span. |
boolean |
isExpired(Timestamp key)
Is this handle expired? |
static void |
main(java.lang.String[] args)
Method for testing only. |
Timestamp |
refreshTimeoutListener(Timestamp key,
long delay)
Refresh a listener before the timeout happened. |
void |
removeAll()
Reset all pending timeouts. |
void |
removeTimeoutListener(Timestamp key)
Remove a listener before the timeout happened. |
void |
run()
Starts the Timeout manager thread. |
void |
shutdown()
Reset and stop the Timeout manager thread. |
long |
spanToTimeout(Timestamp key)
How long to my timeout. |
(package private) static void |
testStrongReference()
Eample for the standard case |
(package private) static void |
testWeakReference()
Test a weak reference |
| Methods inherited from class java.lang.Thread |
activeCount, checkAccess, countStackFrames, currentThread, destroy, dumpStack, enumerate, getContextClassLoader, getName, getPriority, getThreadGroup, holdsLock, interrupt, interrupted, isAlive, isDaemon, isInterrupted, join, join, join, resume, setContextClassLoader, setDaemon, setName, setPriority, sleep, sleep, start, stop, stop, suspend, toString, yield |
| Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
| Field Detail |
private static java.lang.String ME
private java.util.TreeMap map
private boolean running
private boolean ready
private final boolean debug
private final boolean useWeakReference
private boolean mapHasNewEntry
| Constructor Detail |
public Timeout()
public Timeout(java.lang.String name)
name - The name of the thread
public Timeout(java.lang.String name,
boolean useWeakReference)
name - The name of the threaduseWeakReference - If true the reference on your I_Timeout implementation is only
weak referenced and may be garbage collected even that we hold a
weak reference.| Method Detail |
public int getSize()
public void run()
public final Timestamp addTimeoutListener(I_Timeout listener,
long delay,
java.lang.Object userData)
listener - Your callback handle (you need to implement this interface).delay - The timeout in milliseconds. You can pass 0L and the Timeout
thread will fire immediately, this can be useful to dispatch a
task to the timeoutlisteneruserData - Some arbitrary data you supply, it will be routed back to you
when the timeout occurs through method I_Timeout.timeout().
public final Timestamp refreshTimeoutListener(Timestamp key,
long delay)
throws XmlBlasterException
timeout.removeTimeoutListener(timeoutHandle); timeoutHandle = timeout.addTimeoutListener(this, "1000L", "UserData");
key - The timeout handle you received by a previous
addTimeoutListener() call.delay - The timeout in milliseconds measured from now.
XmlBlasterException - if key is null or unknown or invalid because timer elapsed
already
public final Timestamp addOrRefreshTimeoutListener(I_Timeout listener,
long delay,
java.lang.Object userData,
Timestamp key)
throws XmlBlasterException
XmlBlasterExceptionpublic final void removeTimeoutListener(Timestamp key)
key - The timeout handle you received by a previous
addTimeoutListener() call.public final boolean isExpired(Timestamp key)
key - The timeout handle you received by a previous
addTimeoutListener() callpublic final long spanToTimeout(Timestamp key)
key - The timeout handle you received by a previous
addTimeoutListener() call.
public final long elapsed(Timestamp key)
key - The timeout handle you received by a previous
addTimeoutListener() call.
public final long getTimeout(Timestamp key)
key - The timeout handle you received by a previous
addTimeoutListener() call.
public final void removeAll()
public void shutdown()
public static void main(java.lang.String[] args)
throws java.lang.Exception
java.lang.Exceptionstatic void testStrongReference()
static void testWeakReference()
|
xmlBlaster 1.6.2 API | ||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||||