import java.util.*;

class Producer extends Thread {
    static final int MAXQUEUE = 5;
    private Vector messages = new Vector(  );

    public void run(  ) {
        try {
            while ( true ) {
                putMessage(  );
                sleep( 1000 );
            }
        }
        catch( InterruptedException e ) { }
    }

    private synchronized void putMessage(  )
      throws InterruptedException {

        while ( messages.size(  ) == MAXQUEUE ) {
            System.out.println("Producer queue full");
            wait(  );
//            System.out.println("post wait()");
		}

        messages.addElement( new java.util.Date().toString(  ) );

        notify(  );
    }

    // called by Consumer
    public synchronized String getMessage(  )
      throws InterruptedException {

		// not necessary if clause, but it's a better practice
//		if (messages.size() == 0)
  	      notifyAll(  );
//        	System.out.println("Post notify");

        while ( messages.size(  ) == 0 )
            wait(  );

        String message = (String)messages.firstElement(  );
        messages.removeElement( message );

        return message;
    }
}  // end of class Producer
