2

I am looking for a similar library as ArrayBlockingQueue. Just that, I don't need the thread safe feature (For better performance purpose) offered in it, as it is using ReentrantLock in the offer(E e) method.

What I would like is

  1. A FIFO queue.
  2. Has an initial capacity, where pushing new element in will fail if the queue is full.
  3. Thread safety is not a requirement.

I don't find ArrayQueue in Java standard library. Or, am I missing any classes?

5 Answers 5

1

There is a BoundedFiFoBuffer in the Common.Collections with the following properties:

  • The BoundedFifoBuffer is a very efficient implementation of Buffer that is of a fixed size.

  • The removal order of a BoundedFifoBuffer is based on the insertion order; elements are removed in the same order in which they were added.

  • The iteration order is the same as the removal order.

  • Note that this implementation is not synchronized.

  • BufferOverflowException is thrown on attempt to add elements to the full buffer.

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

Comments

1

Just use ArrayList with simple check for 'fullness' on add? Or write simple wrapper around it.

Comments

0

Use ConcurrentLinkedQueue (see http://download.oracle.com/javase/6/docs/api/java/util/concurrent/ConcurrentLinkedQueue.html for more details)

1 Comment

Agreed. Simple tweak around ArrayList should address the issue
0

http://commons.apache.org/collections/api-2.1.1/org/apache/commons/collections/BoundedFifoBuffer.html Should suit your needs. Be careful as it always returns true from add and instead will throw if you exceed the bounds

Comments

0

I would just use an ArrayBlockingQueue unless you know it won't be fast enough. If you are sending millions of objects per second, you could consider batching them to reduce overhead. If you have only hundreds of thousands per second its unlikely to matter.

Comments

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.