Fixed Size Queue Implementations in Java

06 May 2023 Balmiki Mandal 0 Core Java

Fixed Size Queue Implementations in Java

A fixed size queue is a type of collection data structure in which the number of elements in the queue is limited. This is achieved by limiting the total size of the queue and restricting the number of elements that can be added to or removed from the queue. In Java, there are several different implementations of this type of queue, each with its own advantages and disadvantages.

ArrayBlockingQueue

The ArrayBlockingQueue implementation is a base implementation of a fixed size queue that uses an array as its underlying data structure. It has a fixed size set at construction time and follows a FIFO (first-in, first-out) approach. It is thread-safe, meaning that multiple threads can safely interact with the same queue instance. It is also very efficient in terms of memory usage and run time performance.

LinkedBlockingQueue

The LinkedBlockingQueue is an implementation of a fixed size queue that uses a doubly-linked list as its underlying data structure. This means it can store a greater number of elements than an ArrayBlockingQueue since the data structure does not have a fixed size. Similar to the ArrayBlockingQueue, it follows a FIFO approach and is thread-safe. However, it's a bit less efficient in terms of memory usage and runtime performance.

CircularFifoQueue

The CircularFifoQueue is an implementation of a fixed size queue that uses an array as its underlying data structure. It is thread-safe and follows a FIFO approach. In addition, it has an additional feature: once the queue is full, any new elements added to it will replace the oldest element in the queue. This means that it can act as a last-in, first-out queue as well as a first-in, first-out queue.

Conclusion

Fixed size queues are a useful data structure for applications where the size of the queue needs to remain constant and access to the elements is sequential. Implementations of fixed size queues in Java offer different features, performance characteristics, and levels of thread safety. It is important to choose the best implementation for each application depending on the specific requirements.

BY: Balmiki Mandal

Related Blogs

Post Comments.

Login to Post a Comment

No comments yet, Be the first to comment.