Advertisement
raffaele181188

Producer and Consumer with wait() and notify()

Apr 11th, 2012
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.17 KB | None | 0 0
  1. package com.zybnet.so;
  2.  
  3.  
  4. public class Main {
  5.    
  6.     public static void main(String[] args) throws Exception{
  7.         RoundRobinBuffer buf = new RoundRobinBuffer();
  8.         Thread produce = new Thread(new Producer(buf));
  9.         Thread consume = new Thread(new consumer(buf));
  10.  
  11.         produce.start();
  12.         consume.start();
  13.  
  14.         produce.join();
  15.         consume.join();
  16.        
  17.         System.out.println("Ok finished");
  18.     }
  19.  
  20.     static class RoundRobinBuffer {
  21.         private double buff[];
  22.         private volatile int nextWrite;
  23.         private volatile int nextRead;
  24.  
  25.         public RoundRobinBuffer(){
  26.             buff = new double[1000];
  27.             nextWrite = 0;
  28.             nextRead = 0;
  29.         }
  30.  
  31.         public synchronized void add(double add) throws InterruptedException{
  32.             if((nextWrite + 1) % 1000 == nextRead)
  33.                 wait();
  34.             buff[nextWrite] = add;
  35.             nextWrite = (nextWrite+1)%1000;
  36.             notify();
  37.         }
  38.        
  39.         public synchronized double get()throws InterruptedException{
  40.             if(nextRead == nextWrite)
  41.                    wait();
  42.             double temp = buff[nextRead];
  43.             nextRead = (nextRead+1)%1000;
  44.             notify();
  45.             return temp;
  46.         }
  47.  
  48.     }
  49.    
  50.     public static class consumer implements Runnable{
  51.         private RoundRobinBuffer consumBuff;
  52.  
  53.         public consumer (RoundRobinBuffer buff){
  54.             consumBuff = buff;
  55.         }
  56.  
  57.         public void run() {
  58.             for(int i = 1; i<=1000000; i++) {
  59.                 try {
  60.                     System.out.println("Consumer has consumed " + consumBuff.get());
  61.                 } catch (InterruptedException e) {
  62.                     throw new RuntimeException(e);
  63.                 }          
  64.             }
  65.             System.out.println("!!!Consumer has consumed all items!!!");
  66.         }
  67.     }
  68.    
  69.     public static class Producer implements Runnable{
  70.         private RoundRobinBuffer produceBuff;
  71.  
  72.         public Producer (RoundRobinBuffer buff){
  73.             produceBuff = buff;
  74.         }
  75.  
  76.         public void run(){
  77.             for(int i = 1; i<=1000000; i++) {
  78.                 try {
  79.                     produceBuff.add(i);
  80.                 } catch (InterruptedException e) {
  81.                     throw new RuntimeException(e);
  82.                 }
  83.                 if((i % 100000) == 0)
  84.                     System.out.println("Producer has produced "+ i );
  85.             }
  86.             System.out.println("Producer has produced all items.");
  87.         }
  88.        
  89.     }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement