File: Timer.java

package info (click to toggle)
libpj-java 0.0~20150107%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 13,396 kB
  • sloc: java: 99,543; ansic: 987; sh: 153; xml: 26; makefile: 10; sed: 4
file content (474 lines) | stat: -rw-r--r-- 16,683 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
//******************************************************************************
//
// File:    Timer.java
// Package: edu.rit.util
// Unit:    Class edu.rit.util.Timer
//
// This Java source file is copyright (C) 2002-2004 by Alan Kaminsky. All rights
// reserved. For further information, contact the author, Alan Kaminsky, at
// ark@cs.rit.edu.
//
// This Java source file is part of the Parallel Java Library ("PJ"). PJ is free
// software; you can redistribute it and/or modify it under the terms of the GNU
// General Public License as published by the Free Software Foundation; either
// version 3 of the License, or (at your option) any later version.
//
// PJ is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
// A PARTICULAR PURPOSE. See the GNU General Public License for more details.
//
// Linking this library statically or dynamically with other modules is making a
// combined work based on this library. Thus, the terms and conditions of the
// GNU General Public License cover the whole combination.
//
// As a special exception, the copyright holders of this library give you
// permission to link this library with independent modules to produce an
// executable, regardless of the license terms of these independent modules, and
// to copy and distribute the resulting executable under terms of your choice,
// provided that you also meet, for each linked independent module, the terms
// and conditions of the license of that module. An independent module is a
// module which is not derived from or based on this library. If you modify this
// library, you may extend this exception to your version of the library, but
// you are not obligated to do so. If you do not wish to do so, delete this
// exception statement from your version.
//
// A copy of the GNU General Public License is provided in the file gpl.txt. You
// may also obtain a copy of the GNU General Public License on the World Wide
// Web at http://www.gnu.org/licenses/gpl.html.
//
//******************************************************************************

package edu.rit.util;

import java.util.Date;

/**
 * Class Timer controls the execution of a {@linkplain TimerTask}'s timed
 * actions.
 * <P>
 * A timer is in one of three states as shown by this state diagram:
 * <P>
 * <FONT SIZE="-1">
 * <PRE>
 *       +---+                   +---+
 *       |   | stop              |   | start
 *       |   v                   |   v
 *    +---------+    start    +---------+   timeout   +-----------+
 *    |         |------------&gt;|         |------------&gt;|           |
 * --&gt;| STOPPED |             | STARTED |             | TRIGGERED |
 *    |         |&lt;------------|         |&lt;------------|           |
 *    +---------+     stop    +---------+    start    +-----------+
 *       ^   ^                     ^                     |     |
 *       |   |                     |            action() |     | stop
 *       |   |                     |           performed |     |
 *       |   |                     |                     |     |
 *       |   |                     | periodic timeout    |     |
 *       |   |                     +---------------------+     |
 *       |   |                       one-shot timeout    |     |
 *       |   +-------------------------------------------+     |
 *       |                                                     |
 *       +-----------------------------------------------------+
 * </PRE>
 * </FONT>
 * <P>
 * When a timer is created, it is associated with a {@linkplain TimerTask}. The
 * timer is initially <B>stopped.</B> The timer can then be <B>started</B> in
 * various ways. The timer will then <B>time out</B> at some instant, depending
 * on how it was started. When the timer times out, the timer becomes
 * <B>triggered,</B> and the timer's timer task's <TT>action()</TT> method is
 * called. The timer task can stop the timer, or it can start the timer again
 * for a different timeout, or it can leave the timer alone. If the timer task
 * does not start or stop the timer, the timer goes into a state determined by
 * the way the timer was originally started. If the timer was started with a
 * <B>periodic timeout,</B> the timer automatically starts itself to time out
 * again after a certain interval. If the timer was started with a <B>one-shot
 * timeout,</B> the timer automatically stops itself.
 * <P>
 * By calling the appropriate method, a timer may be started to do timeouts in
 * any of the following ways:
 * <UL>
 * <LI>
 * <B>One-shot timeout.</B> The timer task's action is performed just once at a
 * certain point in time, specified either as an absolute time or as an interval
 * from now.
 * <BR>&nbsp;
 * <LI>
 * <B>Periodic fixed-rate timeout.</B> The timer task's action is performed
 * repeatedly at a given interval. The first action happens at a certain point
 * in time, specified either as an absolute time or as an interval from now.
 * Thereafter, each subsequent action starts at the given repetition interval
 * after the <I>scheduled start</I> of the previous action. Thus, the interval
 * between the scheduled starts of successive actions is always the same,
 * regardless of how long each action takes to complete, and the interval
 * between successive actions may vary, depending on how long each action takes
 * to complete.
 * <BR>&nbsp;
 * <LI>
 * <B>Periodic fixed-interval timeout.</B> The timer task's action is performed
 * repeatedly at a given interval. The first action happens at a certain point
 * in time, specified either as an absolute time or as an interval from now.
 * Thereafter, each subsequent action starts at the given repetition interval
 * after the <I>end</I> of the previous action. Thus, the interval between the
 * starts of successive actions may vary, depending on how long each action
 * takes to complete, and the interval between successive actions is always the
 * same, regardless of how long each action takes to complete.
 * </UL>
 * <P>
 * A Timer object is not constructed directly. Rather, a timer object is created
 * by calling a {@linkplain TimerThread}'s <TT>createTimer()</TT> method, giving
 * a timer task to associate with the timer. A single timer thread can control
 * any number of timers. The timer thread is responsible for doing all the
 * timers' timeouts and causing the timers to call the timer tasks'
 * <TT>action()</TT> methods at the proper times. Since a timer thread does not
 * provide real-time guarantees, the time at which a timer task's action
 * actually starts may be later than when the timeout occurred.
 * <P>
 * Classes Timer, {@linkplain TimerTask}, and {@linkplain TimerThread} provide
 * capabilities similar to classes java.util.Timer and java.util.TimerTask.
 * Unlike the latter, they also provide the ability to stop and restart a timer
 * and the ability to deal with race conditions in multithreaded programs.
 *
 * @author  Alan Kaminsky
 * @version 15-Dec-2002
 */
