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