001    package pnc;
002    import java.awt.*;
003    import java.util.*;
004    import cs101.awt.DisplayField;
005    
006    public
007    abstract class NonInterPNC extends PNC {
008      private Production pro;
009      private Consumption con;  
010    
011      public void init() {  
012        // call super class layout
013        super.init();
014        
015        // Create production and consumption 
016        pro = new Production(this.store, this.toProduce);
017        con = new Consumption(this.store, this.toConsume);    
018      }
019    
020      public void start() {
021        super.start();
022        
023        // start the production and consumption 
024        pro.start();
025        con.start();
026    
027      }
028     
029    }
030    
031    class Production extends Thread {
032      private Buffer storage;
033      private DisplayField display;
034    
035      public Production(Buffer storage, DisplayField display) {
036        this.storage = storage;
037        this.display = display;
038      }
039    
040      public void run() {
041        Random randomizer = new Random(); 
042        while (true) {
043          // start a producer
044          Producer maker = new Producer(this.storage, this.display);
045          maker.start();
046          
047          // use the randomizer here to find a good amount to wait
048          Float ran = new Float(randomizer.nextFloat()*5000);
049          int wait = ran.intValue();
050    
051          // wait a random amount
052          try { Thread.sleep(wait); } 
053          catch (InterruptedException e) {}
054    
055        }
056      }
057    }
058    
059    class Consumption extends Thread {
060      private Buffer supply;
061      private DisplayField display;
062    
063      public Consumption(Buffer supply, DisplayField display) {
064        this.supply = supply;
065        this.display = display;
066      }
067    
068      public void run() {
069        Random randomizer = new Random(); 
070        while (true) {
071          // start a consumer
072          Consumer unmaker = new Consumer(this.supply, this.display);
073          unmaker.start();
074         
075          // use the randomizer here to find a good amount to wait
076          Float ran = new Float(randomizer.nextFloat()*5000);
077          int wait = ran.intValue();
078          System.out.println("Wait " + wait);
079          // wait a random amount
080          try { Thread.sleep(wait); } 
081          catch (InterruptedException e) {}
082    
083        }
084      }
085    }
086    
087    
088    
089    
090    
091