Java. Lang. Ref. referencequeue

Source: Internet
Author: User

Package java. Lang. Ref;

/**
* Reference queue
*
* Comment by liqiang
*
* @ Version 1.20, 01/23/03
* @ Author Mark Reinhold
* @ SINCE 1.2
*/

Public class referencequeue {

/**
* Constructor
*/
Public referencequeue (){}

// A static internal class used to generate a non-queue or queued flag. This class has no queuing capability.
Private Static class null extends referencequeue {
Boolean enqueue (reference R ){
Return false;
}
}

// Indicates that the reference object has not registered a queue
Static referencequeue null = new null ();
// Indicates that the feference object is already in the queue
Static referencequeue enqueued = new null ();

// A static internal class used to indicate the lock
Static private class lock {};
Private lock = new lock ();

// List Header
Private reference head = NULL;
Private long queuelength = 0;

// Queue operations
Boolean enqueue (reference R) {// called by the enqueue () method of the reference class
Synchronized (r ){
// If you have already joined the queue, false is returned.
If (R. Queue = enqueued) return false;
Synchronized (LOCK ){
R. Queue = enqueued;
// If the original Header element is not empty, the next object of this object is the original Header element; otherwise, it points to itself
R. Next = (Head = NULL )? R: head;
// New Header element
Head = R;
// The queue length increases
Queuelength ++;
Lock. policyall ();
Return true;
}
}
}

// Team-out Operation Method
Private reference reallypoll () {// this method is called under the lock
If (Head! = NULL ){
Reference r = head;
// If the next element of the current Header element is not empty, the current Header element is the next element of the original Header element.
// Otherwise, the current Header element is null.
Head = (R. Next = r )? Null: R. Next;
// Empty the queue flag of the queue Element
R. Queue = NULL;
// Point the next element of the team-out element to itself
R. Next = R;
// Queue length-1
Queuelength --;
Return R;
}
Return NULL;
}

/**
*
* The element leaves the queue. If the element is valid, this element is returned. Otherwise, null is returned.
*
* @ Return if the reference object is available, return NULL if not available.
*
*/
Public reference poll (){
Synchronized (LOCK ){
Return reallypoll ();
}
}

/**
* The element leaves the queue. If the element is valid, this element is returned. Otherwise, null is returned.
*
*
* Element team-out
*
* @ Param timeout wait time
*
* @ Return a if a reference object is available, return NULL if not available
*
*/
Public reference remove (long timeout)
Throws illegalargumentexception, interruptedexception
{
// If the wait time is negative, an exception is thrown.
If (timeout <0 ){
Throw new illegalargumentexception ("negative timeout value ");
}
 
Synchronized (LOCK ){
// Team out
Reference r = reallypoll ();
// If it is not null, the system returns the result directly.
If (R! = NULL) return R;

// Round Inspection
For (;;){
Lock. Wait (timeout );
R = reallypoll ();
If (R! = NULL) return R;
If (timeout! = 0) return NULL;
}
}
}

/**
* An element object is returned. If no element object exists, the system waits infinitely.
*
*/
Public reference remove () throws interruptedexception {
Return remove (0 );
}

}

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.