01: /*
02: * Copyright 1999,2004 The Apache Software Foundation.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16: package org.apache.catalina.util;
17:
18: import java.util.Vector;
19:
20: /**
21: * A simple FIFO queue class which causes the calling thread to wait
22: * if the queue is empty and notifies threads that are waiting when it
23: * is not empty.
24: *
25: * @author Anil V (akv@eng.sun.com)
26: */
27: public class Queue {
28: private Vector vector = new Vector();
29:
30: /**
31: * Put the object into the queue.
32: *
33: * @param object the object to be appended to the
34: * queue.
35: */
36: public synchronized void put(Object object) {
37: vector.addElement(object);
38: notify();
39: }
40:
41: /**
42: * Pull the first object out of the queue. Wait if the queue is
43: * empty.
44: */
45: public synchronized Object pull() {
46: while (isEmpty())
47: try {
48: wait();
49: } catch (InterruptedException ex) {
50: }
51: return get();
52: }
53:
54: /**
55: * Get the first object out of the queue. Return null if the queue
56: * is empty.
57: */
58: public synchronized Object get() {
59: Object object = peek();
60: if (object != null)
61: vector.removeElementAt(0);
62: return object;
63: }
64:
65: /**
66: * Peek to see if something is available.
67: */
68: public Object peek() {
69: if (isEmpty())
70: return null;
71: return vector.elementAt(0);
72: }
73:
74: /**
75: * Is the queue empty?
76: */
77: public boolean isEmpty() {
78: return vector.isEmpty();
79: }
80:
81: /**
82: * How many elements are there in this queue?
83: */
84: public int size() {
85: return vector.size();
86: }
87: }
|