user1899174
user1899174

Reputation: 279

Java FIFO queue implementation

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

Answers (3)

grisha
grisha

Reputation: 399

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

Upvotes: 0

Cratylus
Cratylus

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

Peter Lawrey
Peter Lawrey

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

Related Questions