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

Lambda calculus, Type theory, Formal semantics, Program analysis

       Previous     Next           Code      TestFrame     All lectures

Abstract Stream class subclassed by producers

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

abstract class Stream extends Thread {
   TestFrame test;
   int value;

   synchronized public void putIt (int t) {
      value = t;
      notify();

      if (test != null)
         test.out.append("PutIt ("+value+"): "+
                    Thread.currentThread()+"\n");

      try { wait(); } catch (Exception e) {  }
   }

   synchronized public int next () {
      if (this.getState() == Thread.State.NEW) 
         start(); 
      else 
         notify();

      try { wait(); } catch (Exception e) {  }

      return value;
   }
}

class Successor extends Stream {
   int number = 0;

   public Successor (TestFrame test) { 
      this.test = test; 
   }

   public void run () {  
      while (true) { 
         putIt(number++); 
      } 
   }
}

class Times extends Stream {
   Stream stream;

   Times (Stream s, TestFrame test) { 
      stream = s;  
      this.test = test; 
   }

   public void run () { 
      while (true) { 
         putIt(stream.next()*3); 
      } 
   }
}

class Thread_2Frame extends TestFrame {
   Stream stream = null;

   public Thread_2Frame () {
      setLayout(new BorderLayout());
      JPanel p = new JPanel();
      p.setLayout(new FlowLayout());
      p.add(button);
      button.setText("Connect");
      p.add(next);
      add("South",p);
      p = new JPanel();
      p.setLayout(new GridLayout(2,1));
      p.add(new JScrollPane(text));
      p.add(new JScrollPane(out));
      add("Center",p);
   }

   public void actionPerformed (ActionEvent evt) {
      if (evt.getSource() == button) {
         text.setText("");
         stream = new Times(new Successor(this),this);
         text.append("Stream connection opened\n");
         stream.next();
      } else if (stream == null) {
         out.append("You must connect first -"+
                    "press start\n");
      } else {
         text.append("Next token:  "+
                     stream.next()+"\n");
      }
   }
}

public class thread_2 
   extends 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_2Frame();
      test.setSize(700,600);
      test.setVisible(true);
   }
}
 -  The Monitor class of the previous slide is replaced by an abstract Stream class that provides the same service except that the methods providing the service are called putIt and next and the run method is implemented by a Producer object from a class that subclasses the Stream class. In this case the Producer classes are Sucessor and Times.