Java Collection 中的 ArrayBlockingQueue 和 LinkedBlockingQueue 是 BlockingQueue 接口的常见实现。
ArrayBlockingQueue : ArrayBlockingQueue 是Java中的一个类,它实现了BlockingQueue接口。 ArrayBlockingQueue 类及其迭代器实现了 Collection 和 Iterator 接口的所有可选方法。 ArrayBlockingQueue是一个由数组支持的有界 BlockingQueue。这里,有界意味着队列的大小是有限且固定的。一旦创建,我们就不能增加或缩小队列的大小。如果我们尝试将一个元素插入到一个完整的队列中,那么它将导致操作阻塞。同样,如果我们尝试从空队列中取出一个元素,那么操作也会被阻塞。 ArrayBlockingQueue以FIFO (先进先出)的顺序在内部存储 Queue 中的元素。队列头部或前面的元素是该队列中所有元素中最旧的元素。该队列尾部的元素是该队列所有元素的最新元素。新元素总是插入到队列的末尾或末尾,检索操作获取队列头的元素。
链接阻塞队列: LinkedBlockingQueue 是Java中的一个类,它实现了 BlockingQueue 接口。 LinkedBlockingQueue是一个由链接节点支持的可选有界BlockingQueue。这里, optional-bounded表示给 LinkedBlockingQueue 的容量是有界的,否则,它将是无界的。容量可以作为参数提供给 LinkedBlockingQueue 的构造函数。如果未指定,容量等于Integer.MAX_VALUE 。 Linked BlockingQueue 类及其迭代器实现了 Collection 和 Iterator 接口的所有可选方法。 LinkedBlockingQueue以FIFO (先进先出)的顺序在内部存储 Queue 中的元素。队列头部或前面的元素是该队列中所有元素中最旧的元素。该队列尾部的元素是该队列所有元素的最新元素。新元素总是插入到队列的末尾或末尾,检索操作获取队列头的元素。链接队列通常比基于数组的队列具有更高的吞吐量,但在大多数并发应用程序中的可预测性能较差。
LinkedBlockingQueue 演示
Java
// Java program to demonstrate LinkedBlockingQueue
import java.util.concurrent.LinkedBlockingQueue;
public class LinkedBlockingQueueDemo {
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
// ArrayBlockingQueue
import java.util.concurrent.ArrayBlockingQueue;
public class ArrayBlockingQueueDemo {
public static void main(String[] args)
{
// define capacity of ArrayBlockingQueue
int capacity = 15;
// create object of ArrayBlockingQueue
// using ArrayBlockingQueue(int initialCapacity)
// constructor
ArrayBlockingQueue abq
= new ArrayBlockingQueue(capacity);
// add numbers
abq.add(1);
abq.add(2);
abq.add(3);
// print queue
System.out.println("ArrayBlockingQueue:" + abq);
}
}
LinkedBlockingQueue:[1, 2, 3]
ArrayBlockingQueue 演示
Java
// Java program to demonstrate
// ArrayBlockingQueue
import java.util.concurrent.ArrayBlockingQueue;
public class ArrayBlockingQueueDemo {
public static void main(String[] args)
{
// define capacity of ArrayBlockingQueue
int capacity = 15;
// create object of ArrayBlockingQueue
// using ArrayBlockingQueue(int initialCapacity)
// constructor
ArrayBlockingQueue abq
= new ArrayBlockingQueue(capacity);
// add numbers
abq.add(1);
abq.add(2);
abq.add(3);
// print queue
System.out.println("ArrayBlockingQueue:" + abq);
}
}
ArrayBlockingQueue:[1, 2, 3]
ArrayBlockingQueue 和 LinkedBlockingQueue 的区别:
ArrayBlockingQueue |
LinkedBlockingQueue |
---|---|
It stores the elements internally in an array. | It stores the elements internally in linked nodes. |
ArrayBlockingQueue is bounded which means the size will never change after its creation. | LinkedBlockingQueue is optionally bounded which means it can optionally have an upper bound if desired. If no upper bound is specified, Integer.MAX_VALUE is used as the upper bound. |
It has lower throughput than linked nodes queues. | It has a higher throughput than array-based queues. |
It uses the single-lock double condition algorithm. It means that producer and consumer share a single lock. | It uses two lock queue algorithms and it has two lock conditions putLock and takeLock for inserting and removing elements respectively from the Queue. |
ArrayBlockingQueue always holds an object array | LinkedBlockingQueue is a linked node with an object with three object fields. |