de.bea.domingo.queue
Class MTQueue

java.lang.Object
  extended by de.bea.domingo.queue.MTQueue
All Implemented Interfaces:
Queue

public final class MTQueue
extends java.lang.Object
implements Queue

A multithreaded blocking queue which is very useful for implementing producer-consumer style threading patterns.

Multiple blocking threads can wait for items being added to the queue while other threads add to the queue.

Non blocking and timeout based modes of access are possible as well.

Author:
Kurt Riede

Constructor Summary
MTQueue(java.lang.Object mutex, DNotesMonitor theMonitor)
          Constructor.
 
Method Summary
 java.lang.Object dequeue()
          Removes the first object from the queue, blocking until one is available.
 java.lang.Object dequeue(long timeout)
          Removes the first object from the queue, blocking only up to the given timeout time.
 java.lang.Object dequeueNoWait()
          Removes the first object from the queue without blocking.
 void enqueue(java.lang.Object object)
          Adds a new object to the end of the queue.
 boolean isEmpty()
          Checks is the queue is empty or not.
 int size()
          Returns the number of objects in the queue.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

MTQueue

public MTQueue(java.lang.Object mutex,
               DNotesMonitor theMonitor)
Constructor.

Parameters:
mutex - a mutex for syncronization
theMonitor - the monitor
Method Detail

size

public int size()
Returns the number of objects in the queue.

Specified by:
size in interface Queue
Returns:
number of objects in the queue
See Also:
Queue.size()

isEmpty

public boolean isEmpty()
Checks is the queue is empty or not.

Specified by:
isEmpty in interface Queue
Returns:
true if the queue is empty, else false
See Also:
Queue.isEmpty()

enqueue

public void enqueue(java.lang.Object object)
Adds a new object to the end of the queue. At least one thread will be notified.

Specified by:
enqueue in interface Queue
Parameters:
object - the object to add to the queue

dequeue

public java.lang.Object dequeue()
Removes the first object from the queue, blocking until one is available. Note that this method will never return null and could block forever.

Specified by:
dequeue in interface Queue
Returns:
next object from the queue

dequeue

public java.lang.Object dequeue(long timeout)
Removes the first object from the queue, blocking only up to the given timeout time. A thread can also wake up without being notified, interrupted, or timing out, a so-called spurious wakeup. This will rarely occur in practice, but it can occur and in such a case, this method will not wait upto the timeout, but come back earlier without a dequeued object.

Parameters:
timeout - maximum time to wait for an object from the queue
Returns:
next object from the queue

dequeueNoWait

public java.lang.Object dequeueNoWait()
Removes the first object from the queue without blocking. This method will return immediately with an item from the queue or null.

Returns:
the first object removed from the queue or null if the queue is empty


Domingo Java-API