home *** CD-ROM | disk | FTP | other *** search
Java Source | 1998-03-18 | 5.8 KB | 187 lines |
- /*
- * Copyright (c) 1997 Krumel & Associates, Inc. All Rights Reserved.
- *
- * www.krumel.com - controls@krumel.com
- *
- * Permission is given to the buyer of this package for one software
- * developer to use this software on one CPU (one workstation) and to make
- * one backup copy. You may uitilize and/or modify this class for use in your
- * projects. You may distribute or sell any executable which results from
- * using this code in yur application, except a utility or class of similar
- * nature to this product. You may distribute this product in compiled
- * form only, but soley to be used with your cmpiled executable product
- * for the puposes of dynamic loading. You may NOT redistribute the source
- * code in any form or make it accessible through a network or other
- * distribution media to others. Please refer to the file "copyright.html"
- * for further important copyright and licensing information.
- *
- * The source code is the confidential and proprietary information
- * of Krumel & Associates, Inc. ("Confidential Information"). You shall
- * not disclose such Confidential Information and shall use it only in
- * accordance with the terms of the license agreement you entered into
- * with Krumel & Associates, Inc..
-
- * KRUMEL & ASSOCIATES MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE
- * SUITABILITY OF THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT
- * NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
- * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. KRUMEL & ASSOCIATES SHALL NOT
- * BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING,
- * MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
- */
-
-
- package symantec.itools.db.awt.genutil;
-
- import java.util.*;
-
- public class Timer extends Observable implements Runnable {
- Hashtable callbacks = new Hashtable();
-
- private static Timer timer;
-
- Thread timerThread;
-
- public static Timer getTimer() {
- if (timer == null) {
- timer = new Timer();
- }
-
- return timer;
- }
-
- private Timer() {
- createTimer();
- }
-
- private synchronized void createTimer() {
- if (timerThread == null) {
- timerThread = new Thread(this);
- timerThread.start();
- }
- }
-
- public void register(Observer obs, int interval) {
- TimerRegistration reg = new TimerRegistration(obs, interval);
- callbacks.put(obs, reg);
- }
-
- public synchronized void activate(Observer obs) {
- TimerRegistration reg = (TimerRegistration)callbacks.get(obs);
- reg.activate();
- notify();
- }
-
- public synchronized void activate(Observer obs, int interval) {
- TimerRegistration reg = (TimerRegistration)callbacks.get(obs);
- reg.updateInterval(interval);
- reg.activate();
- notify();
- }
-
- public void inactivate(Observer obs) {
- TimerRegistration reg = (TimerRegistration)callbacks.get(obs);
- reg.inactivate();
- }
-
- public synchronized void updateInterval(Observer obs, int newInterval) {
- TimerRegistration reg = (TimerRegistration)callbacks.get(obs);
- reg.updateInterval(newInterval);
- notify();
- }
-
- public int getInterval(Observer obs) {
- TimerRegistration reg = (TimerRegistration)callbacks.get(obs);
- return reg.getInterval();
- }
-
- //the timer routine
- public synchronized void run() {
- Thread current = Thread.currentThread();
- if (current != timerThread) { return; }
-
- long nextWakeup;
-
- while(true) {
- while (true) {
- waitForCallbacks();
- if (activeRegistrations()) {
- break;
- } else {
- try {
- wait();
- }catch(Exception ex) {}
- }
- }
-
- do {
- nextWakeup = findNextWakeup();
- waitTill(nextWakeup);
- } while(System.currentTimeMillis() < nextWakeup);
-
- doCallbacks();
- }
- }
-
- private synchronized void waitForCallbacks() {
- if (callbacks.size() == 0) {
- try {
- wait();
- } catch(InterruptedException ex) {}
- }
- }
-
- private synchronized boolean activeRegistrations() {
- Enumeration e = callbacks.elements();
-
- while(e.hasMoreElements()) {
- TimerRegistration reg = (TimerRegistration)e.nextElement();
- if (reg.isActive()) {
- return true;
- }
- }
-
- return false;
- }
-
- private synchronized long findNextWakeup() {
- Enumeration e = callbacks.elements();
- long next = 0;
- long temp;
-
- while(e.hasMoreElements()) {
- TimerRegistration reg = (TimerRegistration)e.nextElement();
- if (!reg.isActive()) { continue; }
-
- temp = reg.next();
- if (next == 0 || temp < next) {
- next = temp;
- }
- }
- return next;
- }
-
- private synchronized void waitTill(long till) {
- long current = System.currentTimeMillis();
- long time = till - current;
-
- try {
- if (time > 10) {
- wait(time);
- } else {
- timerThread.sleep (Math.max(time, 0));
- }
- } catch(InterruptedException ex) {}
- }
-
- private synchronized void doCallbacks() {
- Enumeration e = callbacks.elements();
- long time = System.currentTimeMillis();
-
- while(e.hasMoreElements()) {
- TimerRegistration reg = (TimerRegistration)e.nextElement();
- reg.callIfRequired(this, time);
- }
- }
- }
-
-