|
xmlBlaster 2.1.0 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.TimeoutPooled
public class TimeoutPooled
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 pool of threads used to execute the I_Timeout.timeout() callback. Timer callbacks should complete quickly to not exhaust the pool. 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 {
...
TimeoutPooled timeout = new TimeoutPooled("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:
TimeoutPooled timeout = new TimeoutPooled("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.
-logging/org.xmlBlaster.util.TimeoutPooled FINER
TimeoutTest| Nested Class Summary |
|---|
| Nested classes/interfaces inherited from class java.lang.Thread |
|---|
java.lang.Thread.State, java.lang.Thread.UncaughtExceptionHandler |
| Field Summary | |
|---|---|
private static java.util.logging.Logger |
log
|
private java.util.TreeMap<Timestamp,Container> |
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 java.util.concurrent.ThreadPoolExecutor |
threadPool
|
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 | |
|---|---|
TimeoutPooled()
Create a timer thread with a strong reference on the callback objects. |
|
TimeoutPooled(java.lang.String name)
Create a timer thread with a strong reference on the callback objects. |
|
TimeoutPooled(java.lang.String name,
boolean useWeakReference)
|
|
TimeoutPooled(java.lang.String name,
boolean useWeakReference,
java.util.concurrent.ThreadPoolExecutor threadPool)
|
|
| 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. |
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. |
private Timestamp |
refreshTimeoutListener(Timestamp key,
long delay,
boolean throwExceptionIfMissing)
|
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 |
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 |
| Field Detail |
|---|
private static java.util.logging.Logger log
private static java.lang.String ME
private java.util.TreeMap<Timestamp,Container> map
private boolean running
private boolean ready
private final boolean useWeakReference
private boolean mapHasNewEntry
private final java.util.concurrent.ThreadPoolExecutor threadPool
| Constructor Detail |
|---|
public TimeoutPooled()
public TimeoutPooled(java.lang.String name)
name - The name of the thread
public TimeoutPooled(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.
public TimeoutPooled(java.lang.String name,
boolean useWeakReference,
java.util.concurrent.ThreadPoolExecutor threadPool)
| Method Detail |
|---|
public int getSize()
I_TimeoutManager
getSize in interface I_TimeoutManagerpublic java.lang.String toString()
toString in class java.lang.Threadpublic java.lang.String dumpStatus()
dumpStatus in interface I_TimeoutManagerpublic Container[] getContainers()
public void run()
run in interface java.lang.Runnablerun in class java.lang.Thread
public final Timestamp addTimeoutListener(I_Timeout listener,
long delay,
java.lang.Object userData)
I_TimeoutManager
addTimeoutListener in interface I_TimeoutManagerlistener - 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
I_TimeoutManagertimeout.removeTimeoutListener(timeoutHandle); timeoutHandle = timeout.addTimeoutListener(this, "1000L", "UserData");
refreshTimeoutListener in interface I_TimeoutManagerkey - 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
private Timestamp refreshTimeoutListener(Timestamp key,
long delay,
boolean throwExceptionIfMissing)
throws XmlBlasterException
XmlBlasterException
public final Timestamp addOrRefreshTimeoutListener(I_Timeout listener,
long delay,
java.lang.Object userData,
Timestamp key)
throws XmlBlasterException
I_TimeoutManager
addOrRefreshTimeoutListener in interface I_TimeoutManagerXmlBlasterExceptionpublic final void removeTimeoutListener(Timestamp key)
I_TimeoutManager
removeTimeoutListener in interface I_TimeoutManagerkey - The timeout handle you received by a previous
addTimeoutListener() call.public final boolean isExpired(Timestamp key)
I_TimeoutManager
isExpired in interface I_TimeoutManagerkey - The timeout handle you received by a previous
addTimeoutListener() callpublic final long spanToTimeout(Timestamp key)
I_TimeoutManager
spanToTimeout in interface I_TimeoutManagerkey - The timeout handle you received by a previous
addTimeoutListener() call.
public final long elapsed(Timestamp key)
I_TimeoutManager
elapsed in interface I_TimeoutManagerkey - The timeout handle you received by a previous
addTimeoutListener() call.
public final long getTimeout(Timestamp key)
I_TimeoutManager
getTimeout in interface I_TimeoutManagerkey - The timeout handle you received by a previous
addTimeoutListener() call.
public final void removeAll()
I_TimeoutManager
removeAll in interface I_TimeoutManagerpublic void shutdown()
I_TimeoutManager
shutdown in interface I_TimeoutManager
public static void main(java.lang.String[] args)
throws java.lang.Exception
java.lang.Exceptionstatic void testStrongReference()
static void testWeakReference()
|
xmlBlaster 2.1.0 API | |||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||