public class Producer extends Thread
1: //Producer.java
2: //Producer's run method controls a thread that
3: //stores values from 1 to 4 in sharedLocation.
4:
5: public class Producer extends Thread
6: {
7: private Buffer sharedLocation; // reference to shared object
8:
9: // constructor
10: public Producer( Buffer shared )
11: {
12: super( "Producer" );
13: sharedLocation = shared;
14: }
15:
16: // store values from 1 to 4 in sharedLocation
17: public void run()
18: {
19: for ( int count = 1; count <= 4; count++ )
20: {
21: // sleep 0 to 3 seconds, then place value in Buffer
22: try
23: {
24: Thread.sleep( ( int ) ( Math.random() * 3001 ) );
25: sharedLocation.set( count );
26: }
27:
28: // if sleeping thread interrupted, print stack trace
29: catch ( InterruptedException exception )
30: {
31: exception.printStackTrace();
32: }
33:
34: } // end for
35:
36: System.err.println( getName() + " done producing." +
37: "\nTerminating " + getName() + ".");
38:
39: } // end method run
40:
41: } // end class Producer
42: