import java.util.Vector;
class Producer extends Thread {
private Vector messages = new Vector();
static int MAXQUEUE = 5;
public void run() {
try {
while ( true ) {
putMessage();
sleep( 1000 );
}
} catch( InterruptedException e ) { }
}
private synchronized void putMessage() throws InterruptedException {
while ( messages.size() == MAXQUEUE )
wait();
messages.addElement( new java.util.Date().toString() );
notifyAll();
}
public synchronized String getMessage() throws InterruptedException {
notifyAll();
while ( messages.size() == 0 )
wait();
String message = (String)messages.firstElement();
messages.removeElement( message );
return message;
}
}
|