📄 timeoutmanager.java
字号:
/* * @(#)$Id: TimeoutManager.java,v 1.5 2004/07/02 23:59:23 huebsch Exp $ * * Copyright (c) 2001-2004 Regents of the University of California. * All rights reserved. * * This file is distributed under the terms in the attached BERKELEY-LICENSE * file. If you do not find these files, copies can be found by writing to: * Computer Science Division, Database Group, Universite of California, * 617 Soda Hall #1776, Berkeley, CA 94720-1776. Attention: Berkeley License * * Copyright (c) 2003-2004 Intel Corporation. All rights reserved. * * This file is distributed under the terms in the attached INTEL-LICENSE file. * If you do not find these files, copies can be found by writing to: * Intel Research Berkeley, 2150 Shattuck Avenue, Suite 1300, * Berkeley, CA, 94704. Attention: Intel License Inquiry. */package util.timer.timeouts;import java.util.HashMap;import java.util.TreeMap;import services.LocalNode;import services.timer.TimerClient;/** * Class TimeoutManager * */public class TimeoutManager implements TimerClient { private HashMap itemLookup; private TreeMap items; private double nextAlarm; /** * Constructor TimeoutManager */ public TimeoutManager() { itemLookup = new HashMap(); items = new TreeMap(); this.nextAlarm = Double.MAX_VALUE; } /** * Method addTimeout * * @param item * @param delay * @param client */ public void addTimeout(Object item, double delay, TimeoutManagerClient client) { addTimeout(item, item, delay, client); } /** * Method addTimeout * * @param item * @param key * @param delay * @param client */ public void addTimeout(Object item, Object key, double delay, TimeoutManagerClient client) { double currentTime = LocalNode.myTimer.getCurrentTime(); addAbsoluteTimeout(item, key, currentTime + delay, client); } /** * Method addAbsoluteTimeout * * @param item * @param timeout * @param client */ public void addAbsoluteTimeout(Object item, double timeout, TimeoutManagerClient client) { addAbsoluteTimeout(item, item, timeout, client); } /** * Method addAbsoluteTimeout * * @param item * @param key * @param timeout * @param client */ public void addAbsoluteTimeout(Object item, Object key, double timeout, TimeoutManagerClient client) { TimeoutManagerEntry entry = TimeoutManagerEntry.allocate(item, key, timeout, client); items.put(entry, entry); itemLookup.put(key, entry); determineNextTimer(); } /** * Remove a timeout, to prevent a callback * @param key the key provided when registering (adding) the timeout * @return the item (not the key) obtained from registering the timeout */ public Object removeTimeout(Object key) { Object entry = itemLookup.remove(key); if (entry != null) { TimeoutManagerEntry result = (TimeoutManagerEntry) items.remove(entry); return result.getItem(); } return null; } private void setNextTimer(double nextUpdate) { if (nextUpdate < nextAlarm) { LocalNode.myTimer.schedule( nextUpdate - LocalNode.myTimer.getCurrentTime(), null, this); nextAlarm = nextUpdate; } } private void determineNextTimer() { if (items.size() != 0) { TimeoutManagerEntry firstItem = (TimeoutManagerEntry) items.firstKey(); setNextTimer(firstItem.getTimeout()); } } /** * Method handleClock * * @param data */ public void handleClock(Object data) { double currentTime = LocalNode.myTimer.getCurrentTime(); nextAlarm = Double.MAX_VALUE; while (items.size() != 0) { TimeoutManagerEntry headItem = (TimeoutManagerEntry) items.firstKey(); if (headItem.getTimeout() > currentTime) { determineNextTimer(); return; } else { items.remove(headItem); itemLookup.remove(headItem.getKey()); headItem.getClient().handleTimeout(headItem.getItem(), currentTime); } } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -