20-CS-4003-001 Organization of Programming Languages Fall 2017
Notify, Wait

Lambda calculus, Type theory, Formal semantics, Program analysis

       Previous     Next           Test4a.java      TestFrame      AuxFrame.java      Sister code     All lectures

Cause N objects to wait for all W objects to finish

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

interface Process {  int getIdentity(); }

// Hashtable saves W objects that are running.  W objects leave
// hashtable when they finish.  When the hashtable is empty, all 
// N objects are released.
class M3 {
   TestFrame test;
   Hashtable hash = new Hashtable();
   
   M3 (TestFrame tst) { test = tst; }

   public void waitIt(Process p) {
      if (hash.get(p) != null) return; else hash.put(p,p);
   }
   
   public void endIt(Process p)   {
      if (hash.get(p) != null) hash.remove(p);
      if (hash.isEmpty()) doIt();
   }
   
   synchronized void saveIt (Process p) {
      test.text.append("Wait: ("+p.getIdentity()+")\n");
      try { wait(); } catch (Exception e) { }
      test.text.append("Release: ("+p.getIdentity()+")\n");
   }
   
   synchronized void doIt () { notifyAll(); }
}

class W3 extends Thread implements Process {
   JTextField text;
   M3 mp;
   int id;
   
   public W3 (M3 m, int n, JTextField t) { 
      mp = m; 
      id = n;
      test = t;
      text.setText("");
      m.waitIt(this); 
   }

   public int getIdentity() { return id; }
   
   public void run () {
      mp.test.text.append("Starting "+id+"\n");
      for (long i=0 ; i < 10000000 ; i++)
	 if ((i%10000) == 0) text.setText(String.valueOf(i));
      mp.test.text.append("Terminating "+id+"\n");
      mp.endIt(this);
   }
}

class N3 extends Thread implements Process {
   JTextField text;
   M3 mp;
   int id;
   
   public N3 (M3 m, int n, JTextField t) { 
      mp = m; 
      id = n;
      text = t;
      text.setText("");
   }

   public int getIdentity() { return id; }
   
   public void run () {
      mp.test.text.append("Starting "+id+"\n");
      mp.saveIt(this);
      for (long i=0 ; i < 10000000 ; i++)
	 if ((i%10000) == 0) text.setText(String.valueOf(i));
      mp.test.text.append("\nTerminating "+id+"\n");
   }
}

class Test4aFrame extends TestFrame {
   AuxFrame aux;

   public Test4aFrame () {
      setLayout(new BorderLayout());
      JPanel p = new JPanel();
      p.add(button);
      add("South", p);
      add("Center", new JScrollPane(text));
      aux = new AuxFrame(7);
      aux.setSize(170,300);
      aux.setVisible(true);
   }
   
   public void actionPerformed (ActionEvent evt) {
      text.setText("");
      try { Thread.sleep(200); } catch (Exception e) { }

      M3 monitor = new M3(this);
      W3 w1 = new W3 (monitor,1,aux.w1t);
      W3 w2 = new W3 (monitor,2,aux.w2t);
      W3 w3 = new W3 (monitor,3,aux.w3t);
      W3 w4 = new W3 (monitor,4,aux.w4t);
      N3 n1 = new N3 (monitor,5,aux.n1t);
      N3 n2 = new N3 (monitor,6,aux.n2t);
      N3 n3 = new N3 (monitor,7,aux.n3t);

      n1.start();
      w1.start();
      w2.start();
      n2.start();
      w3.start();
      w4.start();
      n3.start();
      
      text.append("\n------\nGo\n-------\n");
   }
}

public class Test4a extends Applet {
   public void init () {
      TestFrame test = new Test4aFrame();
      test.setSize(300,700);
      test.setVisible(true);
   }
}