KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jdon > util > task > TaskEngine


1 package com.jdon.util.task;
2
3
4 import java.util.LinkedList JavaDoc;
5 import java.util.Timer JavaDoc;
6 import java.util.TimerTask JavaDoc;
7
8 /**
9  * A TaskEngine object accepts Runnable objects and queues them for execution by
10  * worker threads.<p>
11  *
12  * Note: the current implementation of this class ignores priority. This may
13  * be changed in the future, however.
14  *
15  * this is s simple task/job engine, more power see Quartz or JMS
16  *
17  * usage:
18  * <pre>
19  * TaskEngine taskEngine = new TaskEngine();
20  * MyTask myTask = new MyTask();
21  * engine.addTask(myTask, Thread.NORM_PRIORITY);
22  * </pre>
23  * MyTask must implements Runnable of Thread.
24  *
25  */

26 public class TaskEngine {
27
28     /**
29      * A queue of tasks to be executed.
30      */

31     private static LinkedList JavaDoc taskList = null;
32
33     /**
34      * An array of worker threads.
35      */

36     private static Thread JavaDoc[] workers = null;
37
38     /**
39      * A Timer to perform periodic tasks.
40      */

41     private static Timer JavaDoc taskTimer = null;
42
43     private static Object JavaDoc lock = new Object JavaDoc();
44
45     static {
46         //Initialize the task timer and make it a deamon.
47
taskTimer = new Timer JavaDoc(true);
48         // Use 7 worker threads by default.
49
workers = new Thread JavaDoc[7];
50         taskList = new LinkedList JavaDoc();
51         for (int i=0; i<workers.length; i++) {
52             TaskEngineWorker worker = new TaskEngineWorker();
53             workers[i] = new Thread JavaDoc(worker);
54             workers[i].setDaemon(true);
55             workers[i].start();
56         }
57     }
58
59     private TaskEngine() {
60         // Not instantiable.
61
}
62
63     /**
64      * Adds a task to the task queue with normal priority. The task will be
65      * executed immediately provided there is a free worker thread to execute
66      * it. Otherwise, it will execute as soon as a worker thread becomes
67      * available.
68      */

69     public static void addTask(Runnable JavaDoc r) {
70         addTask(r, Thread.NORM_PRIORITY);
71     }
72
73     /**
74      * Adds a task to the task queue. The task will be executed immediately
75      * provided there is a free worker thread to execute it. Otherwise, it
76      * will execute as soon as a worker thread becomes available.<p>
77      *
78      * The priority of the task can be specified and must be one of the
79      * following values:<ul>
80      * <li> Thread.MAX_PRIORITY
81      * <li> Thread.NORM_PRIORITY
82      * <li> Thread.MIN_PRIORITY
83      * </ul>
84      *
85      * @param task the task to execute
86      */

87     public static void addTask(Runnable JavaDoc task, int priority) {
88         synchronized(lock) {
89             taskList.addFirst(task);
90             // Notify the worker threads. The notifyAll() methods notifies all
91
// threads waiting on this object.
92
lock.notifyAll();
93         }
94     }
95
96     /**
97      * Schedules a task to periodically run. This is useful for tasks such as
98      * updating search indexes, deleting old data at periodic intervals, etc.
99      *
100      * @param task task to be scheduled.
101      * @param delay delay in milliseconds before task is to be executed.
102      * @param period time in milliseconds between successive task executions.
103      * @return a TimerTask object which can be used to track executions of the
104      * task and to cancel subsequent executions.
105      */

106     public static TimerTask JavaDoc scheduleTask(Runnable JavaDoc task, long delay,
107             long period)
108     {
109         TimerTask JavaDoc timerTask = new ScheduledTask(task);
110         taskTimer.scheduleAtFixedRate(timerTask, delay, period);
111         return timerTask;
112     }
113
114     /**
115      * Schedules a task to periodically run. This is useful for tasks such as
116      * updating search indexes, deleting old data at periodic intervals, etc.
117      *
118      * @param task task to be scheduled.
119      * @param priority the priority the periodic task should run at.
120      * @param delay delay in milliseconds before task is to be executed.
121      * @param period time in milliseconds between successive task executions.
122      * @return a TimerTask object which can be used to track executions of the
123      * task and to cancel subsequent executions.
124      */

125     public static TimerTask JavaDoc scheduleTask(Runnable JavaDoc task, int priority,
126             long delay, long period)
127     {
128         TimerTask JavaDoc timerTask = new ScheduledTask(task, priority);
129         taskTimer.scheduleAtFixedRate(timerTask, delay, period);
130         return timerTask;
131     }
132
133     /**
134      * Return the next task in the queue. If no task is available, this method
135      * will block until a task is added to the queue.
136      *
137      * @return a <code>Task</code> object
138      */

139     private static Runnable JavaDoc nextTask() {
140         synchronized(lock) {
141             // Block until we have another object in the queue to execute.
142
while (taskList.isEmpty()) {
143                 try {
144                     lock.wait();
145                 }
146                 catch (InterruptedException JavaDoc ie) { }
147             }
148             return (Runnable JavaDoc)taskList.removeLast();
149         }
150     }
151
152     /**
153      * A worker thread class which executes <code>Task</code> objects.
154      */

155     private static class TaskEngineWorker implements Runnable JavaDoc {
156
157         private boolean done = false;
158
159         /**
160          * Get tasks from the task engine. The call to get another task will
161          * block until there is an available task to execute.
162          */

163         public void run() {
164             while (!done) {
165                 nextTask().run();
166             }
167         }
168     }
169
170     /**
171      * A subclass of TimerClass that passes along a Runnable to the task engine
172      * when the scheduled task is run.
173      */

174     private static class ScheduledTask extends TimerTask JavaDoc {
175
176         private Runnable JavaDoc task;
177         private int priority;
178
179         public ScheduledTask(Runnable JavaDoc task) {
180             this(task, Thread.NORM_PRIORITY);
181         }
182
183         public ScheduledTask(Runnable JavaDoc task, int priority) {
184             this.task = task;
185             this.priority = priority;
186         }
187
188         public void run() {
189             //Put the task into the queue to be run as soon as possible by a
190
//worker.
191
addTask(task);
192         }
193     }
194 }
195
Popular Tags