//file: NamedConsumer.java
public class NamedConsumer extends Thread {
Producer producer;
String name;
NamedConsumer(String name, Producer producer) {
this.producer = producer;
this.name = name;
}
public void run( ) {
try {
while ( true ) {
String message = producer.getMessage( );
System.out.println(name + " got message: " + message);
sleep( 2000 );
}
}
catch( InterruptedException e ) { }
}
public static void main(String args[]) {
Producer producer = new Producer( );
producer.start( );
// start two this time
new NamedConsumer( "One", producer ).start( );
new NamedConsumer( "Two", producer ).start( );
}
}
|