Merge pull request #1844 from jrw972/monterey
[ACE_TAO.git] / TAO / examples / Simulator / DOVEBrowser / MTQueue.java
blobe110a4c38cb929edd025702841cd7d49ce62c701
1 // This is a queue class for use by multiple threads, with at least one
2 // thread enqueueing objects and another dequeueing them. The dequeue_*
3 // methods block the thread if there are no more objects in the queue,
4 // while the try_dequeue_* methods do not block but rather return a
5 // null reference if there is nothing in the queue.
6 public class MTQueue
8 // Initially, the head and tail of the queue are null
9 MTQueue_Node head_ = null;
10 MTQueue_Node tail_ = null;
12 // Constructor - does nothing.
13 public MTQueue ()
17 // Places a passed Object at the end of the queue.
18 public synchronized void enqueue_tail (Object new_data)
20 // Create a new node to hold the object.
21 MTQueue_Node new_node = new MTQueue_Node(new_data);
23 // Insert the node into the queue.
24 if (tail_ == null)
26 tail_ = new_node;
27 head_ = new_node;
29 else
31 new_node.prev_ = tail_;
32 tail_.next_ = new_node;
33 tail_ = new_node;
36 // Wake up any waiting threads
37 notifyAll ();
40 // Places a passed Object at the front of the queue.
41 public synchronized void enqueue_head(Object new_data)
43 // Create a new node to hold the object.
44 MTQueue_Node new_node = new MTQueue_Node(new_data);
46 // Insert the node into the queue.
47 if (head_ == null)
49 tail_ = new_node;
50 head_ = new_node;
52 else
54 new_node.next_ = head_;
55 head_.prev_ = new_node;
56 head_ = new_node;
59 // Wake up any waiting threads
60 notifyAll ();
63 // Try to remove an object from the head of the queue - nonblocking.
64 public synchronized Object try_dequeue_head()
66 // Start with a null reference.
67 Object return_value = null;
69 // If there's anything there, dequeue it.
70 if (head_ != null)
72 return_value = dequeue_head ();
75 // Return what we found, if anything.
76 return return_value;
79 // Remove an object from the head of the queue - blocking.
80 public synchronized Object dequeue_head()
82 // Start with a null reference.
83 Object return_value = null;
85 // Wait until there's something to dequeue.
86 while (head_ == null)
88 try
90 wait ();
92 catch (InterruptedException e)
94 return return_value;
98 // Dequeue the object at the head of the queue. Make sure
99 // to null out references within dequeued nodes to prevent
100 // out of memory errors.
101 if (tail_ == head_)
103 return_value = head_.data_;
104 head_.next_ = null;
105 head_.prev_ = null;
106 head_.data_ = null;
107 tail_ = null;
108 head_ = null;
110 else
112 return_value = head_.data_;
113 head_ = head_.next_;
114 head_.prev_.next_ = null;
115 head_.prev_.prev_ = null;
116 head_.prev_.data_ = null;
117 head_.prev_ = null;
120 // Return the object we dequeued.
121 return return_value;
124 // Try to remove an object from the tail of the queue - nonblocking.
125 public synchronized Object try_dequeue_tail ()
127 // Start with a null reference.
128 Object return_value = null;
130 // If there's anything there, dequeue it.
131 if (tail_ != null)
133 return_value = dequeue_tail ();
136 // Return what we found, if anything.
137 return return_value;
140 // Remove an object from the tail of the queue - blocking.
141 public synchronized Object dequeue_tail ()
143 // Start with a null reference.
144 Object return_value = null;
146 // Wait until there's something to dequeue.
147 while (tail_ == null)
151 wait ();
153 catch (InterruptedException e)
155 return return_value;
159 // Dequeue the object at the back of the queue. Make sure
160 // to null out references within dequeued nodes to prevent
161 // out of memory errors.
162 if (tail_ == head_)
164 return_value = tail_.data_;
165 tail_.data_ = null;
166 tail_.next_ = null;
167 tail_.prev_ = null;
168 tail_ = null;
169 head_ = null;
171 else
173 return_value = tail_.data_;
174 tail_ = tail_.prev_;
175 tail_.next_.data_ = null;
176 tail_.next_.next_ = null;
177 tail_.next_.prev_ = null;
178 tail_.next_ = null;
181 // Return the object we dequeued.
182 return return_value;
186 class MTQueue_Node
188 public MTQueue_Node prev_ = null;
189 public MTQueue_Node next_ = null;
190 public Object data_;
192 public MTQueue_Node(Object data)
194 data_ = data;