|
xmlBlaster 2.2.0 client API | |||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object java.lang.Thread org.xmlBlaster.util.Timeout
public class 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.
org.xmlBlaster.test.classtest.TimeoutTest
,
TimeoutPooled
Nested Class Summary |
---|
Nested classes/interfaces inherited from class java.lang.Thread |
---|
java.lang.Thread.State, java.lang.Thread.UncaughtExceptionHandler |
Field Summary |
---|
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. |
java.lang.String |
dumpStatus()
|
long |
elapsed(Timestamp key)
How long am i running. |
org.xmlBlaster.util.Container[] |
getContainers()
|
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. |
java.lang.String |
toString()
|
Methods inherited from class java.lang.Thread |
---|
activeCount, checkAccess, countStackFrames, currentThread, destroy, dumpStack, enumerate, getAllStackTraces, getContextClassLoader, getDefaultUncaughtExceptionHandler, getId, getName, getPriority, getStackTrace, getState, getThreadGroup, getUncaughtExceptionHandler, holdsLock, interrupt, interrupted, isAlive, isDaemon, isInterrupted, join, join, join, resume, setContextClassLoader, setDaemon, setDefaultUncaughtExceptionHandler, setName, setPriority, setUncaughtExceptionHandler, sleep, sleep, start, stop, stop, suspend, yield |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Constructor Detail |
---|
public Timeout()
public Timeout(java.lang.String name)
name
- The name of the threadpublic 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()
getSize
in interface I_TimeoutManager
public java.lang.String toString()
toString
in class java.lang.Thread
public java.lang.String dumpStatus()
dumpStatus
in interface I_TimeoutManager
public org.xmlBlaster.util.Container[] getContainers()
public void run()
run
in interface java.lang.Runnable
run
in class java.lang.Thread
public final Timestamp addTimeoutListener(I_Timeout listener, long delay, java.lang.Object userData)
addTimeoutListener
in interface I_TimeoutManager
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");
refreshTimeoutListener
in interface I_TimeoutManager
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
alreadypublic final Timestamp addOrRefreshTimeoutListener(I_Timeout listener, long delay, java.lang.Object userData, Timestamp key) throws XmlBlasterException
addOrRefreshTimeoutListener
in interface I_TimeoutManager
XmlBlasterException
public final void removeTimeoutListener(Timestamp key)
removeTimeoutListener
in interface I_TimeoutManager
key
- The timeout handle you received by a previous
addTimeoutListener() call.public final boolean isExpired(Timestamp key)
isExpired
in interface I_TimeoutManager
key
- The timeout handle you received by a previous
addTimeoutListener() callpublic final long spanToTimeout(Timestamp key)
spanToTimeout
in interface I_TimeoutManager
key
- The timeout handle you received by a previous
addTimeoutListener() call.
public final long elapsed(Timestamp key)
elapsed
in interface I_TimeoutManager
key
- The timeout handle you received by a previous
addTimeoutListener() call.
public final long getTimeout(Timestamp key)
getTimeout
in interface I_TimeoutManager
key
- The timeout handle you received by a previous
addTimeoutListener() call.
public final void removeAll()
removeAll
in interface I_TimeoutManager
public void shutdown()
shutdown
in interface I_TimeoutManager
public static void main(java.lang.String[] args) throws java.lang.Exception
java.lang.Exception
|
xmlBlaster 2.2.0 client API | |||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |