Back to slide -- Keyboard shortcut: 'u'  next  next  ProducerConsumerTest.java - En forbedret CubbyHole klassen.Lecture 14 - slide 35 : 41
Program 6

class CubbyHole {
    private int contents;
    private boolean available = false;

    public synchronized int get() {
        while (!available) {
            try {
                wait();
            } catch (InterruptedException e) { }
        } // while
        available = false;
        System.out.println("Consumer got: " + contents);

        notifyAll();
        return contents;
    }

    public synchronized void put(int value) {
        while (available) {
            try {
                wait();
            } catch (InterruptedException e) { }
        } // while
        contents = value;
        available = true;
        System.out.println("Producer put: " + value);
        notifyAll();
    }
} // end CubbyHole