Table of Contents
BlockingQueue is introduced in java with concurrent package with ConcurrentHashMap. It is thread safe queue to put and take elements from it.
BlockingQueue is special type of queue which is used when one thread produces object and another thread consumes it.
Producer thread will keep inserting objects to queue until it reaches upper limit. Once this queue size has reached that limit then producer thread will get blocked and won’t able to put objects into queue until consumer thread starts consuming it.
Similarly consumer thread keep taking objects from queue until queue becomes empty. Once queue becomes empty, consumer thread get blocked and waits for producer threads for inserting objects into the queue.
If you put null to BlockingQueue, it will [NullPointerException](https://java2blog.com/exception-thread-main-java-lang-nullpointerexception/ “NullPointerException”) at run time.
It has two important methods
put : producer thread put objects into the queue until it reaches to the limit and waits for consumer thread to take out object after that.
take : consumer thread take out object from the queue until queue becomes empty. Once queue is empty, it waits for producer thread to put object into the queue.
Example:
In this example, we will see how to use BlockingQueue.
Create Producer thread which will create objects which will be consumed by Consumer thread.
1. Producer.java
1. Producer.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
package org.arpit.java2blog; import java.util.concurrent.BlockingQueue; public class Producer implements Runnable { BlockingQueue queue=null; public Producer(BlockingQueue queue) { super(); this.queue = queue; } @Override public void run() { for (int i = 1; i <=50; i++) { System.out.println("Produced item "+i); try { queue.put("item "+i); } catch (InterruptedException e) { e.printStackTrace(); } } } } |
2. Consumer.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
package org.arpit.java2blog; import java.util.concurrent.BlockingQueue; public class Consumer implements Runnable { BlockingQueue queue=null; public Consumer(BlockingQueue queue) { super(); this.queue = queue; } @Override public void run() { while(true) { try { System.out.println("Consumed "+queue.take()); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } |
3. BlockingQueueMain.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
package org.arpit.java2blog; import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.BlockingQueue; public class BlockingQueueMain { public static void main(String args[]) { BlockingQueue queue=new ArrayBlockingQueue(10); Producer producer=new Producer(queue); Consumer consumer=new Consumer(queue); new Thread(producer).start(); new Thread(consumer).start(); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
Produced item 1 Produced item 2 Consumed item 1 Produced item 3 Consumed item 2 Produced item 4 Consumed item 3 Produced item 5 Consumed item 4 Produced item 6 Consumed item 5 Produced item 7 Consumed item 6 Produced item 8 Consumed item 7 Produced item 9 Consumed item 8 Produced item 10 ... |
Source code:
Was this post helpful?
Let us know if this post was helpful. Feedbacks are monitored on daily basis. Please do provide feedback as that\'s the only way to improve.