Java Collections 框架的 ArrayBlockingQueue
类提供了使用 数组 实现的阻塞队列。
它实现了 Java BlockingQueue 接口。

创建 ArrayBlockingQueue
为了创建一个数组阻塞队列,我们必须导入 java.util.concurrent.ArrayBlockingQueue
包。
导入包后,我们在 Java 中创建数组阻塞队列的方法如下
ArrayBlockingQueue<Type> animal = new ArrayBlockingQueue<>(int capacity);
这里,
- Type - 数组阻塞队列的类型
- capacity - 数组阻塞队列的大小
例如,
// Creating String type ArrayBlockingQueue with size 5
ArrayBlockingQueue<String> animals = new ArrayBlockingQueue<>(5);
// Creating Integer type ArrayBlockingQueue with size 5
ArrayBlockingQueue<Integer> age = new ArrayBlockingQueue<>(5);
注意: 必须提供数组的大小。
ArrayBlockingQueue 的方法
ArrayBlockingQueue
类提供了 BlockingQueue
接口中所有方法的实现。
这些方法用于向数组阻塞队列中插入、访问和删除元素。
此外,我们将学习 put()
和 take()
方法,它们支持数组阻塞队列中的阻塞操作。
这两个方法使数组阻塞队列区别于其他常规队列。
插入元素
add()
- 将指定的元素插入数组阻塞队列。如果队列已满,它会抛出 异常。offer()
- 将指定的元素插入数组阻塞队列。如果队列已满,它返回false
。
例如,
import java.util.concurrent.ArrayBlockingQueue;
class Main {
public static void main(String[] args) {
ArrayBlockingQueue<String> animals = new ArrayBlockingQueue<>(5);
// Using add()
animals.add("Dog");
animals.add("Cat");
// Using offer()
animals.offer("Horse");
System.out.println("ArrayBlockingQueue: " + animals);
}
}
输出
ArrayBlockingQueue: [Dog, Cat, Horse]
访问元素
peek()
- 从数组阻塞队列的前面返回一个元素。如果队列为空,它返回null
。iterator()
- 返回一个 迭代器 对象,用于从数组阻塞队列中顺序访问元素。如果队列为空,它会抛出异常。我们必须导入java.util.Iterator
包才能使用它。
例如,
import java.util.concurrent.ArrayBlockingQueue;
import java.util.Iterator;
class Main {
public static void main(String[] args) {
ArrayBlockingQueue<String> animals = new ArrayBlockingQueue<>(5);
// Add elements
animals.add("Dog");
animals.add("Cat");
animals.add("Horse");
System.out.println("ArrayBlockingQueue: " + animals);
// Using peek()
String element = animals.peek();
System.out.println("Accessed Element: " + element);
// Using iterator()
Iterator<String> iterate = animals.iterator();
System.out.print("ArrayBlockingQueue Elements: ");
while(iterate.hasNext()) {
System.out.print(iterate.next());
System.out.print(", ");
}
}
}
输出
ArrayBlockingQueue: [Dog, Cat, Horse] Accessed Element: Dog ArrayBlockingQueue Elements: Dog, Cat, Horse,
移除元素
remove()
- 返回并从数组阻塞队列中删除指定的元素。如果队列为空,它会抛出异常。poll()
- 返回并从数组阻塞队列中删除指定的元素。如果队列为空,它返回null
。clear()
- 从数组阻塞队列中删除所有元素。
例如,
import java.util.concurrent.ArrayBlockingQueue;
class Main {
public static void main(String[] args) {
ArrayBlockingQueue<String> animals = new ArrayBlockingQueue<>(5);
animals.add("Dog");
animals.add("Cat");
animals.add("Horse");
System.out.println("ArrayBlockingQueue: " + animals);
// Using remove()
String element1 = animals.remove();
System.out.println("Removed Element:");
System.out.println("Using remove(): " + element1);
// Using poll()
String element2 = animals.poll();
System.out.println("Using poll(): " + element2);
// Using clear()
animals.clear();
System.out.println("Updated ArrayBlockingQueue: " + animals);
}
}
输出
ArrayBlockingQueue: [Dog, Cat, Horse] Removed Elements: Using remove(): Dog Using poll(): Cat Updated ArrayBlockingQueue: []
put() 和 take() 方法
在多线程进程中,我们可以使用 put()
和 take()
来阻塞一个线程的操作,以使其与另一个线程同步。这些方法将等待直到它们可以成功执行。
put() 方法
要将元素添加到数组阻塞队列的末尾,我们可以使用 put()
方法。
如果数组阻塞队列已满,它会等待直到数组阻塞队列中有空间可以添加元素。
例如,
import java.util.concurrent.ArrayBlockingQueue;
class Main {
public static void main(String[] args) {
ArrayBlockingQueue<String> animals = new ArrayBlockingQueue<>(5);
try {
// Add elements to animals
animals.put("Dog");
animals.put("Cat");
System.out.println("ArrayBlockingQueue: " + animals);
}
catch(Exception e) {
System.out.println(e);
}
}
}
输出
ArrayBlockingQueue: [Dog, Cat]
在这里,put()
方法在等待时可能会被中断,从而抛出 InterruptedException
。因此,我们必须将其包含在 try..catch 块中。
take() 方法
要从数组阻塞队列的前面返回并删除元素,我们可以使用 take()
方法。
如果数组阻塞队列为空,它会等待直到数组阻塞队列中有元素可以删除。
例如,
import java.util.concurrent.ArrayBlockingQueue;
class Main {
public static void main(String[] args) {
ArrayBlockingQueue<String> animals = new ArrayBlockingQueue<>(5);
try {
//Add elements to animals
animals.put("Dog");
animals.put("Cat");
System.out.println("ArrayBlockingQueue: " + animals);
// Remove an element
String element = animals.take();
System.out.println("Removed Element: " + element);
}
catch(Exception e) {
System.out.println(e);
}
}
}
输出
ArrayBlockingQueue: [Dog, Cat] Removed Element: Dog
在这里,take()
方法在等待时可能会被中断,从而抛出 InterrupedException
。因此,我们必须将其包含在 try...catch
块中。
其他方法
方法 | 描述 |
---|---|
contains(element) |
搜索数组阻塞队列以查找指定的元素。如果找到该元素,则返回 true ,否则返回 false 。 |
size() |
返回数组阻塞队列的长度。 |
toArray() |
将数组阻塞队列转换为数组并返回。 |
toString() |
将数组阻塞队列转换为字符串 |
为什么使用 ArrayBlockingQueue?
ArrayBlockingQueue
使用数组作为其内部存储。
它被认为是线程安全的集合。因此,它通常用于多线程应用程序。
假设一个线程正在向队列中插入元素,而另一个线程正在从队列中删除元素。
现在,如果第一个线程比第二个线程慢,那么数组阻塞队列可以让第二个线程等待,直到第一个线程完成其操作。