Blocking Message Queuing

Source: Internet
Author: User
Tags message queue

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 ());
}
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.