Multithreading : Producer Consumer Problem

Take three class: Producer,Consumer and ProducerConsumerDemo.
Here ProducerConsumerDemo is main class.

Code:

  1. import java.util.ArrayList;  
  2.   
  3. public class Producer implements Runnable {  
  4.       
  5.     private ArrayList<Integer>list;  
  6.     public Producer(ArrayList<Integer>list) {  
  7.         // TODO Auto-generated constructor stub  
  8.         this.list=list;  
  9.     }  
  10.   
  11.     @Override  
  12.     public void run() {  
  13.         // TODO Auto-generated method stub  
  14.         int count=1;  
  15.         while (count<=3) {  
  16.             synchronized (list) {  
  17.                 list.add(count);  
  18.                 System.out.println("Produced :: "+count);  
  19.                 count++;  
  20.                 try {  
  21.                     list.notify();  
  22.                     System.out.println("Notify the consumer.");  
  23.                     list.wait();  
  24.                 } catch (InterruptedException e) {  
  25.                     // TODO: handle exception  
  26.                     e.printStackTrace();  
  27.                 }  
  28.             }  
  29.         }  
  30.           
  31.     }  
  32.   
  33. }  
  34.   
  35.   
  36. import java.awt.List;  
  37. import java.util.ArrayList;  
  38.   
  39. public class Consumer implements Runnable {  
  40.       
  41.     private ArrayList<Integer>list;  
  42.     public Consumer(ArrayList<Integer>list) {  
  43.         // TODO Auto-generated constructor stub  
  44.         this.list=list;  
  45.     }  
  46.   
  47.     @Override  
  48.     public void run() {  
  49.         // TODO Auto-generated method stub  
  50.         int count=1;  
  51.         while (count<=3) {  
  52.             synchronized (list) {  
  53.                 while (list.size()==0) {  
  54.                     try {  
  55.                         list.notify();  
  56.                         System.out.println("Notify the producer: ");  
  57.                         list.wait();  
  58.                     } catch (InterruptedException e) {  
  59.                         // TODO: handle exception  
  60.                         e.printStackTrace();  
  61.                     }  
  62.                 }  
  63.                 Integer value=list.remove(0);  
  64.                 System.out.println("Consumed ::"+value);  
  65.                 count++;  
  66.                 try {  
  67.                     list.notify();  
  68.                     System.out.println("Notify the producer: ");  
  69.                     list.wait();  
  70.                 } catch (InterruptedException e) {  
  71.                     // TODO: handle exception  
  72.                     e.printStackTrace();  
  73.                 }  
  74.             }  
  75.         }  
  76.           
  77.     }  
  78.   
  79. }  
  80.   
  81.   
  82. import java.util.ArrayList;  
  83.   
  84. public class ProducerConsumerDemo {  
  85.   
  86.     public static void main(String[] args) {  
  87.         // TODO Auto-generated method stub  
  88.         ArrayList<Integer>list=new ArrayList<Integer>();  
  89.         Thread t1=new Thread(new Producer(list));  
  90.         Thread t2=new Thread(new Consumer(list));  
  91.           
  92.         t1.start();  
  93.         t2.start();  
  94.           
  95.   
  96.     }  
  97.   
  98. }  
Share:

0 Comments:

Post a Comment