1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.struts2.jasper.util;
19
20 import java.util.Vector;
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 vector = new Vector();
31
32 /***
33 * Put the object into the queue.
34 *
35 * @param object the object to be appended to the
36 * queue.
37 */
38 public synchronized void put(Object object) {
39 vector.addElement(object);
40 notify();
41 }
42
43 /***
44 * Pull the first object out of the queue. Wait if the queue is
45 * empty.
46 */
47 public synchronized Object pull() {
48 while (isEmpty())
49 try {
50 wait();
51 } catch (InterruptedException ex) {
52 }
53 return get();
54 }
55
56 /***
57 * Get the first object out of the queue. Return null if the queue
58 * is empty.
59 */
60 public synchronized Object get() {
61 Object object = peek();
62 if (object != null)
63 vector.removeElementAt(0);
64 return object;
65 }
66
67 /***
68 * Peek to see if something is available.
69 */
70 public Object peek() {
71 if (isEmpty())
72 return null;
73 return vector.elementAt(0);
74 }
75
76 /***
77 * Is the queue empty?
78 */
79 public boolean isEmpty() {
80 return vector.isEmpty();
81 }
82
83 /***
84 * How many elements are there in this queue?
85 */
86 public int size() {
87 return vector.size();
88 }
89 }