class Resource { int n; boolean val=false; synchronized int get() { while(!val) { try { wait(); } catch(Exception e) {} } System.out.println(“Consumer”+n); try { Thread.sleep(500); } catch(Exception e1) {} val=false; notify(); return n; } synchronized void put(int n) { while(val) { try { wait(); } catch(Exception e) {} } this.n=n; val=true; System.out.println(“producer”+n); try { Thread.sleep(500); } catch(Exception e1) {} notify(); } } class producer implements Runnable { Resource q; producer(Resource q) { this.q=q; new Thread(this,”Producer”).start(); } public void run() { int i=0; while(true) { q.put(i++); } } } class consumer implements Runnable { Resource q; consumer(Resource q) { this.q=q; new Thread(this,”Consumer”).start(); } public void run() { while(true) { q.get(); } } }