java.util.concurrent.BlockingQueue is used when you have one thread produces the objects and while the other thread consumes them. This BlockingQueue is basically used to solve the producer-consumer problem in java.
Picture below illustrates how a blocking queue would normally look.
In the above picture we can see that one thread puts the elements into the blocking queue using put() method while the other consumes it using the take() method.
Implementations of BlockingQueue:
1) ArrayBlockingQueue
2) LinkedBlockingQueue
3)PriorityBlockingQueue
4)SynchronousQueue
Following are the different methods used for performing the operations on BlockingQueue.
Insertion:
1) add(E e) : inserts an element into the queue if the space is available for insertion and returns true up on successful insertion of the element, throws IllegalStateException if no space is available for insertion.
2) offer(E e): inserts an element into the queue if the space is available for insertion and returns true up on successful insertion of the element, returns false if no space is available for insertion.
3) offer( E e, long timeout, TimeUnit unit): inserts the element into the queue if the space is available immediately and waits until the specified amount of time if the space is not available for insertion.
4) put(E e): inserts the element into the queue if the space is available, if the space is not available then it waits until the space becomes available.
Retrieval:
1) poll( long timeout, TimeUnit unit): Retrieves and removes the head of this queue, waiting up to the specified wait time if needed.
2)take(): Retrieves and removes the head of this queue, and waits if needed.
There are two ways in which we can create a BlockingQueue.
1)Specifying the limit.
BlockingQueue<String> queue = new ArrayBlockingQueue<String>(7);
In this case queue can only take until 7 elements in it. If the limit is reached then the producer has to wait(depending on the insertion method used) until the consumer consumes the element from it.
2) without specifying the limit
BlockingQueue<String> queue = new ArrayBlockingQueue<String>();
In this case Integer.MAX_VALUE is taken as the size limit of the queue.
Let’s look at a small example which illustrates the blocking queue.
public class Producer implements Runnable{
private BlockingQueue<String> queue ;
public Producer(BlockingQueue<String> queue) {
this.queue = queue;
}
public void run() {
try {
queue.put("element1");
Thread.sleep(10);
queue.put("element2");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public class Consumer implements Runnable{
private BlockingQueue<String> queue ;
public Consumer(BlockingQueue queue) {
this.queue = queue;
}
public void run()
{
try
{
System.out.println(“element 1 consumed” +queue.take());
System.out.println(“element 2 consumed” +queue.take());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public class BlockingQueueDemo {
public static void main(String[] args)
{
BlockingQueue<String> queue = new ArrayBlockingQueue(10);
Producer producer = new Producer(queue);
Consumer consumer = new Consumer(queue);
new Thread(producer).start();
new Thread(consumer).start();
}
}
Comments