SuspendInterrupt.java


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

//: c14:SuspendInterrupt.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=SuspendInterrupt width=300 height=100>
// </applet>
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import com.bruceeckel.swing.*;

public class SuspendInterrupt extends JApplet {
   private JTextField txt = new JTextField(10);
   private JButton 
       suspendB = new JButton("Suspend"),
       resumeB = new JButton("Resume"),
       interruptB = new JButton("Interrupt");

   private int SuspendCount = 1;
   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 synchronized void funInterrupt() {
         interrupt();
         suspendableThread = null; // to release it
      }
      public void run() {
         while (true) {
            txt.setText("Count: " + Integer.toString(count++));
            synchronized (this) {
               if(suspendedFlag) {
                  txt.setText("Suspend # " +  SuspendCount++);
                  try{
                     wait();
                  } 
                  catch(InterruptedException e) {
                     System.err.println("wait Interrupted");
                  }
                  if (suspendedFlag){
                     System.err.println("wait:Interrupt Button");
                     break;
                  }
                  else System.err.println("wait: Resume Button");
               }
            }
            try{
               sleep(1000);
            } 
            catch(InterruptedException e) {
               System.err.println("sleep: Interrupt Button");
               break;
            }
         }
      }
   }
   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);

      interruptB.addActionListener( new ActionListener() {
         public 
             void actionPerformed(ActionEvent e) {
                suspendB.setEnabled(false);
                suspendB.setBackground(Color.red);
                resumeB.setEnabled(false);
                resumeB.setBackground(Color.red);
                interruptB.setEnabled(false);
                interruptB.setBackground(Color.red);
                txt.setText("Thread Interrupted");
                suspendableThread.funInterrupt();
         }
      }
      );
      cp.add(interruptB);
      interruptB.setEnabled(true);
      interruptB.setBackground(Color.green);

   }
   public static void main(String[] args) {
      Console.run(new SuspendInterrupt(), 300, 100);
   }
}