Design the queue of Java

Source: Internet
Author: User
Java has implemented into queues or lists to manage object. until now, however, no any implementations provide blocking or timeout mechanisms. these policies are very useful when multi-threads communicate each other. I have designed and implemented them in single CPU mode. I hope that the new version of jsdk can provide relative API to use :-). of course, if Java funs are interested in them, please r Eply and discuss it. Of course, chances are good for us to understand Java deeply on the process of discuss!
Thanks!

/**
* The queue is synchronizing FIFO interface queue, and supports 'urgent'
*, 'Normal', 'peek 'and 'get' operations. At the same time, the priority of threads
* Is considered to make threads of high priority fastly handled by queue.
* Iqueue shoshould inherit the interface of Java. Io. serializable, because some cases
* Need queue can be serialized into storage media.
*
* Author: Wang Yanqing
* Email: hello.wyq@gmail.com
* Version: 0.001
* Creating Date: 03/14/2006
* Modifying Date: 03/14/2006
* Copyright: free usage and no modification
* History:
* [03/14/2006]
* Define iqueue Interface
*/

Package OSA. Queue;

Import OSA. Common. result;
Import OSA. Common. Waiter;

Public interface iqueue extends java. Io. serializable
{
/**
* When using urgent mode to send object into queue, the object will be inserted
* Into first position of queue. It means that it will be pinned ed firstly in the next time.
*/
Public final static int urgent = 1;

/**
* In normal mode, the object will be append into queue, and keeps to FIFO
* Rule When executing processing and sending operations.
*/
Public final static int normal = 2;

/**
* When processing ing object from queue, consumer can get object without removing
* It From queue. The mode is mainly used to check whether the first object of queue
* Is expected.
*/
Public final static int peek = 3;

/**
* Consumer can get and remove object from queue. It is common to operate
* Queue in processing mode.
*/
Public final static int get = 4;

/**
* Queue shoshould consider the priority of threads, but sometimes receivers or senders
* Hope that queue can ignore priority of threads. At that time, the property shoshould be
* Set when creating queue. I think that priority of threads
*/
Public final static int ignore_priority = 0x01;

/**
* It is used by sending and loading ing functions to indicate whether threads will wait some
* Milliseconds to finish operation. 'No _ wait' means that threads will immediately return
* Error codes when queue has been occupied by others
*/
Public final static long no_wait = waiter. no_wait;

/**
* It is used by sending and loading ing functions to indicate whether threads will wait some
* Milliseconds to finish operation. 'Wait _ Forever 'means that threads will wait for queue
* Until it has been free by others.
*/
Public final static long wait_forever = waiter. wait_forever;

/**
* Send object into queue, and it is synchronized. Timeout indicates the valid time range in
* Which object shocould be added into queue. If time is out, this sending operation will declare
* Failure. There are two kinds of mode. One is the 'urgent' mode which will
* Insert object in front of queue. appending object at the bottom of queue is
* Another mode whose name is 'normal' mode.
*
* @ Param o --- the object inserted which shocould never be null
* @ Param mode --- 'urgent' or 'normal'
* @ Param timeout --- indicate the valid time range, the value must> = 0
* 'No _ wait' and 'wait _ Forever 'Can Be Used too.
* Implementation must consider the cost-time of synchronizing operation.
* @ Return result. einv --- invalid parameters, such as O equals null, Mode
* Is out of 'urgent' or 'normal', timeout less than zero.
* Result. etimeout --- fail to send object in the valid time range which
* Is defined by 'timeout' variable.
* Result. enomen --- fail to send object because system has no any
* Memory to use.
* Result. OK --- success
* @ Since 0.001
*/
Public int send (Object o, int mode, long timeout );

/**
* Receive object from queue, and it is synchronized.
*
* @ Param mode --- the processing mode, the valid modes are 'peek 'and 'get'
* @ Param timeout --- indicate the valid time range, the value must> = 0
* 'No _ wait' and 'wait _ Forever 'Can Be Used too.
* Implementation must consider the cost-time of synchronizing operation.
* @ Param Rs --- the result of processing ing operation will be stored into result object if
* Conusmer is interested in the detailed result.
* Result. einv --- invalid Parameters
* Result. etimeout --- time is out
* Result. OK --- success
* Of course RS can eqaul null, if consumer is not interested in it.
* @ Return real object --- success
* Null --- fail to receive object, the detailed error codes are stored
* Result objet
* @ Since 0.001
*/
Public object receive (INT mode, long timeout, result RS );
}

The structure of package is same as mutex, please see mutex interface to understand it, if readers are interested in it =_^.

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.