1 2 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 42 43 public abstract class Queue extends VisualElement implements Runnable { 44 private static Log log = LogFactory.getLog(Queue.class); 45 46 private final String name; 47 private final Config config; 48 private final Thread thread; 49 private long max_time = 0; 50 51 private final List<Action> actions = 53 Collections.synchronizedList(new LinkedList<Action>()); 54 55 private Vector<Action> actions_copy = null; 57 58 private Action current_action = null; 59 60 65 protected Queue(final String 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 (this, name); 73 thread.start(); 74 } 75 76 81 protected final void initialize(final GUI gui) { 83 super.initialize(gui); 84 if (gui != null) setImageQueue(); 85 } 86 87 92 public int size() { 93 return actions.size(); 94 } 95 96 101 public void addAction(final Action action) { 103 actions.add(action); 104 } 105 106 111 public void removeAction(final Action action) { 113 actions.remove(action); 114 } 115 116 121 protected void informCycle() {} 123 124 129 abstract protected int getCycleDelay(); 131 132 137 abstract protected int getEmptyCycleDelay(); 139 140 145 abstract protected int getActionDelay(); 147 148 153 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 action.invoke(); 181 182 } catch (final IOException ex) { 183 log.warn("Exception", ex); 184 } catch (final InterruptedException ex) { 185 } finally { 186 synchronized(this) { 187 max_time = 0; 188 current_action = null; 189 } 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 ex) { 205 } 207 } 208 209 try { 210 Thread.sleep(getActionDelay()); 211 } catch (final InterruptedException ex) { 212 } 214 } 215 } 216 217 222 public synchronized boolean isExhausted() { 224 return max_time == 0 ? false : new Date().getTime() > max_time; 225 } 226 227 233 public void end() throws InterruptedException { 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 251 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 266 public void removeVisualElements(final VisualElement visual_parent) { 267 return; 268 } 269 } 270 | Popular Tags |