20-CS-4003-001 Organization of Programming Languages Fall 2017
Producer/Consumer

Lambda calculus, Type theory, Formal semantics, Program analysis

       Previous     Next           Code      TestFrame      Demo     All lectures

Implementing a Consumer-Producer Stream - separate monitor

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.applet.*;

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

   public synchronized int get() {
      if (available == false) {
         try { wait(); } 
         catch (InterruptedException e) { }
      }
      available = false;
      notify();
      return contents;
   }

   public synchronized void put(int value) {
      if (available == true) {
         try { wait(); } 
         catch (InterruptedException e) { }
      }
      contents = value;
      available = true;
      notify();
   }
}

class Producer extends Thread {
   private Monitor monitor;
   private int number;
   private TestFrame test;

   public Producer(Monitor c,int number,TestFrame tst) {
      monitor = c;
      this.number = number;
      test = tst;
   }

   public void run() {
      for (int i = 0; i < 10; i++) {
         monitor.put(i);
         test.text.append("Producer #" + this.number +
                          " put: " + i+"\n");
      }
   }
}

class Consumer extends Thread {
   private Monitor monitor;
   private int number;
   private TestFrame test;

   public Consumer(Monitor c,int number,TestFrame tst) {
      monitor = c;
      this.number = number;
      test = tst;
   }

   public void run() {
      int value = 0;
      for (int i = 0; i < 10; i++) {
         value = monitor.get();
         test.text.append("Consumer #" + this.number +
                          " got: " + value+"\n");
      }
   }
}

class Thread_1_Doer extends Thread {
   TestFrame f;

   public Thread_1_Doer (TestFrame f) { this.f = f; }

   public void run () {
      f.text.setText("");
      try { sleep(200); } catch (Exception e) { }

      Monitor monitor = new Monitor();
      Producer p1 = new Producer(monitor, 1, f);
      Consumer c1 = new Consumer(monitor, 1, f);
      p1.start();
      c1.start();
   }
}

class Thread_1Frame extends TestFrame {
   public Thread_1Frame () {
      setLayout(new BorderLayout());
      JPanel p = new JPanel();
      p.add(button);
      add("South",p);
      add("Center", new JScrollPane(text));
   }

   public void actionPerformed (ActionEvent evt) {
      (new Thread_1_Doer(this)).start();
   }
}

public class thread_1 
   xextends Applet implements ActionListener {
   JButton go;

   public void init () {
      setLayout(new BorderLayout());
      setBackground(new Color(255,255,223));
      add("Center", go = new JButton("Applet"));
      go.addActionListener(this);
   }

   public void actionPerformed (ActionEvent evt) {
      TestFrame test = new Thread_1Frame();
      test.setSize(350,700);
      test.setVisible(true);
   }
}
 -  A Producer object creates number tokens, from 1 to 10, and sends them to a Consumer object through the put method of a monitor. The Consumer object gets those tokens using the monitor's get method.