e.g. Calendar Search Help
You must enter a value before pressing Search
tomcat

Class: org.apache.tomcat.util.collections.Queue   ©

 OK to copy?
001 /*
002  *  Copyright 1999-2004 The Apache Software Foundation
003  *
004  *  Licensed under the Apache License, Version 2.0 (the "License");
005  *  you may not use this file except in compliance with the License.
006  *  You may obtain a copy of the License at
007  *
008  *      http://www.apache.org/licenses/LICENSE-2.0
009  *
010  *  Unless required by applicable law or agreed to in writing, software
011  *  distributed under the License is distributed on an "AS IS" BASIS,
012  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013  *  See the License for the specific language governing permissions and
014  *  limitations under the License.
015  */
016 
017 package org.apache.tomcat.util.collections;
018 
019 import java.util.Vector;
020 
021 /**
022  * A simple FIFO queue class which causes the calling thread to wait
023  * if the queue is empty and notifies threads that are waiting when it
024  * is not empty.
025  *
026  * @author Anil V (akv@eng.sun.com)
027  */
028 public class Queue {
029     private Vector vector = new Vector();
030     private boolean stopWaiting=false;
031     private boolean waiting=false;
032     
033     /** 
034      * Put the object into the queue.
035      * 
036      * @param    object        the object to be appended to the
037      *                 queue. 
038      */
039     public synchronized void put(Object object) {
040     vector.addElement(object);
041     notify();
042     }
043 
044     /** Break the pull(), allowing the calling thread to exit
045      */
046     public synchronized void stop() {
047     stopWaiting=true;
048     // just a hack to stop waiting 
049     if( waiting ) notify();
050     }
051     
052     /**
053      * Pull the first object out of the queue. Wait if the queue is
054      * empty.
055      */
056     public synchronized Object pull() {
057     while (isEmpty()) {
058         try {
059         waiting=true;
060         wait();
061         } catch (InterruptedException ex) {
062         }
063         waiting=false;
064         if( stopWaiting ) return null;
065     }
066     return get();
067     }
068 
069     /**
070      * Get the first object out of the queue. Return null if the queue
071      * is empty. 
072      */
073     public synchronized Object get() {
074     Object object = peek();
075     if (object != null)
076         vector.removeElementAt(0);
077     return object;
078     }
079 
080     /**
081      * Peek to see if something is available.
082      */
083     public Object peek() {
084     if (isEmpty())
085         return null;
086     return vector.elementAt(0);
087     }
088     
089     /**
090      * Is the queue empty?
091      */
092     public boolean isEmpty() {
093     return vector.isEmpty();
094     }
095 
096     /**
097      * How many elements are there in this queue?
098      */
099     public int size() {
Rate100     return vector.size();
101     }
102 }

            
All Examples in File:
Example
Line
Rating (found
useful by...)
100 0% of 0