KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > catalina > util > Queue


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.catalina.util;
18
19 import java.util.Vector JavaDoc;
20
21 /**
22  * A simple FIFO queue class which causes the calling thread to wait
23  * if the queue is empty and notifies threads that are waiting when it
24  * is not empty.
25  *
26  * @author Anil V (akv@eng.sun.com)
27  */

28 public class Queue {
29     private Vector JavaDoc vector = new Vector JavaDoc();
30
31     /**
32      * Put the object into the queue.
33      *
34      * @param object the object to be appended to the
35      * queue.
36      */

37     public synchronized void put(Object JavaDoc object) {
38         vector.addElement(object);
39         notify();
40     }
41
42     /**
43      * Pull the first object out of the queue. Wait if the queue is
44      * empty.
45      */

46     public synchronized Object JavaDoc pull() {
47         while (isEmpty())
48             try {
49                 wait();
50             } catch (InterruptedException JavaDoc ex) {
51             }
52         return get();
53     }
54
55     /**
56      * Get the first object out of the queue. Return null if the queue
57      * is empty.
58      */

59     public synchronized Object JavaDoc get() {
60         Object JavaDoc object = peek();
61         if (object != null)
62             vector.removeElementAt(0);
63         return object;
64     }
65
66     /**
67      * Peek to see if something is available.
68      */

69     public Object JavaDoc peek() {
70         if (isEmpty())
71             return null;
72         return vector.elementAt(0);
73     }
74
75     /**
76      * Is the queue empty?
77      */

78     public boolean isEmpty() {
79         return vector.isEmpty();
80     }
81
82     /**
83      * How many elements are there in this queue?
84      */

85     public int size() {
86         return vector.size();
87     }
88 }
89
Popular Tags