0
Sponsored Links


Ad by Google
I have just completed Hibernate Tutorial and Hibernate Interview topic and now planning to start the ever green topic in java called concurrency in java. Of course concurrency is very huge topic and it will take time to complete. So, I am started with BlockingQueue which was introduced in java 1.5 and become very popular to solve producer consumer problem.
In this post, we will solve these questions -
What is ArrayBlockingQueue?
How to use ArrayBlockingQueue with example?
When to use ArrayBlockingQueue?

What is ArrayBlockingQueue

The ArrayBlockingQueue is an implementation class of BlockingQueue interface, which extends Queue and Queue extends Collection interface.
That means ArrayBlockingQueue act as Queue(FIFO) and supports all the operations of Collection interface. It's a fixed size of Queue and backed by an array. Because of it's size is fixed, you cannot add more element than the specified size of the ArrayBlockingQueue and you can specify the size of the queue at the time of instantiation of the ArrayBlockingQueue. Although ArrayBlockingQueue is supports all the operations of Collection interface, it's a thread safe and designed to be used primarily for producer-consumer queues.

How to use ArrayBlockingQueue

To use ArrayBlockingQueue, api gives few methods to add/get/remove element from the queue, just to add element to the queue it has four different methods with different purpose -
public boolean add(E) - If queue is full and trying to inserting element into the queue, it'll throws java.lang.IllegalStateException
public boolean offer(E) - If queue is full and trying to inserting element into the queue, it'll return false immediately.
public boolean put(E) throws InterruptedException - If queue is full and trying to inserting element into the queue, it'll wait for space to become available inside the queue.
public boolean offer(E,long,TimeUnit) throws InterruptedException - Insert the element into the queue,waiting up to the specified wait time if necessary for space to become available.
The same way to delete elements from the queue it has four different methods, for the behavior of the methods please see BlockingQueue Tutorial

OK, Lets see an example of ArrayBlockingQueue -
Chef.java class which will produce the pizza
package com.javamakeuse.poc;

import java.util.concurrent.BlockingQueue;

/**
 * Chef class to produce pizza
 * 
 * @author subodh.ray
 * 
 */
public class Chef implements Runnable {

 private final BlockingQueue<String> bQueue;
 private final String message;
 public Chef(BlockingQueue<String> bQueue, String message) {
  this.bQueue = bQueue;
  this.message = message;
 }

 @Override
 public void run() {
  bQueue.add(message);
 }

}
Customer.java class to consume the pizza produced by producer(Chef.java)
package com.javamakeuse.poc;

import java.util.concurrent.BlockingQueue;

/**
 * Customer class to consumed pizza
 * 
 * @author subodh.ray
 * 
 */
public class Customer implements Runnable {

 private BlockingQueue<String> bQueue;

 public Customer(BlockingQueue<String> bQueue) {
  this.bQueue = bQueue;
 }

 @Override
 public void run() {
  try {
   for (String message = bQueue.take(); message != null; message = bQueue
     .take()) {
    System.out.println("Consumed pizza - " + message);
   }
  } catch (InterruptedException e) {
   e.printStackTrace();
  }
 }

}
Restaurant.java class where pizza produced and consumed.
package com.javamakeuse.poc;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
/**
 * Restaurant class where pizza produce and consumed
 * 
 * @author subodh.ray
 * 
 */
public class Restaurant {

 public static void main(String[] args) throws InterruptedException {

  BlockingQueue<String> bQueue = new ArrayBlockingQueue<String>(2);

  // producing in queue
  (new Thread(new Chef(bQueue, "Large Pizza"))).start();
  (new Thread(new Chef(bQueue, "Medium Pizza"))).start();

  // adding 3rd element will throws IllegalStateException
  // because queue is already full.
  (new Thread(new Chef(bQueue, "Small Pizza"))).start();

  // Consuming from the queue
  (new Thread(new Customer(bQueue))).start();
 }

}
OUT PUT:
Consumed pizza - Large Pizza
Consumed pizza - Medium Pizza
Exception in thread "Thread-2" java.lang.IllegalStateException: Queue full
 at java.util.AbstractQueue.add(AbstractQueue.java:98)
 at java.util.concurrent.ArrayBlockingQueue.add(ArrayBlockingQueue.java:283)
 at com.javamakeuse.poc.Chef.run(Chef.java:23)
 at java.lang.Thread.run(Thread.java:722)


That's it.
Sponsored Links

0 comments:

Post a Comment