1 package org.sapia.ubik.rmi.server.command; 2 3 import org.sapia.ubik.net.Timer; 4 import org.sapia.ubik.rmi.server.ShutdownException; 5 6 import java.util.ArrayList ; 7 import java.util.LinkedList ; 8 import java.util.List ; 9 10 11 21 public class ExecQueue { 22 private LinkedList _queue = new LinkedList (); 23 private boolean _shutdown; 24 25 28 public ExecQueue() { 29 super(); 30 } 31 32 37 public synchronized void add(Executable toExecute) { 38 if (_shutdown) { 39 throw new ShutdownException(); 40 } 41 42 _queue.add(toExecute); 43 notifyAll(); 44 } 45 46 53 public synchronized List removeAll() 54 throws InterruptedException , ShutdownException { 55 while (_queue.size() == 0) { 56 if (_shutdown) { 57 notify(); 58 throw new ShutdownException(); 59 } 60 61 wait(); 62 } 63 64 List toReturn = new ArrayList (_queue); 65 _queue.clear(); 66 67 return toReturn; 68 } 69 70 76 public synchronized void shutdown(long timeout) throws InterruptedException { 77 _shutdown = true; 78 notifyAll(); 79 80 Timer timer = new Timer(timeout); 81 82 while (_queue.size() > 0) { 83 wait(timeout); 84 85 if (timer.isOver()) { 86 break; 87 } 88 } 89 } 90 91 96 public int size() { 97 return _queue.size(); 98 } 99 100 105 public synchronized Executable remove() 106 throws InterruptedException , ShutdownException { 107 while (_queue.size() == 0) { 108 if (_shutdown) { 109 notify(); 110 throw new ShutdownException(); 111 } 112 113 wait(); 114 } 115 116 return (Executable) _queue.remove(0); 117 } 118 } 119 | Popular Tags |