ArrayBlockingQueue implements the BlockingQueue interface. Before moving further, please a look at the BlockingQueue interface that help you better understand ArrayBlockingQueue and it's methods.
ArrayBlockingQueue
This is a classic "bounded buffer", in which a fixed-sized array holds elements inserted by producers and extracted by consumers. Once created, the capacity cannot be changed. Attempts to put an element into a full queue will result in the operation blocking;attempts to take an element from an empty queue will similarly block. We'll see an example of this also.
Let's have a look at the basic methods and it's working with the help of example
This example demonstrate the difference between add() and offer() method.
Sample Output
[3]
[3, 5]
[3, 5, 7]
[3, 5, 7, 4]
[3, 5, 7, 4, 9]
you can see element are added to tail
size of queue : 5
size of queue : 5
Now try the same code by un-commenting the line blockQue.add(9);
Sample output
[3]
[3, 5]
[3, 5, 7]
[3, 5, 7, 4]
[3, 5, 7, 4, 9]
you can see element are added to tail
size of queue : 5
Exception in thread "main" java.lang.IllegalStateException: Queue full
at java.util.AbstractQueue.add(Unknown Source)
at java.util.concurrent.ArrayBlockingQueue.add(Unknown Source)
at ArrayBlockingQueueDemo.main(ArrayBlockingQueueDemo.java:21)
You can see, add() method is throwing IllegalStateException because it's trying to increase the capacity of the queue.
This example demonstrate how FIFO order is follow while adding and removing element from queue.
Sample Output
[3]
[3, 5]
[3, 5, 7]
[3, 5, 7, 4]
[3, 5, 7, 4, 9]
you can see new elements are added to tail
Head of the queue & remvoing: 3
[5, 7, 4, 9]
Head of the queue & remvoing: 5
[7, 4, 9]
new element (32) added at the tail true
[7, 4, 9, 32]
Producer-Consume problem with ArrayBlockingQueue
In this example, we create ArrayBlockingQueue of size 3 in which producer insert element in a while loop and consumer thread consume element in a while loop. In while loop, if we insert more than 3 element it blocks until consumer consume element from the same queue.
Sample Output
0 inserted in queue
Consume element : 0
1 inserted in queue
2 inserted in queue
3 inserted in queue
Consume element : 1
4 inserted in queue
Consume element : 2
5 inserted in queue
Consume element : 3
6 inserted in queue
Related Post
PriorityQueue in java with examples
PriorityBlockingQueue in java with examples
How thread exchange data with exchange() method
How thread works in phases
If you know anyone who has started learning Java, why not help them out! Just share this post with them.
Thanks for studying today!...
ArrayBlockingQueue
- A bounded blocking queue backed by an array. It store the element internally in an array.That why is it called ArrayBlockingQueue.
- This queue orders elements FIFO (first-in-first-out).
- The head of the queue is that element that has been on the queue the longest time.
- The tail of the queue is that element that has been on the queue the shortest time.
- New elements are inserted at the tail of the queue, and the queue retrieval operations obtain elements at the head of the queue.
This is a classic "bounded buffer", in which a fixed-sized array holds elements inserted by producers and extracted by consumers. Once created, the capacity cannot be changed. Attempts to put an element into a full queue will result in the operation blocking;attempts to take an element from an empty queue will similarly block. We'll see an example of this also.
Let's have a look at the basic methods and it's working with the help of example
Modifier and Type | Method | Description |
boolean | add(E e) | Inserts the specified element at the tail of this queue if it is possible to do so immediately without exceeding the queue's capacity, returning true upon success and throwing an IllegalStateException if this queue is full. |
boolean | offer(E e) | Inserts the specified element at the tail of this queue if it is possible to do so immediately without exceeding the queue's capacity, returning true upon success and false if this queue is full. |
E | peek() | Retrieves, but does not remove, the head of this queue, or returns null if this queue is empty. |
E | poll() | Retrieves and removes the head of this queue, or returns null if this queue is empty. |
void | put(E e) | Inserts the specified element at the tail of this queue, waiting for space to become available if the queue is full. |
E | take() | Retrieves and removes the head of this queue, waiting if necessary until an element becomes available |
This example demonstrate the difference between add() and offer() method.
[3]
[3, 5]
[3, 5, 7]
[3, 5, 7, 4]
[3, 5, 7, 4, 9]
you can see element are added to tail
size of queue : 5
size of queue : 5
Sample output
[3]
[3, 5]
[3, 5, 7]
[3, 5, 7, 4]
[3, 5, 7, 4, 9]
you can see element are added to tail
size of queue : 5
Exception in thread "main" java.lang.IllegalStateException: Queue full
at java.util.AbstractQueue.add(Unknown Source)
at java.util.concurrent.ArrayBlockingQueue.add(Unknown Source)
at ArrayBlockingQueueDemo.main(ArrayBlockingQueueDemo.java:21)
You can see, add() method is throwing IllegalStateException because it's trying to increase the capacity of the queue.
This example demonstrate how FIFO order is follow while adding and removing element from queue.
Sample Output
[3]
[3, 5]
[3, 5, 7]
[3, 5, 7, 4]
[3, 5, 7, 4, 9]
you can see new elements are added to tail
Head of the queue & remvoing: 3
[5, 7, 4, 9]
Head of the queue & remvoing: 5
[7, 4, 9]
new element (32) added at the tail true
[7, 4, 9, 32]
Producer-Consume problem with ArrayBlockingQueue
In this example, we create ArrayBlockingQueue of size 3 in which producer insert element in a while loop and consumer thread consume element in a while loop. In while loop, if we insert more than 3 element it blocks until consumer consume element from the same queue.
Sample Output
0 inserted in queue
Consume element : 0
1 inserted in queue
2 inserted in queue
3 inserted in queue
Consume element : 1
4 inserted in queue
Consume element : 2
5 inserted in queue
Consume element : 3
6 inserted in queue
You'll notice from the above output that queue was bounded to 3 element at a time that how it's differ from unbounded queue where producer produce element without waiting.
You can try the above code by changing queue size to 1. where only element will be in a queue until consume by consumer.Related Post
PriorityQueue in java with examples
PriorityBlockingQueue in java with examples
How thread exchange data with exchange() method
How thread works in phases
If you know anyone who has started learning Java, why not help them out! Just share this post with them.
Thanks for studying today!...
No comments:
Post a Comment