public class producer_consumer_wn {
    public static void main(String[] args) {
        Object the_monitor=new Object();
        The_Store_wn a_store=new The_Store_wn();
        Consumer_wn c1 = new Consumer_wn(a_store,the_monitor);
        Producer_wn p1 = new Producer_wn(a_store,the_monitor);
        if (args[0].equals("1") ) {
            c1.start();
            p1.start();
        } else {
            p1.start();
            c1.start();
        }
    }
}

class The_Store_wn {
    public   int the_count=10;
    public   int the_number=0;
    public The_Store_wn(){
    }
    public  int get() {
        return the_number;
    }

    public void put() {
        the_number++;
        return;
    }

}

class Consumer_wn extends Thread {
    The_Store_wn ts;
    Object the_mon;
    public Consumer_wn(The_Store_wn tsget,Object mon) {
        ts=tsget;
        the_mon=mon;
    }

    public void run() {
        synchronized(the_mon) {
            System.out.println("Consumer starts" );
            try {
                System.out.println("Consumer wait() " );
                the_mon.wait();
            } catch (InterruptedException e) {
            }


            while (ts.the_number != ts.the_count) {
                System.out.println("Consumer #" + ts.get());
                System.out.println("Consumer notify() " );
                the_mon.notify();
                try {
                    System.out.println("Consumer wait() " );
                    the_mon.wait();
                } catch (InterruptedException e) {
                }
            }
            System.out.println("Consumer #" + ts.get());
        }
    }

}

class Producer_wn extends Thread {
    The_Store_wn ts;
    Object the_mon;
    public Producer_wn(The_Store_wn ts_get,Object mon) {
        ts=ts_get;
        the_mon=mon;
    }

    public void run() {
        synchronized (the_mon){
            System.out.println("Producer starts" );
            while (ts.the_number != ts.the_count) {
                ts.put();
                System.out.println("Producer #" + ts.the_number);
                System.out.println("Producer notify() " );
                the_mon.notify();
  
                if (ts.the_number == ts.the_count) {
                    return;
                }
                try {
                System.out.println("Producer wait() " );
                    the_mon.wait();
                } catch (InterruptedException e) {
                }
            }
        }
    }
}