📄 actualtimer.java
字号:
/* * @(#)$Id: ActualTimer.java,v 1.4 2005/07/09 20:11:14 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 runtime.services.timer;import execution.EndExecution;import java.text.DateFormat;import java.util.Date;import java.util.TreeMap;import services.timer.Timer;import services.timer.TimerClient;/** * Class ActualTimer * */public class ActualTimer implements Timer { private long timeOffset; private Object syncObject; private TreeMap events; private ActualTimerThread runner; /** * Constructor ActualTimer * * @param defaultSleepTime * @param beginTime * @param ignoreBeginTime * @param syncObject */ public ActualTimer(long defaultSleepTime, String beginTime, boolean ignoreBeginTime, Object syncObject) { if (ignoreBeginTime) { this.timeOffset = System.currentTimeMillis(); } else { try { DateFormat beginDate = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT); beginDate.setLenient(true); this.timeOffset = ((Date) beginDate.parseObject(beginTime)).getTime(); } catch (Exception exception) { throw new RuntimeException("TIMER: Unable to parse begin time: " + beginTime + ", " + exception.getMessage()); } } this.syncObject = syncObject; events = new TreeMap(); runner = new ActualTimerThread(events, this, defaultSleepTime); runner.start(); } /** * Method schedule * * @param relativeTime * @param data * @param client */ public void schedule(double relativeTime, Object data, TimerClient client) { ActualTimerEvent firstEvent = null; double executionTime = relativeTime + getCurrentTime(); ActualTimerEvent event = ActualTimerEvent.allocate(executionTime, data, client); // Synchronize actions that read/insert the shared treemap synchronized (events) { if (events.size() > 0) { firstEvent = ((ActualTimerEvent) events.firstKey()); } events.put(event, event); } // Check if this event is earlier than all other events, if so wake thread so it can reset timers if ((firstEvent == null) || (firstEvent.getExecutionTime() > executionTime)) { runner.interrupt(); } } /** * Method scheduleMS * * @param relativeTimeMS * @param data * @param client */ public void scheduleMS(long relativeTimeMS, Object data, TimerClient client) { schedule(((double) relativeTimeMS) / 1000D, data, client); } /** * Method getCurrentTime * @return */ public double getCurrentTime() { return (((double) (System.currentTimeMillis() - timeOffset)) / ((double) 1000)); } /** * Method getCurrentTimeMS * @return */ public long getCurrentTimeMS() { return System.currentTimeMillis() - timeOffset; } /** * Method runEvent * * @param event */ protected void runEvent(ActualTimerEvent event) { // run the event TimerClient client = event.getClient(); synchronized (syncObject) { try { client.handleClock(event.getData()); } catch (Exception exception) { EndExecution.masterSync = exception; runner.end(); } } ActualTimerEvent.free(event); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -