01: /**
02: * Objective Database Abstraction Layer (ODAL)
03: * Copyright (c) 2004, The ODAL Development Group
04: * All rights reserved.
05: * For definition of the ODAL Development Group please refer to LICENCE.txt file
06: *
07: * Distributable under LGPL license.
08: * See terms of license at gnu.org.
09: */package com.completex.objective.components.timer.impl;
10:
11: import com.completex.objective.components.timer.TimerListener;
12:
13: /**
14: * @author Gennady Krizhevsky
15: */
16: public class Timer extends Thread {
17: // TimerListener to receive TimerEvent notifications
18: private TimerListener timerListener;
19: // Number of milliseconds in each timer cycle
20: private long cycle;
21: private boolean stop;
22:
23: /**
24: * <p>Constructs a new Timer object
25: *
26: * @param timerListener Object that will receive TimerEvent
27: * notifications
28: * @param cycle Number of seconds in each timer cycle
29: * notification
30: */
31: public Timer(TimerListener timerListener, long cycle) {
32: this .timerListener = timerListener;
33: this .cycle = cycle;
34: }
35:
36: /**
37: * <p>Runs the timer. The timer will run until stopped and
38: * fire a TimerEvent notification every clock cycle
39: */
40: public void run() {
41: while (!stop) {
42: try {
43: sleep(cycle);
44: } catch (InterruptedException ex) {
45: // Do nothing
46: }
47:
48: if (timerListener != null) {
49: timerListener.timerEvent();
50: }
51: }
52: }
53:
54: public void setStop(boolean stop) {
55: this.stop = stop;
56: }
57: }
|