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

Use the interrupt method to wake up threads

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

class N1 extends Thread {
   M1 monitor;
   int numb;

   public N1 (int n, M1 mn) {  
      monitor = mn;  
      numb = n;  
   }

   public void run () {
      monitor.test.text.append("N1: Starting ("+
         numb+")\n");
      monitor.b();
      monitor.test.text.append("N1: Finishing ("+
         numb+")\n");
   }
}

class M1 {
   boolean flag;
   TestFrame test;

   public M1 (boolean flag, TestFrame tst) {  
      this.flag = flag; 
      test = tst; 
   }

   synchronized public void a () {
      test.text.append("a: wait ("+
         ((W1)Thread.currentThread()).numb+")\n");
      try { wait(); } catch (Exception e) { }
      test.text.append("a: rise ("+
         ((W1)Thread.currentThread()).numb+")\n");
   }

   synchronized public void b () {
      test.text.append("b: notify ("+
         ((N1)Thread.currentThread()).numb+")\n");
      if (flag) notify(); else notifyAll();
      test.text.append("b: done ("+
         ((N1)Thread.currentThread()).numb+")\n");
   }
}

class W1 extends Thread {
   M1 monitor;
   int numb;

   public W1 (int n, M1 mn) { 
      monitor = mn;  
      numb = n; 
   }

   public void run () {
      monitor.test.text.append("W1: Starting ("+
         numb+")\n");
      monitor.a();
      monitor.test.text.append("W1: Finishing ("+
         numb+")\n");
   }
}

class Thread_3_Doer extends Thread {
   TestFrame f;

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

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

      M1 m1 = new M1 (false, f);  // Use notifyAll()
      M1 m2 = new M1 (false, f);  // Use notifyAll()
      W1 w1 = new W1 (1, m1);
      W1 w2 = new W1 (2, m2);
      W1 w3 = new W1 (3, m1);
      w1.start();
      w2.start();
      w3.start();

      try { sleep(1000); } catch (Exception e) {}

      w1.interrupt();  // Get a waiting thread to go
      f.text.append("\n");
   }
}

class Thread_3Frame extends TestFrame {
   public Thread_3Frame () {
      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_3_Doer(this)).start();
   }
}

public class thread_3 
   extends Applet implements ActionListener {
   JButton go;

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

   public void actionPerformed (ActionEvent evt) {
      TestFrame test = new Thread_3Frame();
      test.setSize(400,600);
      test.setVisible(true);
   }
}
 -