BlockingQueue in Java10 Sept 2024 | 11 min read Before 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 |
In this section, we will learn everything about the , i.e., what a console is, how we can use the console, how we can implement output in the console, how we can take input using the console, etc. What is a Console? To run a program, we might...
18 min read
In this section, we will learn how to calculate the normal and trace of a matrix in Java. Before moving to, the program, first we will understand the what is normal and trace of a matrix. Normal of a Matrix The normal of a matrix is the square...
5 min read
A queue is another kind of linear data structure that is used to store elements just like any other data structure but in a particular manner. In simple words, we can say that the queue is a type of data structure in the Java programming language...
10 min read
What is ? The SE stands for Java Standard Edition is a computing platform in which we can execute software, and it can be used for development and deployment of portable code for desktop and server environments. It has the Java programming language in use. It is...
5 min read
The Java DecimalFormat class's getPositiveSuffix() method is used to retrieve this DecimalFormat instance's positive suffix value. Syntax: public String getPositiveSuffix() Parameters: No parameters are accepted by this method. Return Value: The DecimalFormat instance's positive suffix value is returned by this method. Example 1: The DecimalFormat class in Java is used in this...
2 min read
In Java programming, identifiers help make different elements inside a program easier to recognize and use by acting as symbolic names for them. Numerous entities, including classes, variables, methods, packages, constants, and more, can be represented by these identifiers. Developers can enhance the readability and...
6 min read
Working with dates is a common task in software development, and it often involves dealing with various date formats. In Java, detecting the format of a given date string can be challenging due to the different conventions and patterns used worldwide. However, Java provides several powerful...
6 min read
What is Platform? The environment in which a program runs is known as a platform. Environment includes software, hardware, libraries, and dependencies. What does platform independence mean? When a programming language can run on different operating systems without any modifications or adjustments is known as platform independence. The...
4 min read
In Java, a package is a group of classes, interfaces, enumeration, and annotations. Java contains many pre-defined packages such as java.lang, java.io, java.net, etc. When we create any Java program the java.lang package is imported by default. We need not to write the package name at...
3 min read
In this section, we will be discussing how to print the reverse of a vowel string in Java. Vowels are the letters "a", "e", "i", "o", and "u", and a vowel string is a string that contains only vowels. We will first define the problem statement...
4 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India