20-CS-694 Advanced Programming Techniques Spring 2012
Producer/Consumer

Interfaces, Exceptions, Graphics, Animation, Threads, Reflection, Networking, RMI, JDBC, JNI

       Previous     Next           Code      TestFrame      AuxFrame     All lectures

Threads Can Be Tricky!

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);
   }
}

// The control portion of the applet is not on a thread
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("");
      aux.init();
      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 thread_5 extends Applet implements ActionListener {
	JButton go;
	
   public void init () {
		setLayout(new BorderLayout());
		add("Center", go = new JButton("Applet"));
		go.addActionListener(this);
	}
	
	public void actionPerformed (ActionEvent evt) {
      TestFrame test = new Test9aFrame();
      test.setSize(320,700);
      test.setVisible(true);
   }
}
 -  Example showing how tricky Threads can be. Printing is directed to standard out as well as a JTextArea but in the sister applet (previous slide) the applet works great but in this slide nothing happens (although standard output is seen). The difference is that the control portion of the code is Threaded in the previous applet but not in this applet.