Advertisement
amitdutta121

consumer and producer

Mar 17th, 2018
239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.68 KB | None | 0 0
  1. package javaapplication1;
  2.  
  3. import java.util.Arrays;
  4. import java.util.Random;
  5. import java.util.logging.Level;
  6. import java.util.logging.Logger;
  7.  
  8. public class JavaApplication1 {
  9.    
  10.     static Thread prod , cons;
  11.    
  12.     static int[] arr = new int[10];
  13.     static int bufferSize = arr.length;
  14.     static int pos=0;
  15.     static int full=0; // number of slots full
  16.     static int empty=bufferSize; // number of slots empty
  17.    
  18.     static boolean entry = false;  
  19.  
  20.     public static void main(String[] args) {
  21.          producer();
  22.          cons();
  23.          
  24.          
  25.     }
  26.    
  27.     public static void cons(){
  28.         prod = new Thread(new Runnable() {
  29.             @Override
  30.             public void run() {
  31.                for(int i=0; i<bufferSize; i++){
  32.                    if(entry==true){
  33.                        try {
  34.                             Thread.sleep(200);
  35.                         } catch (InterruptedException ex) {
  36.                             Logger.getLogger(JavaApplication1.class.getName()).log(Level.SEVERE, null, ex);
  37.                         }
  38.                        
  39.                    }
  40.                     if(entry==false){
  41.                          full--;
  42.                          entry=true;
  43.                          System.out.println("Consumer working on index "+ i);
  44.                          arr[i] = 0;
  45.                          System.out.println(Arrays.toString(arr));
  46.                          entry = false;
  47.                          empty++;
  48.                     }
  49.                }
  50.             }
  51.         });
  52.         prod.start();
  53.     }
  54.     public static void producer(){
  55.          Random ran = new Random();
  56.         cons = new Thread(new Runnable() {
  57.             @Override
  58.             public void run() {
  59.                for(int i=0; i<bufferSize; i++){
  60.                    if(entry==true){
  61.                        try {
  62.                             Thread.sleep(200);
  63.                         } catch (InterruptedException ex) {
  64.                             Logger.getLogger(JavaApplication1.class.getName()).log(Level.SEVERE, null, ex);
  65.                         }
  66.                    }
  67.                     if(entry==false){
  68.                          int item = ran.nextInt(100);
  69.                          empty--;
  70.                          entry=true;
  71.                          System.out.println("Producer working on index "+ i);
  72.                          arr[i] = item;
  73.                          
  74.                          System.out.println(Arrays.toString(arr));
  75.                          entry = false;
  76.                          full++;
  77.                     }
  78.                }
  79.                
  80.             }
  81.         });
  82.         cons.start();
  83.     }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement