KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > rcm > util > Timer


1 /*
2  * Copyright (c) 1998-2002 Carnegie Mellon University. All rights
3  * reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in
14  * the documentation and/or other materials provided with the
15  * distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY CARNEGIE MELLON UNIVERSITY ``AS IS'' AND
18  * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
19  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY
21  * NOR ITS EMPLOYEES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  *
29  */

30
31 package rcm.util;
32
33 import java.util.Vector JavaDoc;
34
35 public class Timer {
36
37     int interval;
38     boolean periodic;
39     boolean isExpired = false;
40
41     static TimerManager manager = new TimerManager ();
42     long deadline;
43     Timer next, prev;
44
45     public Timer () {
46     }
47
48     public void set (int msecDelay, boolean periodic) {
49         interval = msecDelay;
50         this.periodic = periodic;
51         isExpired = false;
52         if (!manager.isAlive ()) {
53             System.err.println ("TimerManager: restarting");
54             manager = new TimerManager ();
55         }
56         manager.register (this, System.currentTimeMillis () + msecDelay);
57     }
58
59     public int getInterval () {
60         return interval;
61     }
62
63     public boolean getPeriodic () {
64         return periodic;
65     }
66
67     public void cancel () {
68         manager.delete (this);
69     }
70
71     protected void alarm () {
72     }
73
74     public boolean expired () {
75         return isExpired;
76     }
77         
78     /*
79     public static void main (String[] args) {
80         for (int i=0; i<args.length; ++i) {
81             boolean periodic = (args[i].charAt (0) == 'p');
82             if (periodic) args[i] = args[i].substring (1);
83             new TestTimer (args[i], Integer.parseInt (args[i]), periodic);
84         }
85         while (true) Thread.yield ();
86     }
87     */

88 }
89
90 class TimerManager extends Thread JavaDoc {
91     Timer first, last;
92
93     /*
94     static ThreadGroup rootThreadGroup;
95     static {
96         rootThreadGroup = Thread.currentThread().getThreadGroup();
97         while (rootThreadGroup.getParent() != null)
98             rootThreadGroup = rootThreadGroup.getParent();
99     }
100     */

101
102     public TimerManager () {
103         super (/* rootThreadGroup, */ "Timer Manager");
104         setDaemon (true);
105         start ();
106     }
107
108     public synchronized void register (Timer t, long deadline) {
109         t.deadline = deadline;
110         delete (t); // just in case it's already registered
111

112         //System.err.println ("TimerManager: set " + t + " to go off at " + deadline);
113
insertion:
114         {
115             for (Timer u = first; u != null; u = u.next) {
116                 if (t.deadline < u.deadline) {
117                     if (u.prev != null)
118                         u.prev.next = t;
119                     else
120                         first = t;
121                     t.prev = u.prev;
122                     t.next = u;
123                     u.prev = t;
124                     break insertion;
125                 }
126             }
127             if (last != null) {
128                 last.next = t;
129                 t.prev = last;
130                 t.next = null;
131                 last = t;
132             } else {
133                 first = last = t;
134             }
135         }
136
137         //System.err.println ("TimerManager: waking up background thread");
138
notifyAll ();
139     }
140
141     public synchronized void delete (Timer t) {
142         if (t.next != null)
143             t.next.prev = t.prev;
144         if (t.prev != null)
145             t.prev.next = t.next;
146         if (t == last)
147             last = t.prev;
148         if (t == first)
149             first = t.next;
150         t.next = null;
151         t.prev = null;
152     }
153
154     static final int FOREVER = 60000; // wake up at least every 60 seconds
155

156     public synchronized void run () {
157         while (true) {
158             try {
159                 //System.err.println ("TimerManager: awake");
160
if (first == null) {
161                     //System.err.println ("TimerManager: waiting forever");
162
wait (FOREVER);
163                     //System.err.println ("TimerManager: woke up");
164
}
165                 else {
166                     Timer t = first;
167                     long now = System.currentTimeMillis ();
168                     if (t.deadline <= now) {
169                         // System.err.println ("TimerManager: timer " + t + " just went off at " + now);
170
try {
171                             t.isExpired = true;
172                             t.alarm ();
173                         } catch (Throwable JavaDoc e) {
174                             if (e instanceof ThreadDeath JavaDoc)
175                                 throw (ThreadDeath JavaDoc)e;
176                             else
177                                 e.printStackTrace ();
178                         }
179                         if (t.periodic) {
180                             register (t, now + t.interval);
181                         }
182                         else {
183                             delete (t);
184                         }
185                     }
186                     else {
187                         //System.err.println ("TimerManager: waiting for " + (t.deadline - now) + " msec");
188
wait (t.deadline - now);
189                         //System.err.println ("TimerManager: woke up");
190
}
191                 }
192             } catch (InterruptedException JavaDoc e) {}
193         }
194     }
195 }
196
197 /*
198 class TestTimer extends Timer {
199     String message;
200
201     public TestTimer (String message, int millisec, boolean periodic) {
202         this.message = message;
203         set (millisec, periodic);
204     }
205
206     public void alarm () {
207         System.out.println (message);
208     }
209 }
210 */

211
Popular Tags