Java ArrayBlockingQueue
在本教程中,我们将借助示例来学习ArrayBlockingQueue类及其方法。
Java Collections框架的ArrayBlockingQueue类提供了使用数组的阻塞队列实现。

创建ArrayBlockingQueue
为了创建一个数组阻塞队列,我们必须导入java.util.concurrent.ArrayBlockingQueue包。
导入包后,可以使用以下方法在Java中创建数组阻塞队列:
ArrayBlockingQueue animal = new ArrayBlockingQueue<>(int capacity);
这里,Type - 数组阻塞队列的类型
capacity - 数组阻塞队列的大小
例如,
//创建大小为5的字符串类型ArrayBlockingQueue
ArrayBlockingQueue animals = new ArrayBlockingQueue<>(5);
//创建大小为5的整数类型ArrayBlockingQueue
ArrayBlockingQueue 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 animals = new ArrayBlockingQueue<>(5);
//使用 add()
animals.add("Dog");
animals.add("Cat");
//使用 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 animals = new ArrayBlockingQueue<>(5);
//添加元素
animals.add("Dog");
animals.add("Cat");
animals.add("Horse");
System.out.println("ArrayBlockingQueue: " + animals);
// 使用 peek()
String element = animals.peek();
System.out.println("访问元素: " + element);
// 使用 iterator()
Iterator iterate = animals.iterator();
System.out.print("ArrayBlockingQueue 元素: ");
while(iterate.hasNext()) {
System.out.print(iterate.next());
System.out.print(", ");
}
}
}
输出结果ArrayBlockingQueue: [Dog, Cat, Horse]
访问元素: Dog
ArrayBlockingQueue 元素: Dog, Cat, Horse,
删除元素remove() - 返回并从数组阻塞队列中删除指定的元素。如果队列为空,则抛出异常。
poll() - 返回并从数组阻塞队列中删除指定的元素。如果队列为空,则返回null。
clear() - 从数组阻塞队列中删除所有元素。
例如,import java.util.concurrent.ArrayBlockingQueue;
class Main {
public static void main(String[] args) {
ArrayBlockingQueue animals = new ArrayBlockingQueue<>(5);
animals.add("Dog");
animals.add("Cat");
animals.add("Horse");
System.out.println("ArrayBlockingQueue: " + animals);
// 使用 remove()
String element1 = animals.remove();
System.out.println("删除元素:");
System.out.println("使用 remove(): " + element1);
// 使用 poll()
String element2 = animals.poll();
System.out.println("使用 poll(): " + element2);
// 使用 clear()
animals.clear();
System.out.println("更新后的ArrayBlockingQueue: " + animals);
}
}
输出结果ArrayBlockingQueue: [Dog, Cat, Horse]
删除元素:
使用 remove(): Dog
使用 poll(): Cat
更新后的ArrayBlockingQueue: []
put() 和 take()方法
在多线程进程中,我们可以使用put()和take()来阻塞一个线程的操作,从而使其与另一个线程同步。这些方法将等待,直到它们能够成功执行。
put()方法
要将元素添加到数组阻塞队列的末尾,可以使用put()方法。
如果阵列阻塞队列已满,它将等待直到数组阻塞队列中有足够的空间来添加元素。
例如,import java.util.concurrent.ArrayBlockingQueue;
class Main {
public static void main(String[] args) {
ArrayBlockingQueue animals = new ArrayBlockingQueue<>(5);
try {
//添加元素到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 animals = new ArrayBlockingQueue<>(5);
try {
//添加元素到animals
animals.put("Dog");
animals.put("Cat");
System.out.println("ArrayBlockingQueue: " + animals);
//删除一个元素
String element = animals.take();
System.out.println("被删除的元素: " + element);
}
catch(Exception e) {
System.out.println(e);
}
}
}
输出结果ArrayBlockingQueue: [Dog, Cat]
被删除的元素: Dog
在这里,如果take()方法在等待过程中被中断,它将抛出InterrupedException异常。因此,我们必须把它封装在一个try…catch块。
其他方法方法内容描述
contains(element)在数组阻塞队列中搜索指定的元素。如果找到该元素,则返回true,否则返回false。
size()返回数组阻塞队列的长度。
toArray()将数组阻塞队列转换为数组并返回它。
toString()将数组阻塞队列转换为字符串
为什么要使用ArrayBlockingQueue?
ArrayBlockingQueue使用数组作为其内部存储。
它被认为是线程安全的集合。因此,它通常用于多线程应用程序中。
假设一个线程正在将元素插入队列,而另一个线程正在从队列中删除元素。
现在,如果第一个线程比第二个线程慢,那么数组阻塞队列可使第二个线程等待,直到第一个线程完成其操作。
本文详细介绍了Java中的ArrayBlockingQueue,包括如何创建、插入、访问、删除元素,以及核心的put()和take()方法。ArrayBlockingQueue是一个线程安全的阻塞队列,常用于多线程同步。通过实例代码展示了ArrayBlockingQueue的各种操作,如add()、offer()、peek()、remove()、poll()等,并解释了它们的工作原理。
1399

被折叠的 条评论
为什么被折叠?



