KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > fr > dyade > aaa > util > Queue


1 /*
2  * Copyright (C) 2001 - 2004 ScalAgent Distributed Technologies
3  * Copyright (C) 1996 - 2000 BULL
4  * Copyright (C) 1996 - 2000 INRIA
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA.
20  */

21 package fr.dyade.aaa.util;
22
23 import java.io.*;
24 import java.util.*;
25
26 /**
27  * The <code>Queue</code> class implements a First-In-First-Out
28  * (FIFO) list of objects.
29  * <p>
30  * A queue is for the exclusive use of one single consumer, whereas many
31  * producers may access it. It is ready for use after instanciation. A
32  * producer may wait for the queue to be empty by calling the
33  * <code>stop()</code> method. This method returns when the queue is
34  * actually empty, and prohibitis any further call to the <code>push</code>
35  * method. To be able to use the queue again, it must be re-started through
36  * the <code>start()</code> method.
37  */

38 public class Queue extends Vector {
39   /**
40    * <code>true</code> if a producer called the <code>stop()</code>
41    * method.
42    */

43   private boolean stopping;
44
45   /**
46    * Constructs a <code>Queue</code> instance.
47    */

48   public Queue() {
49     super();
50     start();
51   }
52
53
54   /**
55    * Pushes an item at the end of this queue.
56    *
57    * @param item The item to be pushed at the end of this queue.
58    * @exception StoppedQueueException If the queue is stopping or stopped.
59    */

60   public synchronized void push(Object JavaDoc item) {
61     if (stopping)
62       throw new StoppedQueueException();
63
64     addElement(item);
65     notify();
66   }
67
68
69   /**
70    * Removes and returns the object at the top of this queue.
71    *
72    * @return The object at the top of this queue.
73    * @exception EmptyQueueException If the queue is empty.
74    */

75   public synchronized Object JavaDoc pop() {
76     Object JavaDoc obj;
77     
78     if (size() == 0)
79       throw new EmptyQueueException();
80     
81     obj =elementAt(0);
82     removeElementAt(0);
83
84     if (stopping && size() == 0)
85       notify();
86
87     return obj;
88   }
89
90
91   /**
92    * Waits for an object to be pushed in the queue, and eventually returns
93    * it without removing it.
94    *
95    * @return The object at the top of this queue.
96    */

97   public synchronized Object JavaDoc get() throws InterruptedException JavaDoc {
98     while (size() == 0)
99       wait();
100
101     return elementAt(0);
102   }
103
104
105   /** Authorizes the use of the queue by producers. */
106   public void start() {
107     stopping = false;
108   }
109
110
111   /**
112    * Stops the queue by returning when it is empty and prohibiting any
113    * further producers call to the <code>push</code> method.
114    */

115   public synchronized void stop() throws InterruptedException JavaDoc {
116     stopping = true;
117     if (size() != 0) wait();
118   }
119 }
120
Popular Tags