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

Lambda calculus, Type theory, Formal semantics, Program analysis

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

Applet must yield

If the applet does not yield, one of the W processes does not start before the monitor releases all threads.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.applet.*;

class M2 {
   boolean flag;
   TestFrame test;

   public M2 (boolean flag, TestFrame tst) { this.flag = flag; test = tst; }
   
   synchronized void saveit (int n) {
      test.text.append("Wait:"+n+"\n"); 
      try { wait(); } catch (Exception e) { }
      test.text.append("Release:"+n+"\n");
   }

   synchronized void doit () {
      test.text.append("Entering doit - notifying processes\n");
      if (flag) notify(); else notifyAll();
      test.text.append("Processes notified\n");
   }
}

class W2 extends Thread {
   JTextField text;
   M2 mp;
   int id;
   
   public W2 (M2 p, int n, JTextField t) { mp = p; id = n; text = t; }
   
   public void run () {
      mp.test.text.append("Starting "+id+"\n");
      mp.saveit(id);
      for (long i=0 ; i < 10000000 ; i++)
         if ((i%10000) == 0) text.setText(String.valueOf(i));
      mp.test.text.append("Terminating "+id+"\n\n");
   }
}

class Test3Doer extends Thread {
   TestFrame test;
   AuxFrame aux;

   public Test3Doer (TestFrame tst, AuxFrame ax) { test = tst; aux = ax; }

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

      try {
         if (test.choice.getSelectedItem().equals("applet yields")) {
            flag = true;
            test.text.append("\nApplet will yield -\n");
         } else {
            flag = false;
            test.text.append("\nApplet will not yield -\n");
         }

         M2 monitor = new M2(false,test);
         W2 w1 = new W2 (monitor,1,aux.w1t);
         W2 w2 = new W2 (monitor,2,aux.w2t);
         W2 w3 = new W2 (monitor,3,aux.w3t);
         W2 w4 = new W2 (monitor,4,aux.w4t);
         w1.start();
         w2.start();
         w3.start();
         w4.start();
         
         if (flag) yield();
         
         test.text.append("Go\n");
         monitor.doit();

      } catch (Exception e) {}
   }
}

class Test3bFrame extends TestFrame {
   AuxFrame aux;

   public Test3bFrame () {
      setLayout(new BorderLayout());
      JPanel p = new JPanel();
      p.setLayout(new GridLayout(1,2));
      p.add(choice);
      choice.addItem("applet yields");
      choice.addItem("applet does not yield");
      p.add(button);
      add("South",p);
      add("Center", new JScrollPane(text));
      aux = new AuxFrame(4);
      aux.setSize(170,170);
      aux.setVisible(true);
   }
   
   public void actionPerformed (ActionEvent evt) {
      (new Test3Doer(this,aux)).start();
   }
}

public class Test3b extends Applet {
   public void init () {
      TestFrame test = new Test3bFrame();
      test.setSize(340,700);
      test.setVisible(true);
   }
}