Java中的 LinkedBlockingQueue 类
LinkedBlockingQueue是一个基于链接节点的可选有界阻塞队列。这意味着 LinkedBlockingQueue 可以是有界的,如果给定其容量,则 LinkedBlockingQueue 将是无界的。容量可以作为参数提供给 LinkedBlockingQueue 的构造函数。此队列对元素进行 FIFO(先进先出)排序。这意味着该队列的头部是该队列中存在的元素中最旧的元素。该队列的尾部是该队列元素中的最新元素。新插入的元素总是插入到队列的尾部,队列检索操作获取队列头部的元素。链接队列通常比基于数组的队列具有更高的吞吐量,但在大多数并发应用程序中性能更不可预测。
容量(如果未指定)等于Integer.MAX_VALUE 。每次插入时都会动态创建链接节点,直到队列的容量未满。此类及其迭代器实现了 Collection 和 Iterator 接口的所有可选方法。它是Java集合框架的成员。
LinkedBlockingQueue 的层次结构
LinkedBlockingQueue
宣言:
public class LinkedBlockingQueue
E - 此集合中包含的元素类型。
LinkedBlockingQueue 的构造函数:
要构造一个 LinkedBlockingQueue,我们需要从Java.util.concurrent.LinkedBlockingQueue中导入它。这里,容量是链接阻塞队列的大小。
1. LinkedBlockingQueue() : 创建一个容量为 Integer.MAX_VALUE 的 LinkedBlockingQueue。
LinkedBlockingQueue
例子:
Java
// Java program to demonstrate
// LinkedBlockingQueue() constructor
import java.util.concurrent.LinkedBlockingQueue;
public class LinkedBlockingQueueDemo {
public static void main(String[] args)
{
// create object of LinkedBlockingQueue
// using LinkedBlockingQueue() constructor
LinkedBlockingQueue lbq
= new LinkedBlockingQueue();
// add numbers
lbq.add(1);
lbq.add(2);
lbq.add(3);
lbq.add(4);
lbq.add(5);
// print queue
System.out.println("LinkedBlockingQueue:" + lbq);
}
}
Java
// Java program to demonstrate
// LinkedBlockingQueue(int initialCapacity) constructor
import java.util.concurrent.LinkedBlockingQueue;
public class GFG {
public static void main(String[] args)
{
// define capacity of LinkedBlockingQueue
int capacity = 15;
// create object of LinkedBlockingQueue
// using LinkedBlockingQueue(int initialCapacity)
// constructor
LinkedBlockingQueue lbq
= new LinkedBlockingQueue(capacity);
// add numbers
lbq.add(1);
lbq.add(2);
lbq.add(3);
// print queue
System.out.println("LinkedBlockingQueue:" + lbq);
}
}
Java
// Java program to demonstrate
// LinkedBlockingQueue(Collection c) constructor
import java.util.concurrent.LinkedBlockingQueue;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
// Creating a Collection
Vector v = new Vector();
v.addElement(1);
v.addElement(2);
v.addElement(3);
v.addElement(4);
v.addElement(5);
// create object of LinkedBlockingQueue
// using LinkedBlockingQueue(Collection c)
// constructor
LinkedBlockingQueue lbq
= new LinkedBlockingQueue(v);
// print queue
System.out.println("LinkedBlockingQueue:" + lbq);
}
}
Java
// Java Program to Demonstrate adding
// elements to the LinkedBlockingQueue
import java.util.concurrent.LinkedBlockingQueue;
public class AddingElementsExample {
public static void main(String[] args)
{
// define capacity of LinkedBlockingQueue
int capacity = 15;
// create object of LinkedBlockingQueue
LinkedBlockingQueue lbq
= new LinkedBlockingQueue(capacity);
// add numbers
lbq.add(1);
lbq.add(2);
lbq.add(3);
// print queue
System.out.println("LinkedBlockingQueue:" + lbq);
}
}
Java
// Java Program to Demonstrate removing
// elements from the LinkedBlockingQueue
import java.util.concurrent.LinkedBlockingQueue;
public class RemovingElementsExample {
public static void main(String[] args)
{
// define capacity of LinkedBlockingQueue
int capacity = 15;
// create object of LinkedBlockingQueue
LinkedBlockingQueue lbq
= new LinkedBlockingQueue(capacity);
// add numbers
lbq.add(1);
lbq.add(2);
lbq.add(3);
// print queue
System.out.println("LinkedBlockingQueue:" + lbq);
// remove all the elements
lbq.clear();
// print queue
System.out.println("LinkedBlockingQueue:" + lbq);
}
}
Java
// Java Program Demonstrate iterating
// over LinkedBlockingQueue
import java.util.concurrent.LinkedBlockingQueue;
import java.util.Iterator;
public class IteratingExample {
public static void main(String[] args)
{
// define capacity of LinkedBlockingQueue
int capacityOfQueue = 7;
// create object of LinkedBlockingQueue
LinkedBlockingQueue linkedQueue
= new LinkedBlockingQueue(capacityOfQueue);
// Add element to LinkedBlockingQueue
linkedQueue.add("John");
linkedQueue.add("Tom");
linkedQueue.add("Clark");
linkedQueue.add("Kat");
// create Iterator of linkedQueue using iterator() method
Iterator listOfNames = linkedQueue.iterator();
// print result
System.out.println("list of names:");
while (listOfNames.hasNext())
System.out.println(listOfNames.next());
}
}
Java
// Java Program Demonstrate accessing
// elements of LinkedBlockingQueue
import java.util.concurrent.LinkedBlockingQueue;
public class AccessingElementsExample {
public static void main(String[] args)
{
// define capacity of LinkedBlockingQueue
int capacityOfQueue = 7;
// create object of LinkedBlockingQueue
LinkedBlockingQueue linkedQueue
= new LinkedBlockingQueue(capacityOfQueue);
// Add element to LinkedBlockingQueue
linkedQueue.add("John");
linkedQueue.add("Tom");
linkedQueue.add("Clark");
linkedQueue.add("Kat");
// find head of linkedQueue using peek() method
String head = linkedQueue.peek();
// print result
System.out.println("Queue is " + linkedQueue);
// print head of queue
System.out.println("Head of Queue is " + head);
// removing one element
linkedQueue.remove();
// again get head of queue
head = linkedQueue.peek();
// print result
System.out.println("\nRemoving one element from Queue\n");
System.out.println("Queue is " + linkedQueue);
// print head of queue
System.out.println("Head of Queue is " + head);
}
}
LinkedBlockingQueue:[1, 2, 3, 4, 5]
2. LinkedBlockingQueue(int capacity) :创建具有给定(固定)容量的 LinkedBlockingQueue。
LinkedBlockingQueue
例子:
Java
// Java program to demonstrate
// LinkedBlockingQueue(int initialCapacity) constructor
import java.util.concurrent.LinkedBlockingQueue;
public class GFG {
public static void main(String[] args)
{
// define capacity of LinkedBlockingQueue
int capacity = 15;
// create object of LinkedBlockingQueue
// using LinkedBlockingQueue(int initialCapacity)
// constructor
LinkedBlockingQueue lbq
= new LinkedBlockingQueue(capacity);
// add numbers
lbq.add(1);
lbq.add(2);
lbq.add(3);
// print queue
System.out.println("LinkedBlockingQueue:" + lbq);
}
}
LinkedBlockingQueue:[1, 2, 3]
3. LinkedBlockingQueue(Collection extends E> c) :创建一个容量为 Integer.MAX_VALUE 的 LinkedBlockingQueue,最初包含给定集合的元素,按集合迭代器的遍历顺序添加。
LinkedBlockingQueue
例子:
Java
// Java program to demonstrate
// LinkedBlockingQueue(Collection c) constructor
import java.util.concurrent.LinkedBlockingQueue;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
// Creating a Collection
Vector v = new Vector();
v.addElement(1);
v.addElement(2);
v.addElement(3);
v.addElement(4);
v.addElement(5);
// create object of LinkedBlockingQueue
// using LinkedBlockingQueue(Collection c)
// constructor
LinkedBlockingQueue lbq
= new LinkedBlockingQueue(v);
// print queue
System.out.println("LinkedBlockingQueue:" + lbq);
}
}
LinkedBlockingQueue:[1, 2, 3, 4, 5]
基本操作
1.添加元素
如果队列未满,LinkedBlockingQueue 的 add(E e) 方法将作为参数传递给方法的元素插入到此 LinkedBlockingQueue 的尾部。如果队列已满,则此方法将等待空间可用,并在空间可用后,将元素插入 LinkedBlockingQueue。
Java
// Java Program to Demonstrate adding
// elements to the LinkedBlockingQueue
import java.util.concurrent.LinkedBlockingQueue;
public class AddingElementsExample {
public static void main(String[] args)
{
// define capacity of LinkedBlockingQueue
int capacity = 15;
// create object of LinkedBlockingQueue
LinkedBlockingQueue lbq
= new LinkedBlockingQueue(capacity);
// add numbers
lbq.add(1);
lbq.add(2);
lbq.add(3);
// print queue
System.out.println("LinkedBlockingQueue:" + lbq);
}
}
LinkedBlockingQueue:[1, 2, 3]
2. 移除元素
LinkedBlockingQueue 的 remove(Object obj) 方法仅从该 LinkedBlockingQueue 中删除作为参数传递的给定对象的一个实例(如果存在)。它删除一个元素 e 使得 obj.equals(e) 并且如果这个队列包含元素 e 的一个或多个实例。如果此队列包含现在从 LinkedBlockingQueue 中删除的元素,则此方法返回 true。
Java
// Java Program to Demonstrate removing
// elements from the LinkedBlockingQueue
import java.util.concurrent.LinkedBlockingQueue;
public class RemovingElementsExample {
public static void main(String[] args)
{
// define capacity of LinkedBlockingQueue
int capacity = 15;
// create object of LinkedBlockingQueue
LinkedBlockingQueue lbq
= new LinkedBlockingQueue(capacity);
// add numbers
lbq.add(1);
lbq.add(2);
lbq.add(3);
// print queue
System.out.println("LinkedBlockingQueue:" + lbq);
// remove all the elements
lbq.clear();
// print queue
System.out.println("LinkedBlockingQueue:" + lbq);
}
}
LinkedBlockingQueue:[1, 2, 3]
LinkedBlockingQueue:[]
3. 迭代
LinkedBlockingQueue 的 iterator() 方法以正确的顺序返回与此 LinkedBlockingQueue 相同元素的迭代器。此方法返回的元素包含 LinkedBlockingQueue 的first(head)到last(tail)的所有元素。返回的迭代器是弱一致的。
Java
// Java Program Demonstrate iterating
// over LinkedBlockingQueue
import java.util.concurrent.LinkedBlockingQueue;
import java.util.Iterator;
public class IteratingExample {
public static void main(String[] args)
{
// define capacity of LinkedBlockingQueue
int capacityOfQueue = 7;
// create object of LinkedBlockingQueue
LinkedBlockingQueue linkedQueue
= new LinkedBlockingQueue(capacityOfQueue);
// Add element to LinkedBlockingQueue
linkedQueue.add("John");
linkedQueue.add("Tom");
linkedQueue.add("Clark");
linkedQueue.add("Kat");
// create Iterator of linkedQueue using iterator() method
Iterator listOfNames = linkedQueue.iterator();
// print result
System.out.println("list of names:");
while (listOfNames.hasNext())
System.out.println(listOfNames.next());
}
}
list of names:
John
Tom
Clark
Kat
4. 访问元素
LinkedBlockingQueue 的 peek() 方法返回 LinkedBlockingQueue 的头部。它检索 LinkedBlockingQueue 头部的值,但不删除它。如果 LinkedBlockingQueue 为空,则此方法返回 null。
Java
// Java Program Demonstrate accessing
// elements of LinkedBlockingQueue
import java.util.concurrent.LinkedBlockingQueue;
public class AccessingElementsExample {
public static void main(String[] args)
{
// define capacity of LinkedBlockingQueue
int capacityOfQueue = 7;
// create object of LinkedBlockingQueue
LinkedBlockingQueue linkedQueue
= new LinkedBlockingQueue(capacityOfQueue);
// Add element to LinkedBlockingQueue
linkedQueue.add("John");
linkedQueue.add("Tom");
linkedQueue.add("Clark");
linkedQueue.add("Kat");
// find head of linkedQueue using peek() method
String head = linkedQueue.peek();
// print result
System.out.println("Queue is " + linkedQueue);
// print head of queue
System.out.println("Head of Queue is " + head);
// removing one element
linkedQueue.remove();
// again get head of queue
head = linkedQueue.peek();
// print result
System.out.println("\nRemoving one element from Queue\n");
System.out.println("Queue is " + linkedQueue);
// print head of queue
System.out.println("Head of Queue is " + head);
}
}
Queue is [John, Tom, Clark, Kat]
Head of Queue is John
Removing one element from Queue
Queue is [Tom, Clark, Kat]
Head of Queue is Tom
LinkedBlockingQueue 的方法
METHOD | DESCRIPTION |
---|---|
clear() | Atomically removes all of the elements from this queue. |
contains(Object o) | Returns true if this queue contains the specified element. |
drainTo(Collection super E> c) | Removes all available elements from this queue and adds them to the given collection. |
drainTo(Collection super E> c, int maxElements) | Removes at most the given number of available elements from this queue and adds them to the given collection. |
forEach(Consumer super E> action) | Performs the given action for each element of the Iterable until all elements have been processed or the action throws an exception. |
iterator() | Returns an iterator over the elements in this queue in the proper sequence. |
offer(E e) | Inserts the specified element at the tail of this queue if it is possible to do so immediately without exceeding the queue’s capacity, returning true upon success and false if this queue is full. |
offer(E e, long timeout, TimeUnit unit) | Inserts the specified element at the tail of this queue, waiting if necessary up to the specified wait time for space to become available. |
put(E e) | Inserts the specified element at the tail of this queue, waiting if necessary for space to become available. |
remainingCapacity() | Returns the number of additional elements that this queue can ideally (in the absence of memory or resource constraints) accept without blocking. |
remove(Object o) | Removes a single instance of the specified element from this queue, if it is present. |
removeAll(Collection> c) | Removes all of this collection’s elements that are also contained in the specified collection (optional operation). |
removeIf(Predicate super E> filter) | Removes all of the elements of this collection that satisfy the given predicate. |
retainAll(Collection> c) | Retains only the elements in this collection that are contained in the specified collection (optional operation). |
size() | Returns the number of elements in this queue. |
spliterator() | Returns a Spliterator over the elements in this queue. |
toArray() | Returns an array containing all of the elements in this queue, in proper sequence. |
toArray(T[] a) | Returns an array containing all of the elements in this queue, in proper sequence; the runtime type of the returned array is that of the specified array. |
在类Java.util.AbstractCollection 中声明的方法
METHOD | DESCRIPTION |
---|---|
containsAll(Collection> c) | Returns true if this collection contains all of the elements in the specified collection. |
isEmpty() | Returns true if this collection contains no elements. |
toString() | Returns a string representation of this collection. |
在类Java.util.AbstractQueue 中声明的方法
METHOD | DESCRIPTION |
---|---|
add(E e) | Inserts the specified element into this queue if it is possible to do so immediately without violating capacity restrictions, returning true upon success, and throwing an IllegalStateException if no space is currently available. |
addAll(Collection extends E> c) | Adds all of the elements in the specified collection to this queue. |
element() | Retrieves, but does not remove, the head of this queue. |
remove() | Retrieves and removes the head of this queue. |
在接口Java.util.concurrent.BlockingQueue 中声明的方法
METHOD | DESCRIPTION |
---|---|
add(E e) | Inserts the specified element into this queue if it is possible to do so immediately without violating capacity restrictions, returning true upon success, and throwing an IllegalStateException if no space is currently available. |
poll(long timeout, TimeUnit unit) | Retrieves and removes the head of this queue, waiting up to the specified wait time if necessary for an element to become available. |
take() | Retrieves and removes the head of this queue, waiting if necessary until an element becomes available. |
在接口Java.util.Collection 中声明的方法
METHOD | DESCRIPTION |
---|---|
addAll(Collection extends E> c) | Adds all of the elements in the specified collection to this collection (optional operation). |
containsAll(Collection> c) | Returns true if this collection contains all of the elements in the specified collection. |
equals(Object o) | Compares the specified object with this collection for equality. |
hashCode() | Returns the hash code value for this collection. |
isEmpty() | Returns true if this collection contains no elements. |
parallelStream() | Returns a possibly parallel Stream with this collection as its source. |
stream() | Returns a sequential Stream with this collection as its source. |
toArray(IntFunction | Returns an array containing all of the elements in this collection, using the provided generator function to allocate the returned array. |
在接口Java .util.Queue 中声明的方法
METHOD | DESCRIPTION |
---|---|
element() | Retrieves, but does not remove, the head of this queue. |
peek() | Retrieves, but does not remove, the head of this queue, or returns null if this queue is empty. |
poll() | Retrieves and removes the head of this queue, or returns null if this queue is empty. |
remove() | Retrieves and removes the head of this queue. |
参考: Java : Java