KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > fenyo > gnetwatch > activities > Queue


1
2 /*
3  * GNetWatch
4  * Copyright 2006, 2007 Alexandre Fenyo
5  * gnetwatch@fenyo.net
6  *
7  * This file is part of GNetWatch.
8  *
9  * GNetWatch is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * GNetWatch is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with GNetWatch; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22  */

23
24 package net.fenyo.gnetwatch.activities;
25
26 import java.util.*;
27 import java.io.*;
28
29 import net.fenyo.gnetwatch.*;
30 import net.fenyo.gnetwatch.GUI.*;
31 import net.fenyo.gnetwatch.actions.*;
32
33 import org.apache.commons.logging.Log;
34 import org.apache.commons.logging.LogFactory;
35
36 /**
37  * Queue is the base class for any queue.
38  * It implements the main queueing process.
39  * @author Alexandre Fenyo
40  * @version $Id: Queue.java,v 1.23 2007/03/12 05:04:15 fenyo Exp $
41  */

42
43 public abstract class Queue extends VisualElement implements Runnable JavaDoc {
44   private static Log log = LogFactory.getLog(Queue.class);
45
46   private final String JavaDoc name;
47   private final Config config;
48   private final Thread JavaDoc thread;
49   private long max_time = 0;
50
51   // many threads access to actions
52
private final List<Action> actions =
53     Collections.synchronizedList(new LinkedList<Action>());
54   
55   // only the Queue thread accesses to localActions
56
private Vector<Action> actions_copy = null;
57
58   private Action current_action = null;
59
60   /**
61    * Constructor.
62    * @param name queue name.
63    * @param config configuration.
64    */

65   // main thread
66
protected Queue(final String JavaDoc name, final Config config) {
67     this.name = name;
68     this.config = config;
69     setType("task queue");
70     setItem(name);
71     setDescription("iterative queue");
72     thread = new Thread JavaDoc(this, name);
73     thread.start();
74   }
75
76   /**
77    * Sets the current GUI instance.
78    * @param GUI current GUI instance.
79    * @return void.
80    */

81   // final because called by constructor (by means of setItem())
82
protected final void initialize(final GUI gui) {
83     super.initialize(gui);
84     if (gui != null) setImageQueue();
85   }
86
87   /**
88    * Returns the size of the queue.
89    * @param none.
90    * @return int size.
91    */

92   public int size() {
93     return actions.size();
94   }
95
96   /**
97    * Adds a new action to this queue.
98    * @param action new action to add.
99    * @return void.
100    */

101   // GUI thread
102
public void addAction(final Action action) {
103     actions.add(action);
104   }
105
106   /**
107    * Removes an action from this queue.
108    * @param action action to remove.
109    * @return void.
110    */

111   // GUI thread
112
public void removeAction(final Action action) {
113     actions.remove(action);
114   }
115
116   /**
117    * Called after each cycle.
118    * @param none.
119    * @return void.
120    */

121   // Queue thread
122
protected void informCycle() {}
123
124   /**
125    * Returns the time to wait after each cycle.
126    * @param none.
127    * @return int time to wait.
128    */

129   // Queue thread
130
abstract protected int getCycleDelay();
131
132   /**
133    * Returns the time to wait between empty cycles.
134    * @param none.
135    * @return time to wait.
136    */

137   // Queue thread
138
abstract protected int getEmptyCycleDelay();
139
140   /**
141    * Returns the time to wait between two actions.
142    * @param none.
143    * @return time to wait.
144    */

145   // Queue thread
146
abstract protected int getActionDelay();
147
148   /**
149    * Loops among the actions currently in this queue.
150    * @param none.
151    * @return void.
152    */

153   // Queue thread
154
public void run() {
155     int nactions = 0;
156     int ncurrent = 0;
157
158     if (actions_copy == null || actions_copy.isEmpty())
159       synchronized(actions) {
160         actions_copy = new Vector<Action>(actions);
161         nactions = actions_copy.size();
162         ncurrent = 0;
163         setProgress(0);
164       }
165
166     while (!config.isEnd()) {
167       if (!actions_copy.isEmpty()) {
168         Action action = actions_copy.get(0);
169         actions_copy.remove(0);
170
171         if (nactions != 0) setProgress(Math.min(100 * ++ncurrent / nactions, 100));
172
173         try {
174           synchronized (this) {
175             max_time = new Date().getTime() + action.getMaxDelay();
176             current_action = action;
177           }
178
179           // this thread may be interrupted by the Background thread only from here ...
180
action.invoke();
181
182         } catch (final IOException ex) {
183           log.warn("Exception", ex);
184         } catch (final InterruptedException JavaDoc ex) {
185         } finally {
186           synchronized(this) {
187             max_time = 0;
188             current_action = null;
189             // ... to there
190
}
191         }
192       }
193
194       if (actions_copy.isEmpty()) {
195         informCycle();
196         synchronized(actions) {
197           actions_copy = new Vector<Action>(actions);
198           nactions = actions_copy.size();
199           ncurrent = 0;
200           setProgress(0);
201         }
202         try {
203           Thread.sleep(actions_copy.size() != 0 ? getCycleDelay() : getEmptyCycleDelay());
204         } catch (final InterruptedException JavaDoc ex) {
205           // each time a timeout occurs, the background thread interrupts this thread
206
}
207       }
208
209       try {
210         Thread.sleep(getActionDelay());
211       } catch (final InterruptedException JavaDoc ex) {
212         // each time a timeout occurs, the background thread interrupts this thread
213
}
214     }
215   }
216
217   /**
218    * Checks that the timeout occured.
219    * @param none.
220    * @return boolean true if timeout occured.
221    */

222   // Background thread
223
public synchronized boolean isExhausted() {
224     return max_time == 0 ? false : new Date().getTime() > max_time;
225   }
226
227   /**
228    * Stops this queue thread.
229    * @param none.
230    * @return void.
231    * @throws InterruptedException exception.
232    */

233   // main thread
234
public void end() throws InterruptedException JavaDoc {
235     try {
236       interrupt(Action.InterruptCause.exiting);
237     }
238     catch (final IOException ex) {
239       log.warn("Exception", ex);
240     }
241
242     thread.join();
243   }
244
245   /**
246    * Stops this queue thread.
247    * @param cause cause.
248    * @return void.
249    * @throws InterruptedException exception.
250    */

251   // main & Background thread
252
public void interrupt(final Action.InterruptCause cause) throws IOException {
253     if (thread != null) {
254       thread.interrupt();
255       synchronized (this) {
256         if (current_action != null) current_action.interrupt(cause);
257       }
258     }
259   }
260
261   /**
262    * Detaches this queue from the selected parent.
263    * @param visual_parent selected parent.
264    * @return void.
265    */

266   public void removeVisualElements(final VisualElement visual_parent) {
267     return;
268   }
269 }
270
Popular Tags