当前位置:网站首页>阻塞队列LinkedBlockingQueue 源码解析
阻塞队列LinkedBlockingQueue 源码解析
2022-07-28 06:45:00 【trigger333】
目录
BlockingQueue的实现有很多,下面主要介绍下LinkedBQ。

构造方法
默认的容量是 integer最大值

linkedBQ里面用的是 单向链表

添加元素
put

offer的实现和ArrayBQ差不多
阻塞队列ArrayBlockingQueue 源码解析_trigger333的博客-CSDN博客
拉取元素
和添加相对应
poll对应offer, take对应put。

和ArrayBQ的区别
LinkedBQ 是链表,ArrayBQ是数组,在频繁添加的时候LinkedBQ 需要创建节点,而ArrayBQ直接添加即可,在频繁删除时LinkedBQ的节点会自己指向自己去 help GC,而ArrayBQ只需要将操作index即可,所以在单一操作较多时,ArrayBQ的添加和删除性能更高,如果既有添加也有删除,那么LinkedBQ 这两种操作是互不干扰的,而ArrayBQ时只能添加或者只能删除。
LinkedBQ 里面有两个锁,一个是putLock,一个是takeLock,也有两个Condition,也就是在put元素的时候如果里面本身就有元素的话,是不会影响到take的,只是当链表中元素从0变为1 的时候会通知等待的take线程。同理take也不会影响到put。
而ArrayBQ里面只有一把锁lock和两个Condition,put的时候不能take,take 的时候不能put。
所以LinkedBQ在多线程的生产者和消费者模式下的吞吐量会更大。
具体实验可参照
Java线程池实验:ArrayBlockingQueue和LinkedBlockingQueue吞吐量测试_trigger333的博客-CSDN博客
Java线程池实验:ArrayBlockingQueue和LinkedBlockingQueue性能测试_trigger333的博客-CSDN博客_arrayblockqueue 测试
边栏推荐
猜你喜欢

Unity切换到另一个场景的时候,发现该场景变暗了

Five screens, VR, projection, "Wei Xiaoli" rolled up on the intelligent cockpit

对spark算子aggregateByKey的理解

A group of South University students rely on science and technology to go to sea, with an annual income of 1billion

Parse tree structure JS

【17】 Establish data path (upper): instruction + operation =cpu
![[Qt5] a method of multi window parameter transmission (using custom signal slot) and case code download](/img/6d/870add6179f0e3a2f9b719f79594f3.png)
[Qt5] a method of multi window parameter transmission (using custom signal slot) and case code download
![[pyqt] pyqt development experience_ How to find events and methods of controls](/img/40/7597d6413c88793e22276325d5f602.png)
[pyqt] pyqt development experience_ How to find events and methods of controls

OSPF comprehensive experiment (7.12)

Qt使用信号量控制线程(QSemaphore)
随机推荐
js糖果消消乐小游戏源码
MPLS -- multi protocol label switching technology
半桥BUCK电路—记录篇
Unity切换到另一个场景的时候,发现该场景变暗了
CarSim simulation quick start (XIII) - steering system
DCL singleton mode
Usage of qgroupbox
Openstack dashboard configuring public network access
In the task manager, the CPU speed displayed is greater than its maximum speed [main frequency]
Understanding of spark operator aggregatebykey
解决EMC、EMI传导干扰的八大方法
Find out whether the number exists from the matrix
js卡片层叠样式的图片切换js特效
本人男,27岁技术经理,收入太高,心头慌得一比
Is the salary of test / development programmers unbalanced? Busy life, all kinds of job hopping
The core packages and middleware required for golang development cover all areas of the project and are worth collecting
opencv+paddle orc 识别图片提取表格信息
Five screens, VR, projection, "Wei Xiaoli" rolled up on the intelligent cockpit
How to use QT help documents
Usage of constructors