简体   繁体   English

Java FIFO队列实现

[英]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? 有谁知道在Java中是否有FIFO队列实现,或者是否有其他库可以让用户设置最大大小,并在队列满时自动拒绝任何请求?

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. 我看过番石榴队列的实现,但是从我所看到的来看,它会在队列满时删除队列中的第一个元素,而不是拒绝请求。

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. 我建议使用ArrayBlockingQueue,因为这自然适合有限的大小,但是您也可以使用LinkedBlockingQueue。 The BlockingDeque(s) support a limit as well. BlockingDeque也支持一个限制。

BTW If you are using a queue with a thread I suggest you use an ExecutorService as it combines these in one. 顺便说一句,如果您正在使用带有线程的队列,建议您使用ExecutorService,因为它将它们组合在一起。

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. 包装器代码将确保最大大小被强制拒绝额外的添加。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM