Package com.qq.util;
Import java.util.concurrent.*;
/**
* A <code> messagedeque<t> </code> defines a blocking message queue
*
* @version 1.0
* @author Mark
*/
public class Messagequeue<t> {
/*
* Blocking queues
*/
Private blockingqueue<t> m_queue = null;
/**
* constructor, you must specify the maximum capacity of the message queue
*
* @param maxitem
* Maximum capacity of message queues
*/
MessageQueue (int maxitem) {
M_queue = new linkedblockingqueue<t> (MaxItem);
}
/**
* Get the number of current queue items
*
* @return number of current queue items
*/
public int GetCount () {
return M_queue.size ();
}
/**
* Empty Queue
*
* @return
*
*/
public void Clear () {
M_queue.clear ();
}
/**
* Get Current Queue Header Item
*
* @return Current Queue Header Item
*/
Public T get () {
return M_queue.poll ();
}
/**
* Get Current Queue Header Item
*
* @param timeoutmillisecond
* Timeout period
* @return Current Queue Header Item
* @see #get ()
*/
Public T get (long Timeoutmillisecond) throws Interruptedexception {
Return M_queue.poll (Timeoutmillisecond, timeunit.milliseconds);
}
/**
* Put the tail element of the queue
*
* @return Success is true, failure is false, and failure is usually a queue full
* @see #put (Object, long)
*/
Public Boolean put (T item) throws Interruptedexception {
return M_queue.offer (item);
}
/**
* Put the tail element of the queue
*
* @param Item
* Timeout period
* @return Success is true, failure is false, and failure is usually a queue full
* @see #put (Object)
*/
Public Boolean put (T item, long Timeoutmillisecond)
Throws Interruptedexception {
Return M_queue.offer (item, Timeoutmillisecond, Timeunit.milliseconds);
}
/**
* @param args
* @throws interruptedexception
*/
public static void Main (string[] args) throws Interruptedexception {
TODO auto-generated Method Stub
messagequeue<string> msg = new messagequeue<string> (4);
Msg.put ("aaaaaa");
Msg.put ("bbbbbb");
Msg.put ("CCCCCCC");
Msg.put ("ddddddd");
Msg.put ("eeeeeee");
while (Msg.getcount () > 0) {
System.out.println (Msg.get ());
}
}
}