Study of queue linkedblockingqueue based on linked list

Source: Internet
Author: User

Linkedblockingqueue for FIFO queue

1. Nodes in the list, next is the successor node
Static Class Node<e> {
E item;
Node<e> Next;
Node (E x) {item = x;}
}

2. Three ways to construct a. Capacity is the maximum B. Capacity for the specified size C. Capacity for maximum, using collection C to initialize the queue
Public Linkedblockingqueue () {this (integer.max_value);}
public linkedblockingqueue (int capacity) {...}
Public Linkedblockingqueue (collection<? extends e> c) {...}

3. Common methods Add (), offer (), put (), poll (), Peek (), Clear ()
Add (): Call the method in the parent class Abstractqueue, and the parent class calls the Linkedblockingqueue.offer (e) method
Public boolean Add (E e) {
if (Offer (e))
return true;
Else
throw new IllegalStateException ("Queue full");
}
Offer (): Waits for a specified time to insert an element that returns false if the time element has not been added.
Inserts the specified element at the tail of this queue, waiting if necessary up to the specified wait time for
Space to become available.
Public Boolean offer (E e) {...}
Public Boolean offer (e E, long timeout, timeunit unit) {...}
Put (): Inserts the specified element at the tail of this queue, waiting if necessary for space to become available.
public void put (E e) {...}
Poll (): Removes a node from head of the queue.
Public E poll () {...}
Public E Poll (long timeout, timeunit unit) {...}
If the number of queues equals 0, wait for a specified time to return null
Peek (): View the header of the queue and not remove the element
Public E Peek () {return first.item;}
Clear (): atomically removes all of the elements from this queue.
public void Clear () {...}

Study of queue linkedblockingqueue based on linked list

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.