class MyData
{
    private int datum;
    private boolean ready;

    MyData()
    { ready = false; }


    synchronized void put (int num)
    {
	while ( ready )
	    try {
		wait();
	    } catch (InterruptedException e) {}
	datum = num;
	ready = true;
	notify();

    }


    synchronized int get()
    {
	while( !ready )
	    try { wait(); 
	    } catch ( InterruptedException e ){}
	int num = datum;
	ready = false;
	notify();
	return num;
    }
	
} // end of my data

class Producer extends Thread
{
    MyData store;
    int id;

    Producer ( int i, MyData st )
    {
	store = st;
	id = i;
    }

    public void run ()
    {
	for ( int i=0; i<10; i++ ) {
	    store.put(i);
	    System.out.println("Producer: " + id + " : " + i );

	    try {
		Thread.sleep((int)(Math.random()*500 ));
	    } catch (InterruptedException e ) {}
	}
    }
}


class Consumer extends Thread
{
    MyData store;
    int id;

    Consumer ( int i, MyData st )
    {
	store = st;
	id = i;
    }

    public void run ()
    {
	int i = 0;
	while( i<10 ) {
	    i = store.get();
	    System.out.println("Consumer: " + id + " : " + i );

	    try {
		Thread.sleep((int)(Math.random()*500 ));
	    } catch (InterruptedException e ) {}
	}
    }
}


public class P_n_C
{

    public static void main( String []args )
    {
	MyData store = new MyData();
	new Producer( 1, store).start();
	new Producer( 2, store).start();
	new Consumer( 1, store).start();
	new Consumer( 2, store).start();
    }
}
		
	    

