ArrayBlockingQueue使用单个锁进行插入和删除,但LinkedBlockingQueue使用2个单独的锁 [英] ArrayBlockingQueue uses a single lock for insertion and removal but LinkedBlockingQueue uses 2 separate locks

查看:545
本文介绍了ArrayBlockingQueue使用单个锁进行插入和删除,但LinkedBlockingQueue使用2个单独的锁的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在浏览ArrayBlockingQueue和LinkedBlockingQueue的源代码。 LinkedBlockingQueue有一个putLock和一个takeLock分别插入和删除,但ArrayBlockingQueue只使用一个锁。我相信LinkedBlockingQueue是基于简单,快速和实用的非阻塞和阻塞
并发队列算法
。在本文中,他们提到他们保留一个虚拟节点,使入口队从不必访问头和出列队员从来没有访问尾,避免了死锁场景。我想知道为什么ArrayBlockingQueue不借用相同的想法和使用2锁代替。

I was going through the source code of ArrayBlockingQueue and LinkedBlockingQueue. LinkedBlockingQueue has a putLock and a takeLock for insertion and removal respectively but ArrayBlockingQueue uses only 1 lock. I believe LinkedBlockingQueue was implemented based on the design described in Simple, Fast, and Practical Non-Blocking and Blocking Concurrent Queue Algorithms. In this paper, they mention that they keep a dummy node so that enqueuers never have to access head and dequeuers never have to access tail which avoids deadlock scenarios. I was wondering why ArrayBlockingQueue doesn't borrow the same idea and use 2 locks instead.

推荐答案

ArrayBlockingQueue必须避免重写条目它需要知道开始和结束的位置。 LinkedBlockQueue不需要知道这一点,因为它让GC担心清理队列中的节点。

ArrayBlockingQueue has to avoid overwriting entries so that it needs to know where the start and the end is. A LinkedBlockQueue doesn't need to know this as it lets the GC worry about cleaning up Nodes in the queue.

这篇关于ArrayBlockingQueue使用单个锁进行插入和删除,但LinkedBlockingQueue使用2个单独的锁的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