I know there have been questions and discussions about this(ArrayBlockingQueue uses a single lock for insertion and removal but LinkedBlockingQueue uses 2 separate locks) and (What is the reason to implement ArrayBlockingQueue with one lock on tail and head?), but I still don't understand it.
There is an accepted answer in the link given above:
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.
I don't quite understand, can someone explain it to me?
Then I tried to transform ABQ with two locks and ran throughput tests following the code in book Java Concurrency in practice
, and it did prove that the double-locked version of ABQ has better throughput.
All code (including ABQ using double lock implementatio and test code from book Java Concurrency in practice
) can be seen here: https://pastebin.com/QpW9dsVc
Is there a bug in the ABQ code that uses the double lock implementation, If not, why not use double locks.