ArrayBlockingQueue:并发put和take [英] ArrayBlockingQueue: concurrent put and take

查看:956
本文介绍了ArrayBlockingQueue:并发put和take的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么ABQ没有使用LinkedBlockingQueue的方式实现。我们可以使用AtomicInteger来保持ABQ中的轨道计数也与LBQ相同。我们可以使用ABQ的两个锁。
我绊倒了类似的问题。 ArrayBlockingQueue使用单个锁定插入和删除,但LinkedBlockingQueue使用2个独立的锁

Why ABQ has not been implemented using the way LinkedBlockingQueue. We can use AtomicInteger to keep Track count in ABQ also the same way LBQ does. We can use the Two Locks for ABQ too. I stumble upon the similar question on SO. ArrayBlockingQueue uses a single lock for insertion and removal but LinkedBlockingQueue uses 2 separate locks

但我不明白这个问题的答案。我需要帮助理解,如果我们实现ABQ使用两个锁会出现的问题。
如果有人可以给出可能失败的竞争条件的示例,那将是非常好的。
这个问题可以标记为重复,但我真的寻找一个更具描述性的答案。这将是一个很大的帮助。

But I couldn't understand the answer for that question. I need help in understanding that problem which would arise if we implement ABQ using two locks. If would be very nice if somebody can give example of a race condition where it might fail. This question can be marked as a duplicate, but am really looking for a more descriptive answer. That would be a great help.

我在这里粘贴了一个代码 http ://pastebin.com/ZD1uFy7S 。任何人都可以显示在粘贴的代码中是否可能存在竞争条件。

I have pasted a code here http://pastebin.com/ZD1uFy7S . can anyone show whether there could be a possible race condition in the code pasted.

推荐答案

我得出的结论是,ABQ可以实现与LBQ相同的方式。我已粘贴代码在粘贴bin,以显示 http://pastebin.com/ZD1uFy7S
不实现这种方式的主要原因是因为代码得到两个复杂,特别是因为支持迭代器和性能的好处是不重要去更复杂的实现。

I researched a little bit. I came to the conclusion that yes ABQ can be implemented the same way as LBQ. I have pasted a code in paste bin to show that http://pastebin.com/ZD1uFy7S. The main reason for not implementing that way was because code was getting two complex especially because to support iterator and the performance benefits wasn't significant to go for more complex implementation.

这篇关于ArrayBlockingQueue:并发put和take的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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