3

When I instantiate ArrayBlockingQueue, I must also put int capacity in its constructor. Same thing doesn't apply for LinkedBlockingQueue. I am interested in why is that?

Of course, I can also put bound in LinkedBlockingQueue, but it is optimal. Why is it optimal here, and not in ArrayBlockingQueue? Couldn't ArrayBlockingQueue have initial capacity on default like ArrayList has?

1
  • To avoid queue growing too large. A queue size in practice should be close to zero, or handle spikes. If queue is full, add method will block. Linked Queues can be optionally bounded. Commented May 24, 2024 at 20:51

1 Answer 1

3

The documentation for ArrayBlockingQueue says.

This is a classic "bounded buffer", in which a fixed-sized array holds elements inserted by producers and extracted by consumers. Once created, the capacity cannot be changed. Attempts to put an element into a full queue will result in the operation blocking; attempts to take an element from an empty queue will similarly block.

Since the capacity is fixed and can't change, it is up to the user of the class to decide when the queue should start to block.

The documentation for LinkedBlockQueue says

An optionally-bounded blocking queue based on linked nodes. This queue orders elements FIFO (first-in-first-out). The head of the queue is that element that has been on the queue the longest time. The tail of the queue is that element that has been on the queue the shortest time. New elements are inserted at the tail of the queue, and the queue retrieval operations obtain elements at the head of the queue. Linked queues typically have higher throughput than array-based queues but less predictable performance in most concurrent applications.

The optional capacity bound constructor argument serves as a way to prevent excessive queue expansion. The capacity, if unspecified, is equal to Integer.MAX_VALUE. Linked nodes are dynamically created upon each insertion unless this would bring the queue above capacity.

In this case, the blocked based on a supplied capacity. If specified, that blocking capacity is enforced the same as the ArrayBlockingQueue

Sign up to request clarification or add additional context in comments.

5 Comments

I read the docs already, thanks anyway. But that doesn't actually answer my question. I was wondering why is Linked capacity optimal, and Array's not.
I think you meant optional (or maybe not). Anyway, it's because it is an optionally bounded queue. That is just a feature of the class which is different than ArrayBlockingQueue. And your question also asked why it couldn't be done like an ArrayList. And as I explained, the ArrayBlockingQueue capacity is fixed, whereas ArrayList isn't because ArrayList does not block.
Yeah I follow you :) I understand that is how they are implemented, but why not have same implementation (make both optimal or not for example). And then decide if you would like array or linkedList structure.
Just a guess but it could be because LinkedLists grow automatically and don't require reserved space. The max capacity of a linkedList (which it would probably never reach BTW) is Integer.MAX_VALUE. To do the same in an ArrayBlockingQueue the array would have to be that big. If it has to continually copy like an arrayList and update itself that would be a performance hit. To know for certain you could email Doug Lea and ask him. He wrote both classes.
Thanks mate, appreciate it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.