BlockingQueue in JavaBefore directly jumping to the topic 'Blocking Queue' let us first understand Queue in brief. Queue is an ordered list of objects where insertions take place at the rear end of the list and deletion of elements takes place from the front end. Therefore, it is also said that Queue is based on FIFO ( First-In-First-Out ) principle. BlockingQueue is a queue that additionally supports operations that wait for the queue to become non-empty when we are trying to retrieve an element, and wait for the space to be empty when an element is to be inserted in the queue. Java 1.5 provides support for BlockingQueue interface along with other concurrent utility classes. Some important points with respect to the Blocking Queue :
BlockingQueue Implementing ClassesThere is no provision of directly providing an instance of the BlockingQueue since it as an interface, so to implement BlockingQueue we need to create classes implementing it.
LinkedBlockingQueue and ArrayBlockingQueue are the classes used to implement BlockingDeque class. These two classes are a composition of BlockingDeque and linked list data structure and BlockingDeque and array respectively. Syntax of using BlockingQueue : We make use of import statement in order to use the above classes and import java.util.concurrent.BlockingQueue package. Declaring a BlockingQueue Creating objects of BlockingQueue class using LinkedBlockingDeque Creating objects of BlockingQueue class using ArrayBlockingQueue Methods of the BlockingQueue InterfaceThere are three categories of methods used in the implementation of BlockingQueue : 1. Methods that throw an exception
2. Methods that return some value i. offer( ): It inserts the specific element to the BlockingQueue at the rear end of the queue. It returns false if the queue is full. The method can also be used with timeouts, that is time units can be passed as a parameter. For example: Here, value is the element to be inserted in the queue. The above method will insert an element to the BlockingQueue for 100 milliseconds. If the element cannot be inserted in 100 milliseconds, the method returns false. ii. peek( ): It returns the top or head of the BlockingQueue. It returns null if the queue is empty. iii. poll( ): It removes an element from the BlockingQueue. It returns null if the queue is empty. It can also be used with timeouts, that is time units can be passed as a parameter. 3. Methods that block the operation
BlockingQueue TypesThere are two types of BlockingQueue: 1. Unbounded Queue: Unbounded blocking queue is the queue that never blocks because its size could be grown to a very large size. The capacity of the BlockingQueue will be set to Integer.MAX_VALUE. When the elements are added, the size of the Unbounded queue grows. Syntax : 2. Bounded Queue: Another type of the blocking queue is the bounded queue. It can be created by passing the capacity of the queue to the constructor of the queue. Syntax : Example : Let us consider an example that explains the concept of BlockingQueue. Output: Content of BLockingQueue : [ A , B , C , D , E , F , G ] The number removed is : A Content of BLockingQueue after deleting one element : [ B , C , D , E , F , G ] Basic OperationsLet us have a broader look over different operations that can be performed on BlockingQueue :
1. Adding ElementsWe can add elements into a ' LinkedBlockedDeque ' in different ways depending on the type of structure we are trying to use it as. The most common method used to add elements in the rear end of the deque is the ' add( ) ' method. There is another function named ' addAll( ) ' method to add an entire collection of the elements to LinkedBlockingDeque. In order to use the deque as a queue function like ' add( ) ' and ' put( ) ' can be added in the program. Example : Output: Contents of Blocking Queue : [ A , B , C , D , E ] Contents of another Blocking Queue : [ A , B , C , D , E ] 2. Accessing ElementsWe can access the elements of the ' LinkedBlockingDeque ' using methods like contains( ), element( ), peek( ), poll( ). Example : Output: The contents of the Linked Blocking Queue is : [ A , B , C , D , E ] Yayy! Element C successfully founded in the queue The top element of the queue is : A 3. Deleting ElementsElements can be deleted from a LinkedBlockingDeque using remove(). Other methods such as take( ) and poll( ) can also be used in a way to remove the first and the last elements. Example : Output: The content of LinkedBlockingDeque is : [ A , B , C , D , E ] The content of the LinkedBlockingDeque after removing elements is : [ A , B , D ] 4. Iterating through the ElementsIn order to iterate through the elements of a ' LinkedBlockingDeque ' we can create an iterator and use the methods of the Iterable interface, which is the root of the Collection Framework of Java, to access the elements. The ' next( ) ' method of Iterable returns the element of any collection. Output: The content of the Linked Blocking Deque is : A B C D E BlockingQueue Methods
The Behavior of BlockingQueue Methods : BlockingQueue provides certain methods for insertion, removal, and examine operations on the Blocking queue. Each of the four sets of methods behaves differently if the requested operation is not satisfied immediately.
Next TopicNext Greater Element in Java
|