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.
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.
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.
31 new_node
.prev_
= tail_
;
32 tail_
.next_
= new_node
;
36 // Wake up any waiting threads
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.
54 new_node
.next_
= head_
;
55 head_
.prev_
= new_node
;
59 // Wake up any waiting threads
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.
72 return_value
= dequeue_head ();
75 // Return what we found, if anything.
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.
92 catch (InterruptedException e
)
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.
103 return_value
= head_
.data_
;
112 return_value
= head_
.data_
;
114 head_
.prev_
.next_
= null;
115 head_
.prev_
.prev_
= null;
116 head_
.prev_
.data_
= null;
120 // Return the object we dequeued.
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.
133 return_value
= dequeue_tail ();
136 // Return what we found, if anything.
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)
153 catch (InterruptedException e
)
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.
164 return_value
= tail_
.data_
;
173 return_value
= tail_
.data_
;
175 tail_
.next_
.data_
= null;
176 tail_
.next_
.next_
= null;
177 tail_
.next_
.prev_
= null;
181 // Return the object we dequeued.
188 public MTQueue_Node prev_
= null;
189 public MTQueue_Node next_
= null;
192 public MTQueue_Node(Object data
)