public class Timer
	{

// Hidden data members.

	// Timer thread which created this timer.
	private TimerThread myTimerThread;

	// Associated timer task.
	private TimerTask myTimerTask;

	// State of this timer.
	private int myState = STOPPED;
		private static final int STOPPED   = 0;
		private static final int STARTED   = 1;
		private static final int TRIGGERED = 2;

	// Kind of timeout.
	private int myKind;
		private static final int ONE_SHOT_TIMEOUT       = 0;
		private static final int FIXED_RATE_TIMEOUT     = 1;
		private static final int FIXED_INTERVAL_TIMEOUT = 2;

	// Time at which this timer is next scheduled to time out (milliseconds
	// since midnight 01-Jan-1970 UTC).
	private long myTimeout;

	// Interval for periodic timeouts (milliseconds).
	private long myInterval;

// Hidden constructors.

	/**
	 * Construct a new timer.
	 *
	 * @param  theTimerThread  Timer thread.
	 * @param  theTimerTask    Timer task.
	 *
	 * @exception  NullPointerException
	 *     (unchecked exception) Thrown if <TT>theTimerTask</TT> is null.
	 */
	Timer
		(TimerThread theTimerThread,
		 TimerTask theTimerTask)
		{
		if (theTimerTask == null)
			{
			throw new NullPointerException();
			}
		myTimerThread = theTimerThread;
		myTimerTask = theTimerTask;
		}

// Exported operations.

	/**
	 * Start this timer with a one-shot timeout at the given absolute time. If
	 * the time denotes a point in the past, the action is performed
	 * immediately.
	 *
	 * @param  theTime
	 *     Absolute time at which to perform the action.
	 */
	public synchronized void start
		(Date theTime)
		{
		myState = STARTED;
		myKind = ONE_SHOT_TIMEOUT;
		myTimeout = theTime.getTime();
		myTimerThread.schedule (myTimeout, this);
		}

	/**
	 * Start this timer with a one-shot timeout at the given interval from now.
	 * If the interval is less than or equal to zero, the action is performed
	 * immediately.
	 *
	 * @param  theInterval
	 *     Timeout interval before performing the action (milliseconds).
	 */
	public synchronized void start
		(long theInterval)
		{
		myState = STARTED;
		myKind = ONE_SHOT_TIMEOUT;
		myTimeout = System.currentTimeMillis() + theInterval;
		myTimerThread.schedule (myTimeout, this);
		}

	/**
	 * Start this timer with a periodic fixed-rate timeout starting at the given
	 * absolute time. If the start time denotes a point in the past, the first
	 * action is performed immediately. Each subsequent action is started at the
	 * given repetition interval after the scheduled start of the previous
	 * action.
	 *
	 * @param  theFirstTime
	 *     Absolute time at which to perform the first action.
	 * @param  theRepetitionInterval
	 *     Interval between successive actions (milliseconds).
	 *
	 * @exception  IllegalArgumentException
	 *     (unchecked exception) Thrown if <TT>theRepetitionInterval</TT> &lt;=
	 *     0.
	 */
	public synchronized void start
		(Date theFirstTime,
		 long theRepetitionInterval)
		{
		if (theRepetitionInterval <= 0)
			{
			throw new IllegalArgumentException();
			}
		myState = STARTED;
		myKind = FIXED_RATE_TIMEOUT;
		myTimeout = theFirstTime.getTime();
		myInterval = theRepetitionInterval;
		myTimerThread.schedule (myTimeout, this);
		}

	/**
	 * Start this timer with a periodic fixed-rate timeout starting at the given
	 * interval from now. If the interval is less than or equal to zero, the
	 * first action is performed immediately. Each subsequent action is started
	 * at the given repetition interval after the scheduled start of the
	 * previous action.
	 *
	 * @param  theFirstInterval
	 *     Timeout interval before performing the first action (milliseconds).
	 * @param  theRepetitionInterval
	 *     Interval between successive actions (milliseconds).
	 *
	 * @exception  IllegalArgumentException
	 *     (unchecked exception) Thrown if <TT>theRepetitionInterval</TT> &lt;=
	 *     0.
	 */
	public synchronized void start
		(long theFirstInterval,
		 long theRepetitionInterval)
		{
		if (theRepetitionInterval <= 0)
			{
			throw new IllegalArgumentException();
			}
		myState = STARTED;
		myKind = FIXED_RATE_TIMEOUT;
		myTimeout = System.currentTimeMillis() + theFirstInterval;
		myInterval = theRepetitionInterval;
		myTimerThread.schedule (myTimeout, this);
		}

	/**
	 * Start this timer with a periodic fixed-interval timeout starting at the
	 * given absolute time. If the start time denotes a point in the past, the
	 * first action is performed immediately. Each subsequent action is started
	 * at the given repetition interval after the end of the previous action.
	 *
	 * @param  theFirstTime
	 *     Absolute time at which to perform the first action.
	 * @param  theRepetitionInterval
	 *     Interval between successive actions (milliseconds).
	 *
	 * @exception  IllegalArgumentException
	 *     (unchecked exception) Thrown if <TT>theRepetitionInterval</TT> &lt;=
	 *     0.
	 */
	public synchronized void startFixedIntervalTimeout
		(Date theFirstTime,
		 long theRepetitionInterval)
		{
		if (theRepetitionInterval <= 0)
			{
			throw new IllegalArgumentException();
			}
		myState = STARTED;
		myKind = FIXED_INTERVAL_TIMEOUT;
		myTimeout = theFirstTime.getTime();
		myInterval = theRepetitionInterval;
		myTimerThread.schedule (myTimeout, this);
		}

	/**
	 * Start this timer with a periodic fixed-interval timeout starting at the
	 * given interval from now. If the interval is less than or equal to zero,
	 * the first action is performed immediately. Each subsequent action is
	 * started at the given repetition interval after the end of the previous
	 * action.
	 *
	 * @param  theFirstInterval
	 *     Timeout interval before performing the first action (milliseconds).
	 * @param  theRepetitionInterval
	 *     Interval between successive actions (milliseconds).
	 *
	 * @exception  IllegalArgumentException
	 *     (unchecked exception) Thrown if <TT>theRepetitionInterval</TT> &lt;=
	 *     0.
	 */
	public synchronized void startFixedIntervalTimeout
		(long theFirstInterval,
		 long theRepetitionInterval)
		{
		if (theRepetitionInterval <= 0)
			{
			throw new IllegalArgumentException();
			}
		myState = STARTED;
		myKind = FIXED_INTERVAL_TIMEOUT;
		myTimeout = System.currentTimeMillis() + theFirstInterval;
		myInterval = theRepetitionInterval;
		myTimerThread.schedule (myTimeout, this);
		}

	/**
	 * Stop this timer.
	 */
	public synchronized void stop()
		{
		myState = STOPPED;
		}

	/**
	 * Determine whether this timer is stopped.
	 *
	 * @return  True if this timer is in the stopped state, false otherwise.
	 */
	public synchronized boolean isStopped()
		{
		return myState == STOPPED;
		}

	/**
	 * Determine whether this timer is started.
	 *
	 * @return  True if this timer is in the started state, false otherwise.
	 */
	public synchronized boolean isStarted()
		{
		return myState == STARTED;
		}

	/**
	 * Determine whether this timer is triggered.
	 *
	 * @return  True if this timer is in the triggered state, false otherwise.
	 */
	public synchronized boolean isTriggered()
		{
		return myState == TRIGGERED;
		}

	/**
	 * Determine the time when this timer is or was scheduled to time out.
	 * <P>
	 * If the <TT>getTimeout()</TT> method is called when this timer is in the
	 * stopped state, then <TT>Long.MAX_VALUE</TT> is returned.
	 * <P>
	 * If the <TT>getTimeout()</TT> method is called when this timer is in the
	 * started state, then the <TT>getTimeout()</TT> method returns the time
	 * when the next timeout will occur.
	 * <P>
	 * If the <TT>getTimeout()</TT> method is called when this timer is in the
	 * triggered state, such as by this timer's timer task's <TT>action()</TT>
	 * method, then the <TT>getTimeout()</TT> method returns the time when the
	 * present timeout was <I>scheduled</I> to occur. Since a timer thread
	 * provides no real-time guarantees, the time when the timeout
	 * <I>actually</I> occurred may be some time later. If desired, the caller
	 * can compare the scheduled timeout to the actual time and decide whether
	 * it's too late to perform the action.
	 *
	 * @return  Time of scheduled timeout (milliseconds since midnight
	 *          01-Jan-1970 UTC).
	 */
	public synchronized long getTimeout()
		{
		return myState == STOPPED ? Long.MAX_VALUE : myTimeout;
		}

	/**
	 * Returns this timer's timer task.
	 */
	public TimerTask getTimerTask()
		{
		return myTimerTask;
		}

// Hidden operations.

	/**
	 * Trigger this timer.
	 *
	 * @param  theTriggerTime
	 *     Time at which the trigger occurred (milliseconds since midnight
	 *     01-Jan-1970 UTC).
	 */
	void trigger
		(long theTriggerTime)
		{
		synchronized (this)
			{
			// Make sure we're started and it really is time to trigger.
			if (myState != STARTED || myTimeout > theTriggerTime)
				{
				return;
				}

			// Switch to the triggered state.
			myState = TRIGGERED;
			}

		// Call the timer task's action() method. Do it outside the synchronized
		// block, otherwise a deadlock may happen if another thread tries to
		// start or stop the timer.
		myTimerTask.action (this);

		synchronized (this)
			{
			// Decide whether to do an automatic restart.
			if (myState != TRIGGERED)
				{
				// Someone already stopped or restarted us. Do nothing.
				}
			else if (myKind == FIXED_RATE_TIMEOUT)
				{
				myState = STARTED;
				myTimeout += myInterval;
				myTimerThread.schedule (myTimeout, this);
				}
			else if (myKind == FIXED_INTERVAL_TIMEOUT)
				{
				myState = STARTED;
				myTimeout = System.currentTimeMillis() + myInterval;
				myTimerThread.schedule (myTimeout, this);
				}
			else // ONE_SHOT_TIMEOUT
				{
				myState = STOPPED;
				}
			}
		}

	}