java中的ArrayBlockingQueue
ArrayBlockingQueue
ArrayBlockingQueue 是 Java 并发包 (java.util.concurrent) 中的一个线程安全的阻塞队列实现。
它基于数组实现,容量固定,支持先进先出的顺序。
Array Blocking Queue 数组阻塞队列 读音: [əˈreɪ] [ˈblɒkɪŋ] [kjuː]
concurrent 同时发生的 /kənˈkʌrənt/
peek 偷看;窥视 /piːk/
ArrayBlockingQueue的常见方法
添加元素
1,boolean add(E e)将元素添加到队列尾部。如果队列已满,抛出异常。返回值是布尔类型
2,boolean offer(E e)将元素添加到队列尾部。如果队列已满,返回 false。返回值是布尔类型[常用]
3, void put(E e)将元素添加到队列尾部。如果队列已满,会造成阻塞,最好抛出异常
移除元素
1,E remove()移除并返回队列头部的元素。如果队列为空,抛出 NoSuchElementException,最好抛出异常。
2,E poll()移除并返回队列头部的元素。如果队列为空,返回 null[常用]
3,E take()移除并返回队列头部的元素。如果队列为空,就会造成阻塞。最好抛出异常,take必须放置在try-catch中,否则会在编译的时不会通过。
查看元素
1,E element()返回队列头部的元素(不移除)。如果队列为空,抛出 NoSuchElementException。
2,E peek()返回队列头部的元素(不移除)。如果队列为空,返回 null[常用]
其他方法
1,int size()返回队列中的元素数量。
2,int remainingCapacity()返回队列剩余的容量。
3,boolean contains(Object o)检查队列是否包含指定元素。
4,void clear()清空队列中的所有元素。
5,Object[] toArray()将队列转换为数组。
ps: E 表示泛型
ArrayBlockingQueue.add 添加元素
ArrayBlockingQueue.add 添加数据的时候。
如果添加的长度已经大于了设置的长度。
在程序运行的时候会报错 Queue full 队列已满
package part;
// 在util->concurrent包下
import java.util.concurrent.ArrayBlockingQueue;
public class Java01 {
public static void main(String[] args) {
ArrayBlockingQueue queue = new ArrayBlockingQueue(3);
queue.add("张三");
queue.add("李四");
queue.add("wangwu");
queue.add("赵六");
// 会出现报错,Queue full 队列已满
System.out.println(queue);
}
}
ArrayBlockingQueue.put 添加元素,超出设置的长度会造成阻塞
我们设置ArrayBlockingQueue的长度是3
当我们添加数据的长度大于3的时候
程序是会出现错误:编译不会通过,使用异常捕获后,还是会造成阻塞
挂号:赵六 输不出来
package part;
// 在util->concurrent包下
import java.util.concurrent.ArrayBlockingQueue;
public class Java01 {
public static void main(String[] args) {
ArrayBlockingQueue queue = new ArrayBlockingQueue(3);
try{
queue.put("张三");
System.out.println("挂号:张三");
queue.put("李四");
System.out.println("挂号:李四");
queue.put("wangwu");
System.out.println("挂号:wangwu");
queue.put("赵六");
System.out.println("挂号:赵六");
System.out.println(queue);
}catch (Exception e){
e.printStackTrace();
}
}
}
ArrayBlockingQueue.offer 添加元素
ArrayBlockingQueue.offer 也是新增数据的。
它的返回值是布尔值。
如果放入进去了。返回true,没有放进去,返回false。
package part;
// 在util->concurrent包下
import java.util.concurrent.ArrayBlockingQueue;
public class Java01 {
public static void main(String[] args) {
ArrayBlockingQueue queue = new ArrayBlockingQueue(2);
Boolean BoolA= queue.offer("A");
Boolean BoolB= queue.offer("B");
Boolean BoolC= queue.offer("C");
System.out.println(BoolA); // true
System.out.println(BoolB); // true
System.out.println(BoolC); // false
}
}
ArrayBlockingQueue.poll()
ArrayBlockingQueue.poll() 移除并返回队列头部的元素。如果队列为空,返回 null。
public class Java01 {
public static void main(String[] args) {
ArrayBlockingQueue queue = new ArrayBlockingQueue(3);
Boolean BoolA= queue.offer("A");
Boolean BoolB= queue.offer("B");
Boolean BoolC= queue.offer("C");
// [A, B, C]
System.out.println(queue);
// poll: 移除并返回队列头部的元素。如果队列为空,返回 null。
// 队列头部的元素是A,因此A会被移除,并返回A
System.out.println( queue.poll()); // 输出的是 A
// [B, C]
System.out.println(queue);
}
}
ArrayBlockingQueue.remove()移除并返回队列头部的元素
E ArrayBlockingQueue.remove()移除并返回队列头部的元素。
如果队列为空,抛出 NoSuchElementException,最好抛出异常。
package part;
// 在util->concurrent包下
import java.util.concurrent.ArrayBlockingQueue;
public class Java01 {
public static void main(String[] args) {
ArrayBlockingQueue queue = new ArrayBlockingQueue(3);
Boolean BoolA= queue.offer("A");
Boolean BoolB= queue.offer("B");
Boolean BoolC= queue.offer("C");
// [A, B, C]
System.out.println(queue);
System.out.println( queue.remove());
System.out.println( queue.remove());
System.out.println( queue.remove());
// 报错 Exception in thread "main" java.util.NoSuchElementException
// 因为:此时队列已经是空(空数组)在移除就会报错了
System.out.println(queue.remove());
}
}
take()移除并返回队列头部的元素。如果队列为空,就会造成阻塞。
E ArrayBlockingQueue.take()移除并返回队列头部的元素。
如果队列为空,就会造成阻塞。
最好抛出异常,take必须放置在try-catch中,否则会在编译的时不会通过。
package part;
// 在util->concurrent包下
import java.util.concurrent.ArrayBlockingQueue;
public class Java01 {
public static void main(String[] args) {
ArrayBlockingQueue queue = new ArrayBlockingQueue(3);
Boolean BoolA= queue.offer("A");
Boolean BoolB= queue.offer("B");
Boolean BoolC= queue.offer("C");
// [A, B, C]
System.out.println(queue);
try{
System.out.println( queue.take());
System.out.println( queue.take());
System.out.println( queue.take());
// take必须放置在try-catch中,否则会在编译的时不会通过。
// 如果队列为空,就会造成阻塞。
System.out.println(queue.take());
}catch (Exception e){
System.out.println(e);
}
}
}
element返回队列头部的元素(不移除)如果队列为空,抛出异常。
E element()返回队列头部的元素(不移除)。如果队列为空,抛出 NoSuchElementException。
package part;
// 在util->concurrent包下
import java.util.concurrent.ArrayBlockingQueue;
public class Java01 {
public static void main(String[] args) throws InterruptedException {
ArrayBlockingQueue queue = new ArrayBlockingQueue(3);
Boolean BoolA= queue.offer("A");
Boolean BoolB= queue.offer("B");
Boolean BoolC= queue.offer("C");
// [A, B, C]
System.out.println(queue);
System.out.println(queue.element()); // 输出A
System.out.println(queue.element()); // 输出A
}
}
package part;
// 在util->concurrent包下
import java.util.concurrent.ArrayBlockingQueue;
public class Java01 {
public static void main(String[] args) throws InterruptedException {
ArrayBlockingQueue queue = new ArrayBlockingQueue(3);
Boolean BoolA= queue.offer("A");
Boolean BoolB= queue.offer("B");
Boolean BoolC= queue.offer("C");
// 清空
queue.clear();
// 输出 []
System.out.println(queue);
// 报错,因为:如果队列为空,抛出 NoSuchElementException。
System.out.println(queue.element());
}
}
使用queue.element()安全的写法
package part;
// 在util->concurrent包下
import java.util.concurrent.ArrayBlockingQueue;
public class Java01 {
public static void main(String[] args) throws InterruptedException {
ArrayBlockingQueue queue = new ArrayBlockingQueue(3);
Boolean BoolA= queue.offer("A");
Boolean BoolB= queue.offer("B");
Boolean BoolC= queue.offer("C");
// 清空
queue.clear();
// 输出 []
System.out.println(queue);
// 安全的写法,如果是空数组,就不会取出来。或者使用peek
if(queue.size() >0){
System.out.println(queue.element());
}
}
}
使用 peek() 返回队列头部的元素(不移除)。如果队列为空,返回 null。
package part;
// 在util->concurrent包下
import java.util.concurrent.ArrayBlockingQueue;
public class Java01 {
public static void main(String[] args) throws InterruptedException {
ArrayBlockingQueue queue = new ArrayBlockingQueue(3);
Boolean BoolA= queue.offer("A");
Boolean BoolB= queue.offer("B");
Boolean BoolC= queue.offer("C");
System.out.println(queue.peek()); // A
queue.clear();
System.out.println(queue.peek()); // null
}
}
remainingCapacity方法返回的值返回队列剩余的容量
remainingCapacity() 方法返回的值返回队列剩余的容量。具体来说:
如果队列是空的,返回值是初始容量减去当前队列中元素的数量
如果队列已经满了,返回的剩余容量将为0。
在其他情况下,返回的剩余容量将是初始容量减去当前队列中元素的数量。
package part;
// 在util->concurrent包下
import java.util.concurrent.ArrayBlockingQueue;
public class Java01 {
public static void main(String[] args) throws InterruptedException {
ArrayBlockingQueue queue = new ArrayBlockingQueue(3);
Boolean BoolA= queue.offer("A");
Boolean BoolB= queue.offer("B");
Boolean BoolC= queue.offer("C");
// 3-3 =0 输出的是0
System.out.println(queue.remainingCapacity());
// 获取对队列中的元素数量
int len = queue.size();
System.out.println(len);// 3
//检查队列是否包含指定元素B,包含的话,返回trie
Boolean hasExist =queue.contains("B");
System.out.println(hasExist); // true
// 移除并返回队列头部的元素。如果队列为空,返回 null。
queue.poll();
System.out.println(queue); // [B, C]
// 3-2 = 1
System.out.println(queue.remainingCapacity());
}
}
尾声
准备开始学习java了。
今天学习的第四天,每天都会发文章,我要卷起来。
请小伙伴们监督我,奥利给
出处:https://www.cnblogs.com/ishoulgodo/
想问问题,打赏了卑微的博主,求求你备注一下的扣扣或者微信;这样我好联系你;(っ•̀ω•́)っ✎⁾⁾!
如果觉得这篇文章对你有小小的帮助的话,记得在右下角点个“推荐”哦,或者关注博主,在此感谢!
万水千山总是情,打赏5毛买辣条行不行,所以如果你心情还比较高兴,也是可以扫码打赏博主(っ•̀ω•́)っ✎⁾⁾!
想问问题,打赏了卑微的博主,求求你备注一下的扣扣或者微信;这样我好联系你;(っ•̀ω•́)っ✎⁾⁾!


如果文中有什么错误,欢迎指出。以免更多的人被误导。
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 清华大学推出第四讲使用 DeepSeek + DeepResearch 让科研像聊天一样简单!
· 推荐几款开源且免费的 .NET MAUI 组件库
· 实操Deepseek接入个人知识库
· 易语言 —— 开山篇
· 【全网最全教程】使用最强DeepSeekR1+联网的火山引擎,没有生成长度限制,DeepSeek本体