KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jtheory > jdring > AlarmWaiter


1 /*
2  * com/jtheory/jdring/AlarmWaiter.java
3  * Copyright (C) 1999 - 2004 jtheory creations, Olivier Dedieu et al.
4  *
5  * This library is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU Library General Public License as published
7  * by the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

19
20 package com.jtheory.jdring;
21
22 /**
23  * This class manages the thread which sleeps until the next alarm.
24  * Methods are synchronized to prevent interference from the AlarmWaiter
25  * thread and external threads.
26  *
27  * @author Olivier Dedieu, David Sims, Jim Lerner, Rob Whelan
28  * @version 1.4, 2004/04/02
29  */

30 public class AlarmWaiter implements Runnable JavaDoc {
31     protected AlarmManager mgr;
32     protected Thread JavaDoc thread;
33     private long sleepUntil = -1;
34     private boolean debug = false;
35     private boolean shutdown = false;
36     
37     private void debug(String JavaDoc s) {
38         if (debug)
39             System.out.println("[" + Thread.currentThread().getName() + "] AlarmWaiter: " + s);
40     }
41     
42     /**
43      * Creates a new AlarmWaiter.
44      *
45      * @param isDaemon true if the waiter thread should run as a daemon.
46      * @param threadName the name of the waiter thread
47      */

48     public AlarmWaiter(AlarmManager mgr, boolean isDaemon, String JavaDoc waiterName) {
49         this.mgr = mgr;
50         
51         // start the thread
52
thread = new Thread JavaDoc(this, waiterName);
53         thread.setPriority( 1 );
54         thread.setDaemon(isDaemon);
55         thread.start();
56     }
57     
58     /**
59      * Updates the time to sleep.
60      *
61      * @param _sleep_until the new time to sleep until.
62      */

63     public synchronized void update(long _sleep_until) {
64         this.sleepUntil = _sleep_until;
65         debug("Update for " + _sleep_until); // timeToSleep);
66
debug("calling notify() to update thread wait timeout");
67         notify();
68     }
69     
70     /**
71      * Restarts the thread for a new time to sleep until.
72      *
73      * @param _sleep_until the new time to sleep until.
74      */

75     public synchronized void restart(long _sleep_until) {
76         this.sleepUntil = _sleep_until;
77         notify();
78     }
79     
80     /**
81      * Stops (destroy) the thread.
82      */

83     public synchronized void stop() {
84         shutdown = true;
85         notify();
86     }
87     
88     
89     public synchronized void run() {
90         debug("running");
91         while(!shutdown) {
92             try {
93                 // check if there's an alarm scheduled
94
if (sleepUntil <= 0) {
95                     // no alarm. Wait for a new alarm to come along.
96
wait();
97                 } // if
98
else {
99                     // Found alarm, set timeout based on alarm time
100
long timeout = sleepUntil - System.currentTimeMillis();
101                     if (timeout > 0) {
102                         wait(timeout);
103                     }
104                 }
105                 
106                 // now that we've awakened again, check if an alarm is due (within
107
// 1 second or already past)
108
if (sleepUntil >= 0 && (sleepUntil - System.currentTimeMillis() < 1000)) {
109                     // yes, an alarm is ready (or already past). Notify the manager to ring it.
110
sleepUntil = -1;
111                     debug("notifying manager to ring next alarm");
112                     mgr.ringNextAlarm();
113                 }
114                 
115             }
116             catch(InterruptedException JavaDoc e) {
117                 debug("interrupted");
118             }
119         }
120         debug("stopping");
121     }
122     
123 }
124
125
126
Popular Tags