Notify.java


</COMMENT> No Java 2 support for APPLET!!

//: c14:Notify.java
// From 'Thinking in Java, 2nd ed.' by Bruce Eckel
// www.BruceEckel.com. See copyright notice in CopyRight.txt.
// The alternative approach to using suspend()
// and resume(), which are deprecated in Java 2.
// <applet code=Notify width=300 height=100>
// </applet>
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import com.bruceeckel.swing.*;

public class Notify extends JApplet {
   private JTextField txt = new JTextField(20);
   private JButton 
       suspendB = new JButton("Suspend"),
       resumeB = new JButton("Resume");

   private Suspendable suspendableThread = new Suspendable();

   class Suspendable extends Thread {
      private int count = 0;
      private boolean suspendedFlag = false;
      public Suspendable() { 
         start(); 
      }

      public synchronized void funSuspend() {
         suspendedFlag = true;
      }

      public synchronized void funResume() {
         suspendedFlag = false;
         notify();
      }
      public void run() {
         while (true) {
            txt.setText("Count: " + Integer.toString(count++));
            synchronized (this) {
               if(suspendedFlag) {
                  try{
		     txt.setText("Suspend Button is ON: wait()"); 
                     wait();
                  } 
                  catch(InterruptedException e) {
                     System.err.println("wait Interrupted "+ e);
                  }
                  txt.setText("Resume Button is ON");
               }
            }// synch
            try{
               sleep(2000);
            } 
            catch(InterruptedException e) {
               System.err.println("sleep(): Interrupted " + e);
            }
         }//true
      }//run
   }//thread
   public void init() {
      Container cp = getContentPane();
      cp.setLayout(new FlowLayout());
      cp.add(txt);

      suspendB.addActionListener( new ActionListener() {
         public 
             void actionPerformed(ActionEvent e) {
                suspendableThread.funSuspend();
                suspendB.setEnabled(false);
                suspendB.setBackground(Color.red);
                resumeB.setEnabled(true);
                resumeB.setBackground(Color.green);
         }
      }
      );
      cp.add(suspendB);
      suspendB.setEnabled(true);
      suspendB.setBackground(Color.green);

      resumeB.addActionListener( new ActionListener() {
         public 
             void actionPerformed(ActionEvent e) {
                suspendableThread.funResume();
                suspendB.setEnabled(true);
                suspendB.setBackground(Color.green);
                resumeB.setEnabled(false);
                resumeB.setBackground(Color.red);
         }
      }
      );
      cp.add(resumeB);
      resumeB.setEnabled(false);
      resumeB.setBackground(Color.red);
   }
   public static void main(String[] args) {
      Console.run(new Notify(), 300, 100);
   }
}//JApplet