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

Lambda calculus, Type theory, Formal semantics, Program analysis

       Previous     Next           Test9a.java      TestFrame      Sister code     All lectures

Threads Can Be Tricky!

Example showing how tricky Threads can be. Printing is directed to standard out as well as a JTextArea but in this applet nothing appears in the JTextArea. In the sister applet (Test9b.java) printing to the JTextArea works. The difference is the control portion of the code is Threaded in the sister applet but not this one.
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.applet.*;

class M4a {
   TestFrame test;

   public M4a (TestFrame tst) {  test = tst; }

   synchronized void doit () {
      test.text.append("Entering doit - notifying processes\n");
      System.out.println("Entering doit - notifying processes");
      notifyAll();
      test.text.append("Processes notified\n");
      System.out.println("Processes notified");
      try { 
	 test.text.append("Doit waiting\n"); 
	 System.out.println("Doit waiting"); 
	 wait(); 
      } 
      catch (Exception e) { }
   }
   
   synchronized void saveit (int n) {
      try { 
	 test.text.append("Wait:"+n+"\n"); 
	 System.out.println("Wait:"+n); 
	 wait(); 
      } 
      catch (Exception e) { }
      test.text.append("Release:"+n+"\n");
      System.out.println("Release:"+n);
   }
}

class W4a extends Thread {
   JTextField text;
   M4a mp;
   int id;
   
   public W4a (M4a p, int n, JTextField t) { mp = p; id = n; text = t; }

   public void run () {
      mp.test.text.append("Starting "+id+"\n");
      System.out.println("Starting "+id);
      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");
      System.out.println("Terminating "+id);
   }
}

class Test9aFrame extends TestFrame implements ActionListener {
   AuxFrame aux;

   public Test9aFrame () {
      setLayout(new BorderLayout());
      add("Center", new JScrollPane(text));
      JPanel p = new JPanel();
      p.setLayout(new FlowLayout());
      p.add(button);
      add("South", p);
      aux = new AuxFrame(4);
      aux.setSize(170,170);
      aux.setVisible(true);
   }

   public void actionPerformed (ActionEvent evt) { 
      text.setText("");
      try { Thread.sleep(200); } catch (Exception e) { }

      M4a m = new M4a(this);
      W4a w1 = new W4a(m,1,aux.w1t);
      W4a w2 = new W4a(m,2,aux.w2t);
      W4a w3 = new W4a(m,3,aux.w3t);
      W4a w4 = new W4a(m,4,aux.w4t);  
      w1.start();
      w2.start();
      w3.start();
      w4.start();

      text.append("Go\n");
      try { Thread.sleep(2000); } catch (Exception e) { }
      text.append("Go again\n");

      m.doit();
   }
}

public class Test9a extends Applet {
   public void init () {
      TestFrame test = new Test9aFrame();
      test.setSize(320,700);
      test.setVisible(true);
   }
}