Design The Queue of Java

xiaoxiao2021-03-13  219

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 stay 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 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 WHETER THE FIRST OBJECT OF Queue

* is expected.

* /

Public final static int correker = 3;

/ **

* Consumer Can Get and Remove Object from queue. It is common to operation

* 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 CREANG Queue. I Think That Priority Of Threads

* /

Public final static int ignore_priority = 0x01;

/ **

* It is buy by sending and receiving functions to indicate WHether Threads Will Wait Some

* MilliseConds to finish operation. 'No_wait' Means That Threads Will Immediely Return

* Error Codes When Queue Has Been Occupied by Others

* /

Public final static long no_wait = waiter.no_wait;

/ **

* It is buy 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 tahers.

* /

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 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 Insert Desrted 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 intended 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 ^ _ ^.

转载请注明原文地址:https://www.9cbs.com/read-128973.html

New Post(0)