📄 timeout.java
字号:
* NOTE: The returned timeout handle is different from the original one. * <p /> * * NOTE: If you are not sure if the key has elapsed already try this: * * <pre> * timeout.removeTimeoutListener(timeoutHandle); * timeoutHandle = timeout.addTimeoutListener(this, "1000L", "UserData"); * </pre> * * @param key * The timeout handle you received by a previous * addTimeoutListener() call.<br /> * It is invalid after this call. * @param delay * The timeout in milliseconds measured from now. * @return A new handle which you can use to unregister with * removeTimeoutListener() * @exception XmlBlasterException * if key is null or unknown or invalid because timer elapsed * already */ public final Timestamp refreshTimeoutListener(Timestamp key, long delay) throws XmlBlasterException { if (key == null) { Thread.dumpStack(); throw new XmlBlasterException(Global.instance(), ErrorCode.INTERNAL_NULLPOINTER, ME + "addTimeoutListener", "The timeout handle is null, no timeout refresh done"); } Object obj; synchronized (map) { obj = map.remove(key); } if (obj == null) { String pos = Global.getStackTraceAsString(null); throw new XmlBlasterException(Global.instance(), ErrorCode.RESOURCE_UNAVAILABLE, ME, "The timeout handle '" + key + "' is unknown, no timeout refresh done: " + pos); } Container container = (Container) obj; I_Timeout callback = container.getCallback(); if (callback == null) { if (this.useWeakReference) throw new XmlBlasterException(Global.instance(), ErrorCode.INTERNAL_UNKNOWN, ME, "The weak callback reference for timeout handle '" + key + "' is garbage collected, no timeout refresh done"); else throw new XmlBlasterException(Global.instance(), ErrorCode.INTERNAL_UNKNOWN, ME, "Internal error for timeout handle '" + key + "', no timeout refresh done"); } return addTimeoutListener(callback, delay, container.getUserData()); } /** * Checks if key is null -> addTimeoutListener else refreshTimeoutListener() * in a thread save way. <br /> * Note however that your passed key is different from the returned key and * you need to synchronize this call to avoid having a stale key (two threads * enter this method the same time, the key gets invalid by the first thread * and the second passed a stale key as the first thread has not yet returned * to update 'key') */ public final Timestamp addOrRefreshTimeoutListener(I_Timeout listener, long delay, Object userData, Timestamp key) throws XmlBlasterException { if (key == null) { return addTimeoutListener(listener, delay, userData); } else { try { return refreshTimeoutListener(key, delay); } catch (XmlBlasterException e) { if (ErrorCode.RESOURCE_UNAVAILABLE == e.getErrorCode()) { return addTimeoutListener(listener, delay, userData); } throw e; } } } /** * Remove a listener before the timeout happened. * <p /> * * @param key * The timeout handle you received by a previous * addTimeoutListener() call. */ public final void removeTimeoutListener(Timestamp key) { if (key == null) return; synchronized (map) { Container container = (Container) map.remove(key); if (container != null) { container.reset(); container = null; } } } /** * Is this handle expired? * <p /> * * @param key * The timeout handle you received by a previous * addTimeoutListener() call<br /> * @return true/false */ public final boolean isExpired(Timestamp key) { synchronized (map) { return map.get(key) == null; } } /** * How long to my timeout. * <p /> * * @param key * The timeout handle you received by a previous * addTimeoutListener() call. * @return Milliseconds to timeout, or -1 if not known. */ public final long spanToTimeout(Timestamp key) { if (key == null) { return -1; } synchronized (map) { Container container = (Container) map.get(key); if (container == null) { return -1; } // We know that the key contains the timeout date return getTimeout(key) - System.currentTimeMillis(); } } /** * How long am i running. * <p /> * * @param key * The timeout handle you received by a previous * addTimeoutListener() call. * @return Milliseconds since creation, or -1 if not known. */ public final long elapsed(Timestamp key) { synchronized (map) { Container container = (Container) map.get(key); if (container == null) { return -1; } return System.currentTimeMillis() - container.creation; } } /** * Access the end of life span. * <p /> * * @param key * The timeout handle you received by a previous * addTimeoutListener() call. * @return Time in milliseconds since midnight, January 1, 1970 UTC or -1 if * not known. */ public final long getTimeout(Timestamp key) { if (key == null) return -1; return key.getMillis(); } /** * Reset all pending timeouts. */ public final void removeAll() { synchronized (map) { map.clear(); } } /** * Reset and stop the Timeout manager thread. */ public void shutdown() { removeAll(); running = false; synchronized (this) { notify(); } // destroy(); } /** * Method for testing only. * <p /> * Invoke: java -Djava.compiler= org.xmlBlaster.util.Timeout */ public static void main(String args[]) throws Exception { Timeout t = new Timeout(); System.out.println("Timeout constructor done, sleeping 10 sec " + t.toString()); try { Thread.sleep(10000); } catch (InterruptedException e) { } } /** Eample for the standard case */ static void testStrongReference() { Timeout timeout = new Timeout("TestTimer"); System.out.println("Timer created and ready."); Timestamp timeoutHandle = timeout.addTimeoutListener(new I_Timeout() { public void timeout(Object userData) { System.out.println("Timeout happened"); System.exit(0); } }, 2000L, null); try { Thread.sleep(4000L); } catch (InterruptedException e) { } System.err.println("ERROR: Timeout not occurred " + timeoutHandle.toString()); System.exit(1); } /** Test a weak reference */ static void testWeakReference() { Timeout timeout = new Timeout("TestTimer", true); System.out.println("Timer created and ready."); { WeakObj weakObj = new WeakObj(); /* Timestamp timeoutHandle = */timeout.addTimeoutListener(weakObj, 4000L, weakObj); weakObj = null; } System.gc(); System.out.println("Called gc()"); // NOTE: Without "weakObj = null" and // "System.gc()" the test fails try { Thread.sleep(8000L); } catch (InterruptedException e) { } System.out.println("SUCCESS: No timeout happened"); System.exit(0); }}/** Test a weak reference */final class WeakObj implements I_Timeout { public void timeout(Object userData) { System.err .println("ERROR: Timeout invoked, weak object was not garbage collected."); System.exit(1); } public void finalize() { System.out.println("WeakObj is garbage collected"); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -