FileDocCategorySizeDatePackage
Producer.javaAPI DocExample723Wed Feb 18 01:44:34 GMT 1998None

Producer.java

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;
    }
}