1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.xnap.util;
21
22 import java.util.Timer;
23 import java.util.TimerTask;
24
25 /***
26 * Provides a single thread scheduler for maintenance tasks.
27 *
28 * <p>All tasks are executed sequentially. Therefore tasks should complete
29 * quickly. If a timer task takes excessive time to complete, it might block
30 * the execution of subsequent tasks.</p>
31 *
32 * @see java.util.Timer
33 */
34 public class Scheduler {
35
36
37
38
39
40 private static Timer timer = new Timer(true);
41
42
43
44 private Scheduler()
45 {
46 }
47
48
49
50 /***
51 * Schedules <code>task</code> for execution in <code>delay</code> milli
52 * seconds.
53 */
54 public static void run(long delay, Runnable task)
55 {
56 timer.schedule(new SimpleTimerTask(task).getTimerTask(), delay);
57 }
58
59 /***
60 * Use {@link #run(long, XNapTask)} instead.
61 *
62 * @deprecated
63 */
64 public static void run(long delay, TimerTask task)
65 {
66 timer.schedule(task, delay);
67 }
68
69 /***
70 * Schedules <code>task</code> for execution in <code>delay</code> milli
71 * seconds.
72 */
73 public static void run(long delay, XNapTask task)
74 {
75 timer.schedule(task.getTimerTask(), delay);
76 }
77
78 /***
79 * Schedules <code>task</code> for execution in <code>delay</code> milli
80 * seconds and from that point for repeated executions
81 * in <code>interval</code> milli seconds.
82 */
83 public static void run(long delay, long interval, Runnable task)
84 {
85 timer.schedule(new SimpleTimerTask(task).getTimerTask(), delay, interval);
86 }
87
88 /***
89 * Use {@link #run(long, long, XNapTask)} instead.
90 *
91 * @deprecated
92 */
93 public static void run(long delay, long interval, TimerTask task)
94 {
95 timer.schedule(task, delay, interval);
96 }
97
98 /***
99 * Schedules <code>task</code> for execution in <code>delay</code> milli
100 * seconds and from that point for repeated executions
101 * in <code>interval</code> milli seconds.
102 */
103 public static void run(long delay, long interval, XNapTask task)
104 {
105 timer.schedule(task.getTimerTask(), delay, interval);
106 }
107
108 /***
109 * Provides a simple timer task that executes a runnable.
110 */
111 public static class SimpleTimerTask extends XNapTask {
112
113 Runnable runnable;
114
115 public SimpleTimerTask(Runnable runnable)
116 {
117 this.runnable = runnable;
118 }
119
120 public void run()
121 {
122 runnable.run();
123 }
124
125 }
126
127 }
128
129
130
131
132