Advertisement
apl-mhd

ProducerConsumerProblem

Jan 9th, 2018
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.74 KB | None | 0 0
  1. package producerConsumer;
  2.  
  3. public class Q{
  4.  
  5.  
  6.    public static int  n;
  7.  
  8.     //static  int count;
  9.  
  10.     boolean flag = false;
  11.  
  12.    synchronized void  put(int n){
  13.  
  14.  
  15.        while (flag){
  16.  
  17.            try {
  18.                wait();
  19.  
  20.            }
  21.            catch (InterruptedException e){e.printStackTrace();}
  22.        }
  23.  
  24.  
  25.            this.n=n;
  26.            System.out.println("put: "+n);
  27.            flag = true;
  28.            notify();
  29.  
  30.  
  31.     }
  32.  
  33.   synchronized int get(){
  34.  
  35.        while (!flag){
  36.  
  37.            try {
  38.                wait();
  39.            }
  40.            catch (InterruptedException e){e.printStackTrace();}
  41.        }
  42.  
  43.  
  44.  
  45.        flag = false;
  46.         System.out.println("Get: "+n);
  47.       notify();
  48.  
  49.       return n;
  50.     }
  51.  
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement