Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libGDBMS / src / com / hardcode / gdbms / engine / internalExceptions / Timer.java @ 1956

History | View | Annotate | Download (1.59 KB)

1
package com.hardcode.gdbms.engine.internalExceptions;
2

    
3
/**
4
 * Reseteable timer. After a cancelTimer or after the execution of the
5
 * scheduled task the object becomes unuseful: It's not possible to schedule
6
 * more tasks
7
 *
8
 * @author Fernando Gonz?lez Cort?s
9
 */
10
public class Timer {
11
        private boolean cancel = false;
12
        private Thread timerThread;
13

    
14
        /**
15
         * schedules a task
16
         *
17
         * @param task task to schedule
18
         * @param delay delay in milliseconds
19
         */
20
        public void schedule(Task task, long delay) {
21
                timerThread = new Thread(new TimerThread(delay, task));
22
                timerThread.start();
23
        }
24

    
25
        /**
26
         * restarts the timer
27
         */
28
        public void resetTimer() {
29
                cancel = false;
30
                timerThread.interrupt();
31
        }
32

    
33
        /**
34
         * cancels the timer. It causes the timer to be unuseful. After this call
35
         * you cannot schedule more tasks
36
         */
37
        public void cancelTimer() {
38
                cancel = true;
39
                timerThread.interrupt();
40
        }
41

    
42
        /**
43
         * timer thread
44
         *
45
         * @author Fernando Gonz?lez Cort?s
46
         */
47
        public class TimerThread implements Runnable {
48
                long timeout;
49
                Task task;
50

    
51
                /**
52
                 * Creates a new TimerThread.
53
                 *
54
                 * @param timeout delay in milliseconds
55
                 * @param task task to schedule
56
                 */
57
                public TimerThread(long timeout, Task task) {
58
                        this.timeout = timeout;
59
                        this.task = task;
60
                }
61

    
62
                /**
63
                 * @see java.lang.Runnable#run()
64
                 */
65
                public synchronized void run() {
66
                        while (true) {
67
                                try {
68
                                        // Waits the delay
69
                                        wait(timeout);
70

    
71
                                        //executes the task
72
                                        task.execute();
73

    
74
                                        //exits
75
                                        break;
76
                                } catch (InterruptedException e) {
77
                                        if (cancel) {
78
                                                break;
79
                                        } else {
80
                                                //Volvemos a esperar
81
                                        }
82
                                }
83
                        }
84
                }
85
        }
86
}