0

Does anyone know if there a FIFO queue implementation in Java, or any other library which lets users set a maximum size, and automatically rejects any requests when the queue if full?

I've had a look at guava queue implementation, but from what I've seen it will remove the first element in the queue when it is full, rather than rejecting the request.

2
  • BlockingQueue Commented Apr 24, 2013 at 19:39
  • Which Guava queue implementation? Commented Apr 24, 2013 at 22:35

3 Answers 3

1

Most of the built in queues do this. I suggest ArrayBlockingQueue as this is a natural fit for a limited size, but you can use LinkedBlockingQueue as well. The BlockingDeque(s) support a limit as well.

BTW If you are using a queue with a thread I suggest you use an ExecutorService as it combines these in one.

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

2 Comments

I've had a look at ArrayBlockingQueue, but I can't seem to find any way to set the size. Do you know where I could find this?
There is no way to create an ArrayBlockingQueue without giving it a capacity. Just try creating one.
0

Use a decorator pattern over a simple queue such as:
Queue<String> queue = new LinkedList<String>();

Your wrapper code will make sure the max size is forced rejecting extra additions.

Comments

0
int size=500;
Queue<String> = new ArrayBlockingQueue<>(size);

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.