KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tomcat > util > collections > 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
18 package org.apache.tomcat.util.collections;
19
20 import java.util.Vector JavaDoc;
21
22 /**
23  * A simple FIFO queue class which causes the calling thread to wait
24  * if the queue is empty and notifies threads that are waiting when it
25  * is not empty.
26  *
27  * @author Anil V (akv@eng.sun.com)
28  */

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

40     public synchronized void put(Object JavaDoc object) {
41     vector.addElement(object);
42     notify();
43     }
44
45     /** Break the pull(), allowing the calling thread to exit
46      */

47     public synchronized void stop() {
48     stopWaiting=true;
49     // just a hack to stop waiting
50
if( waiting ) notify();
51     }
52     
53     /**
54      * Pull the first object out of the queue. Wait if the queue is
55      * empty.
56      */

57     public synchronized Object JavaDoc pull() {
58     while (isEmpty()) {
59         try {
60         waiting=true;
61         wait();
62         } catch (InterruptedException JavaDoc ex) {
63         }
64         waiting=false;
65         if( stopWaiting ) return null;
66     }
67     return get();
68     }
69
70     /**
71      * Get the first object out of the queue. Return null if the queue
72      * is empty.
73      */

74     public synchronized Object JavaDoc get() {
75     Object JavaDoc object = peek();
76     if (object != null)
77         vector.removeElementAt(0);
78     return object;
79     }
80
81     /**
82      * Peek to see if something is available.
83      */

84     public Object JavaDoc peek() {
85     if (isEmpty())
86         return null;
87     return vector.elementAt(0);
88     }
89     
90     /**
91      * Is the queue empty?
92      */

93     public boolean isEmpty() {
94     return vector.isEmpty();
95     }
96
97     /**
98      * How many elements are there in this queue?
99      */

100     public int size() {
101     return vector.size();
102     }
103 }
104
Popular Tags