当前位置: 首页 > news >正文

8阻塞队列

阻塞队列能是⼀种 线程安全的数据结构 , 并且具有以下特性:
当队列满的时候, 继续⼊队列就会阻塞, 直到有其他线程从队列中 取⾛ 元素.
当队列空的时候, 继续出队列也会阻塞, 直到有其他线程往队列中 插⼊ 元素

标准库中的阻塞队列

在 Java 标准库中内置了阻塞队列. 如果我们需要在⼀些程序中使⽤阻塞队列, 直接使⽤标准库中的
BlockingQueue 是⼀个接⼝. 真正实现的类是 LinkedBlockingQueue.
put ⽅法⽤于阻塞式的⼊队列, take ⽤于阻塞式的出队列.
BlockingQueue 也有 offer, poll, peek 等⽅法, 但是这些⽅法不带有阻塞特性.

阻塞队列实现

通过 "循环队列" 的⽅式来实现.
使⽤ synchronized 进⾏加锁控制.
put 插⼊元素的时候, 判定如果队列满了, 就进⾏ wait. (注意, 要在循环中进⾏ wait. 被唤醒时不⼀定
队列就不满了, 因为同时可能是唤醒了多个线程).
take 取出元素的时候, 判定如果队列为空, 就进⾏ wait. (也是循环 wait)
class MyBlockingQueue{private int size=0;private int head=0;private int last=0;private String[] data=null;public MyBlockingQueue(int num){data=new String[num];}public void put(String num1) throws InterruptedException {synchronized (this){while(size>=data.length){this.wait();}data[last]=num1;last++;if(last>=data.length){last=0;}size++;this.notify();}}public String talk() throws InterruptedException {synchronized (this){while(size==0){this.wait();//防止提前唤醒}String arr=data[head];head++;if(head>=data.length){head=0;}size--;this.notify();return arr;}}
}
public class work1 {public static void main(String[] args) throws InterruptedException {MyBlockingQueue block1=new MyBlockingQueue(1000);Thread t1=new Thread(()->{while(true){try {System.out.println("消费元素"+block1.talk());Thread.sleep(1000);} catch (InterruptedException e) {throw new RuntimeException(e);}}});Thread t2=new Thread(()->{int num2=0;while(true){try {num2++;block1.put(num2+"");System.out.println("生产元素"+num2);} catch (InterruptedException e) {throw new RuntimeException(e);}}});t1.start();t2.start();

}


http://www.mrgr.cn/news/57234.html

相关文章:

  • 【无标题】LeetCode刷题--双指针--移动零
  • 四、多线程带来的的⻛险-线程安全
  • 聚类--机器学习西瓜书阅读笔记(六)
  • 力扣143.重排链表
  • 数据结构:数字统计
  • vue项目-img 标签的 error 事件
  • Python项目内网环境pdm install超时httpx.ReadTimeout: timed out
  • Vue Router实现路由懒加载
  • 简记 Vue3(一)—— setup、ref、reactive、toRefs、toRef
  • Linux中如何理解一切皆文件
  • Python包——Matplotlib
  • 各种数据类型的定义与常规计算
  • 第23周Java主流框架入门-SpringMVC 3.拦截器
  • 【单元测试】深入解剖单元测试的思维逻辑
  • 【论文速看】DL最新进展20241023-多模态、无监督学习、多任务、图像修复
  • 【哈工大_操作系统实验】Lab8 终端设备的控制
  • 买华为系的车,这个理由无法拒绝
  • hass docker openwrt配置
  • 软件分享丨PDF Shaper
  • 不错的二次元个人导航页源码
  • 从新手到高手:map和set的使用技巧全攻略(C++)
  • 重学SpringBoot3-集成Hazelcast
  • 微服务-nacos
  • 掘金.计算位置 x 到 y 的最少步数(简单01)
  • 面试总结分享:25道数据库测试题
  • Vue01