Reputation: 279
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.
Upvotes: 0
Views: 2709
Reputation: 54074
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.
Upvotes: 0
Reputation: 533442
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.
Upvotes: 1