simple.util
Class MessageQueue

java.lang.Object
  extended by simple.util.MessageQueue

public class MessageQueue
extends java.lang.Object

This provides a means to pass messages between threads. This can be given an arbitrary number of messages from a notifier thread without blocking so that message passing is not a time consuming task. The recieving thread can then acquire the messages using the dequeue method which will block if there are no messages in the queue.

Author:
Niall Gallagher
See Also:
LinkedList

Constructor Summary
MessageQueue()
          This creates a new MessageQueue object for passing messages between threads.
 
Method Summary
 java.lang.Object dequeue()
          This is guaranteed to return an object.
 void enqueue(java.lang.Object value)
          If this enqueues to an empty queue it notifys any dequeuers that were trying to take from the empty queue.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

MessageQueue

public MessageQueue()
This creates a new MessageQueue object for passing messages between threads. This synchronizes itself using a private object so that if this instance is used as a monitor it will not interfere with the operation of the message notifications between the communicating threads.

Method Detail

enqueue

public void enqueue(java.lang.Object value)
If this enqueues to an empty queue it notifys any dequeuers that were trying to take from the empty queue. This ensures that a dequeuer can wake-up an will not block indefinitely.

Parameters:
value - the object to be scheduled in the queue

dequeue

public java.lang.Object dequeue()
                         throws java.lang.InterruptedException
This is guaranteed to return an object. If the queue is empty then the thread that is dequeuing blocks until there is an object to dequeue, it will be notified by the enqueuer.

Returns:
the object with the highest priority in the queue
Throws:
java.lang.InterruptedException - thrown if theres an interrupt