


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!



public class ProducerConsumerTest { public static void main(String[] args) { Plate c = new Plate(); Producer p1 = new Producer(c, 1); Consumer c1 = new Consumer(c, 1); p1.start(); c1.start(); } } class Plate { private int contents; private boolean available = false; public synchronized int get() { while (available == false) { try { wait(); } catch (InterruptedException e) { } } available = false; notifyAll(); return contents; }
public synchronized void put(int value) { while (available == true) { try { wait(); } catch (InterruptedException e) { } } contents = value; available = true; notifyAll(); } } class Consumer extends Thread { private Plate Plate; private int number; public Consumer(Plate c, int number) { Plate = c; this.number = number; } public void run() { int value = 0;
sleep((int)(Math.random() * 100)); } catch (InterruptedException e) { } } } }