import java.util.*;
import java.awt.*;
import java.io.*;


class ProduceInteger implements Runnable {
    private HoldInteger pHold;
    private Thread thread;
    public ProduceInteger(HoldInteger h) {
        pHold=h;
        thread = new Thread(this);
        thread.start();

    }
    public void run() {
        for(int count=0; count < 10; count++) {
            pHold.setSharedInt(count);
            System.out.println("Producer set sharedInt to " + count);

        try {
            thread.sleep((int)(Math.random()*3000));
        }
        catch(InterruptedException exception) {
            System.err.println("Exception " + exception.toString());
        }
        }

    }
}
class ConsumeInteger implements Runnable{
    private HoldInteger cHold;
    private Thread thread;
    public ConsumeInteger(HoldInteger h) {
        cHold = h;
        thread= new Thread(this);
        thread.start();
    }
    public void run() {
        int val;
        val= cHold.getSharedInt();
        System.out.println("Consumer retrieved " + val);
        while(val != 9) {
            try {
                thread.sleep((int)(Math.random()*3000));
            }
            catch(InterruptedException e) {
                System.err.println("Exception " + e.toString());
            }
            val = cHold.getSharedInt();
            System.out.println("Consumer retrieved " + val);
        }
    }
}
class HoldInteger {
    private int sharedInt;
    public void setSharedInt(int val) {sharedInt = val;}
    public int getSharedInt() { return sharedInt;}
}
public class SharedCell {
    public static void main(String[] args) {
        HoldInteger h= new HoldInteger();
        new ProduceInteger(h);
        new ConsumeInteger(h);
    }
}
