1  // File: SharedCell.java (Fig. 13.5 in JHTP2)
  2  //
  3  // Show multiple threads modifying shared object.
  4  // Use synchronization to ensure that both threads 
  5  // access the shared cell properly.
  6  
  7  public class SharedCell {
  8     public static void main( String args[] )
  9     {
 10        HoldInteger h = new HoldInteger();
 11        ProduceInteger p = new ProduceInteger( h );
 12        ConsumeInteger c = new ConsumeInteger( h );
 13  
 14        p.start();
 15        c.start();
 16     }
 17  }
 18  
 19  class ProduceInteger extends Thread {
 20     private HoldInteger pHold;
 21  
 22     public ProduceInteger( HoldInteger h )
 23     {
 24        pHold = h;
 25     }
 26  
 27     public void run()
 28     {
 29        for ( int count = 0; count < 10; count++ ) {
 30           // sleep for a random interval
 31           try {
 32              Thread.sleep( (int) ( Math.random() * 3000 ) );
 33           }
 34           catch( InterruptedException e ) {
 35              System.err.println( e.toString() );
 36           }
 37  
 38           pHold.setSharedInt( count );
 39           System.out.println( "Producer set sharedInt to " +
 40                               count );
 41        }
 42  
 43        pHold.setMoreData( false );
 44     }
 45  }
 46  
 47  class ConsumeInteger extends Thread {
 48     private HoldInteger cHold;
 49  
 50     public ConsumeInteger( HoldInteger h )
 51     {
 52        cHold = h;
 53     }
 54  
 55     public void run()
 56     {
 57        int val;
 58  
 59        while ( cHold.hasMoreData() ) {
 60           // sleep for a random interval
 61           try {
 62              Thread.sleep( (int) ( Math.random() * 3000 ) );
 63           }
 64           catch( InterruptedException e ) {
 65              System.err.println( e.toString() );
 66           }
 67  
 68           val = cHold.getSharedInt();
 69           System.out.println( "Consumer retrieved " + val );
 70        } 
 71     }
 72  }
 73  
 74  class HoldInteger {
 75     private int sharedInt = -1;
 76     private boolean moreData = true;
 77     private boolean writeable = true;
 78  
 79     public synchronized void setSharedInt( int val )
 80     {
 81        while ( !writeable ) {
 82           try {
 83              wait();
 84           }
 85           catch ( InterruptedException e ) {
 86              System.err.println( "Exception: " + e.toString() );
 87           }
 88        }
 89  
 90        sharedInt = val;
 91        writeable = false;
 92        notify();
 93     }
 94  
 95     public synchronized int getSharedInt()
 96     {
 97        while ( writeable ) {
 98           try {
 99              wait();
100           }
101           catch ( InterruptedException e ) {
102              System.err.println( "Exception: " + e.toString() );
103           }
104        }
105  
106        writeable = true;
107        notify();
108        return sharedInt;
109     }
110  
111     public void setMoreData( boolean b ) { moreData = b; }
112  
113     public boolean hasMoreData() { return moreData; }
114  }