KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > scalagent > kjoram > util > Queue


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

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

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

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

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

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

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

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

122   public synchronized void stop() throws InterruptedException JavaDoc
123   {
124     stopping = true;
125
126     if (size() != 0)
127       wait();
128   }
129 }
130
Popular Tags