Java has implemented many queues or lists to manage object. Until now, however, no any implementations provide blocking or timeout mechanism. 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 reply 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 should 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 received 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 receiving and sending operations.
*/
public final static int NORMAL = 2;
/**
* When receiving 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 receiving mode.
*/
public final static int GET = 4;
/**
* Queue should consider the priority of threads, but sometimes receivers or senders
* hope that queue can ignore priority of threads. At that time, the property should be
* set when creating queue. I think that priority of threads
*/
public final static int IGNORE_PRIORITY = 0x01;
/**
* It is used by sending and receiving 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 receiving 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 should 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 the
* another mode whose name is 'NORMAL' mode.
*
* @param o --- the object inserted which should 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 receiving 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 receiving 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 into
* 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^_^.
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 should 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 received 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 receiving and sending operations.
*/
public final static int NORMAL = 2;
/**
* When receiving 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 receiving mode.
*/
public final static int GET = 4;
/**
* Queue should consider the priority of threads, but sometimes receivers or senders
* hope that queue can ignore priority of threads. At that time, the property should be
* set when creating queue. I think that priority of threads
*/
public final static int IGNORE_PRIORITY = 0x01;
/**
* It is used by sending and receiving 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 receiving 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 should 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 the
* another mode whose name is 'NORMAL' mode.
*
* @param o --- the object inserted which should 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 receiving 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 receiving 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 into
* 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^_^.