KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > debug > core > model > Timer


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.debug.core.model;
12
13
14 /**
15  * A timer notifies listeners when a specific amount
16  * of time has passed.
17  *
18  * @see ITimeoutListener
19  */

20 public class Timer {
21     
22     /**
23      * Listener to notify of a timeout
24      */

25     private ITimeoutListener fListener;
26     
27     /**
28      * Timeout value, in milliseconds
29      */

30     private int fTimeout;
31     
32     /**
33      * Whether this timer's thread is alive
34      */

35     private boolean fAlive = true;
36     
37     /**
38      * Whether this timer has been started and
39      * has not yet timed out or been stopped.
40      */

41     private boolean fStarted = false;
42     
43     /**
44      * The single thread used for each request.
45      */

46     private Thread JavaDoc fThread;
47     
48     /**
49      * Constructs a new timer
50      */

51     public Timer() {
52         setTimeout(Integer.MAX_VALUE);
53         Runnable JavaDoc r = new Runnable JavaDoc() {
54             public void run() {
55                 while (isAlive()) {
56                     boolean interrupted = false;
57                     try {
58                         Thread.sleep(getTimeout());
59                     } catch (InterruptedException JavaDoc e) {
60                         interrupted = true;
61                     }
62                     if (!interrupted) {
63                         if (getListener() != null) {
64                             setStarted(false);
65                             setTimeout(Integer.MAX_VALUE);
66                             getListener().timeout();
67                             setListener(null);
68                         }
69                     }
70                 }
71             }
72         };
73         setThread(new Thread JavaDoc(r, "Evaluation Timer")); //$NON-NLS-1$
74
getThread().setDaemon(true);
75         getThread().start();
76     }
77
78     /**
79      * Starts this timer, and notifies the given listener when
80      * the time has passed. A call to <code>stop</code>, before the
81      * time expires, will cancel the the timer and timeout callback.
82      * This method can only be called if this timer is idle (i.e.
83      * <code>isStarted() == false<code>).
84      *
85      * @param listener The timer listener
86      * @param ms The number of milliseconds to wait before
87      * notifying the listener
88      */

89     public void start(ITimeoutListener listener, int ms) {
90         if (isStarted()) {
91             throw new IllegalStateException JavaDoc(JDIDebugModelMessages.Timer_Timer_cannot_be_started_more_than_once_1);
92         }
93         setListener(listener);
94         setTimeout(ms);
95         setStarted(true);
96         getThread().interrupt();
97     }
98     
99     /**
100      * Stops this timer, cancelling any pending timeout
101      * notification.
102      */

103     public void stop() {
104         if (isAlive()) {
105             setStarted(false);
106             setTimeout(Integer.MAX_VALUE);
107             getThread().interrupt();
108         }
109     }
110     
111     /**
112      * Disposes this timer
113      */

114     public void dispose() {
115         if (isAlive()) {
116             setAlive(false);
117             getThread().interrupt();
118             setThread(null);
119         }
120     }
121     
122     /**
123      * Returns whether this timer's thread is alive
124      *
125      * @return whether this timer's thread is alive
126      */

127     private boolean isAlive() {
128         return fAlive;
129     }
130
131     /**
132      * Sets whether this timer's thread is alive. When
133      * set to <code>false</code> this timer's thread
134      * will exit on its next iteration.
135      *
136      * @param alive whether this timer's thread should
137      * be alive
138      * @see #dispose()
139      */

140     private void setAlive(boolean alive) {
141         fAlive = alive;
142     }
143
144     /**
145      * Returns the current timeout listener
146      *
147      * @return timeout listener
148      */

149     protected ITimeoutListener getListener() {
150         return fListener;
151     }
152
153     /**
154      * Sets the listener to be notified if this
155      * timer times out.
156      *
157      * @param listener timeout listener
158      */

159     private void setListener(ITimeoutListener listener) {
160         fListener = listener;
161     }
162
163     /**
164      * Returns whether this timer has been started,
165      * and has not yet timed out, or been stopped.
166      *
167      * @return whether this timer has been started,
168      * and has not yet timed out, or been stopped
169      */

170     public boolean isStarted() {
171         return fStarted;
172     }
173
174     /**
175      * Sets whether this timer has been started,
176      * and has not yet timed out, or been stopped.
177      *
178      * @param started whether this timer has been started,
179      * and has not yet timed out, or been stopped
180      */

181     private void setStarted(boolean started) {
182         fStarted = started;
183     }
184
185     /**
186      * Returns this timer's thread
187      *
188      * @return thread that waits for a timeout
189      */

190     private Thread JavaDoc getThread() {
191         return fThread;
192     }
193
194     /**
195      * Sets this timer's thread used to perform
196      * timeout processing
197      *
198      * @param thread thread that waits for a timeout
199      */

200     private void setThread(Thread JavaDoc thread) {
201         fThread = thread;
202     }
203
204     /**
205      * Returns the amount of time, in milliseconds, that
206      * this timer is/was waiting for.
207      *
208      * @return timeout value, in milliseconds
209      */

210     protected int getTimeout() {
211         return fTimeout;
212     }
213
214     /**
215      * Sets the amount of time, in milliseconds, that this
216      * timer will wait for before timing out.
217      *
218      * @param timeout value, in milliseconds
219      */

220     private void setTimeout(int timeout) {
221         fTimeout = timeout;
222     }
223 }
224
Popular Tags