home *** CD-ROM | disk | FTP | other *** search
/ Symantec Visual Cafe for Java 2.5 / symantec-visual-cafe-2.5-database-dev-edition.iso / VCafe / prosrc.bin / Timer.java < prev    next >
Encoding:
Java Source  |  1998-03-18  |  5.8 KB  |  187 lines

  1. /*
  2.  * Copyright (c) 1997 Krumel & Associates, Inc. All Rights Reserved.
  3.  *
  4.  * www.krumel.com - controls@krumel.com
  5.  *
  6.  * Permission is given to the buyer of this package for one software
  7.  * developer to use this software on one CPU (one workstation) and to make
  8.  * one backup copy.  You may uitilize and/or modify this class for use in your
  9.  * projects.  You may distribute or sell any executable which results from
  10.  * using this code in yur application, except a utility or class of similar
  11.  * nature to this product.  You may distribute this product in compiled
  12.  * form only, but soley to be used with your cmpiled executable product
  13.  * for the puposes of dynamic loading. You may NOT redistribute the source
  14.  * code in any form or make it accessible through a network or other
  15.  * distribution media to others. Please refer to the file "copyright.html"
  16.  * for further important copyright and licensing information.
  17.  *
  18.  * The source code is the confidential and proprietary information
  19.  * of Krumel & Associates, Inc. ("Confidential Information").  You shall
  20.  * not disclose such Confidential Information and shall use it only in
  21.  * accordance with the terms of the license agreement you entered into
  22.  * with Krumel & Associates, Inc..
  23.  
  24.  * KRUMEL & ASSOCIATES MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE
  25.  * SUITABILITY OF THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT
  26.  * NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  27.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. KRUMEL & ASSOCIATES SHALL NOT
  28.  * BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING,
  29.  * MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  30.  */
  31.  
  32.  
  33. package symantec.itools.db.awt.genutil;
  34.  
  35. import java.util.*;
  36.  
  37. public class Timer extends Observable implements Runnable {
  38.     Hashtable callbacks = new Hashtable();
  39.  
  40.     private static Timer timer;
  41.  
  42.     Thread timerThread;
  43.  
  44.     public static Timer getTimer() {
  45.         if (timer == null) {
  46.             timer = new Timer();
  47.         }
  48.  
  49.         return timer;
  50.     }
  51.  
  52.     private Timer() {
  53.         createTimer();
  54.     }
  55.  
  56.     private synchronized void createTimer() {
  57.         if (timerThread == null) {
  58.             timerThread = new Thread(this);
  59.             timerThread.start();
  60.         }
  61.     }
  62.  
  63.     public void register(Observer obs, int interval) {
  64.         TimerRegistration reg = new TimerRegistration(obs, interval);
  65.         callbacks.put(obs, reg);
  66.     }
  67.  
  68.     public synchronized void activate(Observer obs) {
  69.         TimerRegistration reg = (TimerRegistration)callbacks.get(obs);
  70.         reg.activate();
  71.         notify();
  72.     }
  73.  
  74.     public synchronized void activate(Observer obs, int interval) {
  75.         TimerRegistration reg = (TimerRegistration)callbacks.get(obs);
  76.         reg.updateInterval(interval);
  77.         reg.activate();
  78.         notify();
  79.     }
  80.  
  81.     public void inactivate(Observer obs) {
  82.         TimerRegistration reg = (TimerRegistration)callbacks.get(obs);
  83.         reg.inactivate();
  84.     }
  85.  
  86.     public synchronized void updateInterval(Observer obs, int newInterval) {
  87.         TimerRegistration reg = (TimerRegistration)callbacks.get(obs);
  88.         reg.updateInterval(newInterval);
  89.         notify();
  90.     }
  91.  
  92.     public int getInterval(Observer obs) {
  93.         TimerRegistration reg = (TimerRegistration)callbacks.get(obs);
  94.         return reg.getInterval();
  95.     }
  96.  
  97.     //the timer routine
  98.     public synchronized void run() {
  99.         Thread current = Thread.currentThread();
  100.         if (current != timerThread) { return; }
  101.  
  102.         long nextWakeup;
  103.  
  104.         while(true) {
  105.             while (true) {
  106.                 waitForCallbacks();
  107.                 if (activeRegistrations()) {
  108.                     break;
  109.                 } else {
  110.                     try {
  111.                         wait();
  112.                     }catch(Exception ex) {}
  113.                 }
  114.             }
  115.  
  116.             do {
  117.                 nextWakeup = findNextWakeup();
  118.                 waitTill(nextWakeup);
  119.             } while(System.currentTimeMillis() < nextWakeup);
  120.  
  121.             doCallbacks();
  122.         }
  123.     }
  124.  
  125.     private synchronized void waitForCallbacks() {
  126.         if (callbacks.size() == 0) {
  127.             try {
  128.                 wait();
  129.             } catch(InterruptedException ex) {}
  130.         }
  131.     }
  132.  
  133.     private synchronized boolean activeRegistrations() {
  134.         Enumeration e = callbacks.elements();
  135.  
  136.         while(e.hasMoreElements()) {
  137.             TimerRegistration reg = (TimerRegistration)e.nextElement();
  138.             if (reg.isActive()) {
  139.                 return true;
  140.             }
  141.         }
  142.  
  143.         return false;
  144.     }
  145.  
  146.     private synchronized long findNextWakeup() {
  147.         Enumeration e = callbacks.elements();
  148.         long        next = 0;
  149.         long        temp;
  150.  
  151.         while(e.hasMoreElements()) {
  152.             TimerRegistration reg = (TimerRegistration)e.nextElement();
  153.             if (!reg.isActive()) { continue; }
  154.  
  155.             temp = reg.next();
  156.             if (next == 0 || temp < next) {
  157.                 next = temp;
  158.             }
  159.         }
  160.         return next;
  161.     }
  162.  
  163.     private synchronized void waitTill(long till) {
  164.         long current = System.currentTimeMillis();
  165.         long time = till - current;
  166.  
  167.         try {
  168.             if (time > 10) {
  169.                 wait(time);
  170.             } else {
  171.                 timerThread.sleep (Math.max(time, 0));
  172.             }
  173.         } catch(InterruptedException ex) {}
  174.     }
  175.  
  176.     private synchronized void doCallbacks() {
  177.         Enumeration e = callbacks.elements();
  178.         long        time = System.currentTimeMillis();
  179.  
  180.         while(e.hasMoreElements()) {
  181.             TimerRegistration reg = (TimerRegistration)e.nextElement();
  182.             reg.callIfRequired(this, time);
  183.         }
  184.     }
  185. }
  186.  
  187.