当前位置:网站首页>并发操作之——BlockingQueue
并发操作之——BlockingQueue
2022-06-09 03:23:00 【RobertTeacher】
并发操作
并发操作之——BlockingQueue.
并发操作之——BlockingQueue
一、编程里面解决生产消费者模型的几种方式
核心: 要保证生产者不会在缓冲区满时放入数据,消费者也不会在缓冲区空时消耗数据
常用的同步方法是采用信号或加锁机制
1、wait() / notify()方法
wait() / notify()方法
2、await() / signal()方法
用ReentrantLock和Condition实现等待/通知模型
3、Semaphore信号量
Semaphore信号量
4、BlockingQueue阻塞队列
ArrayBlockingQueue
LinkedBlockingQueue
put方法用来向队尾存入元素,如果队列满,则阻塞
take方法用来从队首取元素,如果队列为空,则阻塞
二、BlockingQueue和常见的阻塞队列
BlockingQueue:
1、当阻塞队列进行插入数据时,如果队列已满,线程将会阻塞等待直到队列非满
2、从阻塞队列读数据时,如果队列为空,线程将会阻塞等待直到队列里面是非空的时候
1、ArrayBlockingQueue
基于数组实现的一个阻塞队列,需要指定容量大小,FIFO先进先出顺序
2、LinkedBlockingQueue
基于链表实现的一个阻塞队列,如果不指定容量大小,默认Integer.MAX_VALUE, FIFO先进先出顺序。
3、PriorityBlockingQueue:
一个支持优先级的无界阻塞队列,默认情况下元素采用自然顺序升序排序,也可以自定义排序实现 java.lang.Comparable接口
4、DelayQueue
延迟队列,在指定时间才能获取队列元素的功能,队列头元素是最接近过期的元素,里面的对象必须实现 java.util.concurrent.Delayed 接口并实现CompareTo和getDelay方法
总结
生产消费者模型的几种方式和阻塞队列BlockingQueue的概念以及常见的阻塞队列。
边栏推荐
- Leetcode 1310. Subarray XOR query prefix and + XOR
- [play with Huawei cloud] functions and features of Kunpeng code migration tool
- Android 程序常用功能《清除缓存》
- 视频边缘计算网关EasyNVR硬件以服务方式启动一直报错,如何排查及解决?
- Traversal of binary tree
- The easynvr hardware of the video edge computing gateway always reports an error when it is started in the service mode. How to troubleshoot and solve it?
- Introduction to AI computing power
- Leetcode 1155. N façons de rouler les dés
- The writing speed is increased by tens of times. The application of tdengine in tostar intelligent factory solution
- No cached version available for offline mode
猜你喜欢

Please ask about the SQL injection problem

Ccf-csp 201812-4 data center minimum spanning tree

New Presto data source support, new symbol map, and release of dataease open source data visual analysis platform v1.11.0

What is the network transformer for? (Ethernet network LAN LAN communication isolation filter) production plant / product schematic diagram / common products / price influencing factors

Ccf-csp 201909-4 recommended system 100 points

Runtime constant area - Method area
![[Beijing Normal University] information sharing of the first and second postgraduate entrance examinations](/img/ec/884e656a921e20a5679a2960c9ac4d.jpg)
[Beijing Normal University] information sharing of the first and second postgraduate entrance examinations

关于我的那些事【我的2022】

Leetcode 238. Product of arrays other than itself

In unity, inherit the lifecycle of monobehavior game objects
随机推荐
Datetimeformatter date formatting and parsing
Ccf-csp 201412-3 call auction
Excuse me: is it safe for Haitong Securities to open an account
Introduction to related operations of Huawei cloud sandbox Lab
About me [my 2022]
sql server统计今天和昨天数据量差异大于10的情况
Leetcode 1155. N façons de rouler les dés
Luogu p3647 [apio2014] Lianzhu line solution
Redis6学习笔记-第一章-Redis的介绍与环境搭建
InfoQ geek media's 15th anniversary solicitation | one minute Automated Deployment Based on ECs [elastic ECS]
Ccf-csp 201803-3 URL mapping 100 points
The easynvr hardware of the video edge computing gateway always reports an error when it is started in the service mode. How to troubleshoot and solve it?
Excuse me: is it safe for CICC fortune to open an account
神经网络学习(四)----神经网络各层知识的简单总结
In unity, inherit the lifecycle of monobehavior game objects
The difference between single chip and chip
el-cascader 代码取消选择,手动删除某项
Vivado HLS int8/9 multiplication optimization
[detailed explanation of kubernetes 13] - dashboard deployment
Leetcode 560. And is the prefix and of the subarray of K