KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sapia > ubik > rmi > server > command > ExecQueue


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 JavaDoc;
7 import java.util.LinkedList JavaDoc;
8 import java.util.List JavaDoc;
9
10
11 /**
12  * Models a queue of <code>Executable</code> instances.
13  *
14  * @author Yanick Duchesne
15  * <dl>
16  * <dt><b>Copyright:</b><dd>Copyright &#169; 2002-2003 <a HREF="http://www.sapia-oss.org">Sapia Open Source Software</a>. All Rights Reserved.</dd></dt>
17  * <dt><b>License:</b><dd>Read the license.txt file of the jar or visit the
18  * <a HREF="http://www.sapia-oss.org/license.html">license page</a> at the Sapia OSS web site</dd></dt>
19  * </dl>
20  */

21 public class ExecQueue {
22   private LinkedList JavaDoc _queue = new LinkedList JavaDoc();
23   private boolean _shutdown;
24
25   /**
26    * Constructor for CommandQueue.
27    */

28   public ExecQueue() {
29     super();
30   }
31
32   /**
33    * Adds an <code>Executable</code> to this queue.
34    *
35    * @param toExecute an <code>Executable</code>.
36    */

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   /**
47    * Removes all the <code>Executable</code> from this queue and
48    * returns them; if the queue is empty, this method blocks until
49    * a new item is added.
50    *
51    * @return a <code>List</code> of <code>Executable</code>.
52    */

53   public synchronized List JavaDoc removeAll()
54     throws InterruptedException JavaDoc, ShutdownException {
55     while (_queue.size() == 0) {
56       if (_shutdown) {
57         notify();
58         throw new ShutdownException();
59       }
60
61       wait();
62     }
63
64     List JavaDoc toReturn = new ArrayList JavaDoc(_queue);
65     _queue.clear();
66
67     return toReturn;
68   }
69
70   /**
71    * Shuts down this instance.
72    *
73    * @param timeout a timeout in millis. If this queue still has pending
74    * objects after the timeout is reached, this method returns.
75    */

76   public synchronized void shutdown(long timeout) throws InterruptedException JavaDoc {
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   /**
92    * Returns this queue's size.
93    *
94    * @return this queue's size (the number of items in this queue).
95    */

96   public int size() {
97     return _queue.size();
98   }
99
100   /**
101    * Removes the first <code>Executable</code> from this queue and returns it.
102    *
103    * @return an <code>Executable</code>.
104    */

105   public synchronized Executable remove()
106     throws InterruptedException JavaDoc, 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