Wednesday 29 July 2015

Producer Consumer using BlockingQueue

Producer.java

import java.util.concurrent.BlockingQueue;
public class Producer implements Runnable {
   private final BlockingQueue<Integer> sharedQueue;

   public Producer(BlockingQueue<Integer> sharedQueue) {
      this.sharedQueue = sharedQueue;
   }

   @Override
   public void run() {
      for (int i = 0; i < 10; i++) {
         try {
            /**
             * String java.lang.Thread.getName()
             * Returns this thread's name.
             */
            System.out.println("Producer:"+
                          Thread.currentThread().getName()+":"+i);

            /**
             * Inserts the specified element into this queue,
             * waiting if necessary for space to become available.
             * Throws: InterruptedException, ClassCastException
             * NullPointerException - if the specified element is null
             * IllegalArgumentException - if some property of the
             * specified element prevents it from being added to this queue
             * */
            sharedQueue.put(i);
           
         } catch (InterruptedException e) {
            e.printStackTrace();
         }
      }
   }
}

Consumer.java

public class Consumer implements Runnable {
   private final BlockingQueue<Integer> sharedQueue;
  
   public Consumer(BlockingQueue<Integer> sharedQueue) {
      this.sharedQueue = sharedQueue;
   }

   @Override
   public void run() {
      for (int i = 0; i < 10; i++) {
         try {

            /**
             * Retrieves and removes the head of this queue,
             * waiting if necessary until an element becomes available.
             * Returns: the head of this queue
             * Throws: InterruptedException- if interrupted while waiting.
             * */

            System.out.println("Consumer:"+Thread.currentThread().getName()+
                      ":" + sharedQueue.take());
         } catch (InterruptedException e) {
            e.printStackTrace();
         }
      }
   }
}

TestProdConsum.java

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
public class TestProdConsum {

   public static void main(String[] args) {
      final BlockingQueue<Integer> sharedQueue =
                                   new ArrayBlockingQueue<Integer>(5);
      /** Producer 1. */
      Thread producer1 = new Thread(new Producer(sharedQueue), "p1");
     
      /** Consumer 1. */
      Thread consumer1 = new Thread(new Consumer(sharedQueue), "c1");
     
      /** Producer 2. */
      Thread producer2 = new Thread(new Producer(sharedQueue), "p2");
     
      /** Consumer 2. */
      Thread consumer2 = new Thread(new Consumer(sharedQueue), "c2");
      producer1.start();
      consumer1.start();
      producer2.start();
      consumer2.start();
   }
}

Output:
Producer: p1 : 0
Producer: p1 : 1
Producer: p1 : 2
Producer: p1 : 3
Producer: p1 : 4
Producer: p1 : 5
Producer: p2 : 0
Producer: p2 : 1
Consumer: c2 : 1
Consumer: c2 : 2
Consumer: c2 : 3
Consumer: c2 : 4
Consumer: c2 : 0
Producer: p2 : 2
Consumer: c2 : 1
Consumer: c2 : 2
Producer: p2 : 3
Producer: p2 : 4
Producer: p2 : 5
Producer: p2 : 6
Producer: p2 : 7
Producer: p2 : 8
Consumer: c1 : 0
Consumer: c1 : 3
Consumer: c1 : 4
Consumer: c1 : 5
Consumer: c1 : 6
Consumer: c1 : 7
Producer: p1 : 6
Consumer: c2 : 5
Consumer: c2 : 6
Producer: p2 : 9
Producer: p1 : 7
Consumer: c2 : 9
Consumer: c1 : 8
Consumer: c1 : 7
Producer: p1 : 8
Producer: p1 : 9
Consumer: c1 : 8
Consumer: c1 : 9

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...