Please send questions to st10@humboldt.edu .
/**
 * ThreadDemo3
 * 
 * a third demo for Threads;
 * this one modifies the old Spring 2000 CIS 235 ThreadDemo2.java,
 * making it avoid deprecated Thread methods.
 * It uses an AWT Applet and the Runnable interface, also,
 * but also adds a Stop button.
 *
 * RECOMMENDED SIZE: height 100, width 200 is fine.
 *
 * modified by: Sharon M. Tuttle
 * last modified: 3-12-01
 **/

import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;

public class ThreadDemo3 extends Applet implements Runnable, ActionListener
{
        // threads to be started up within this applet
        Thread  myThread1, myThread2, myThread3;

        Button  stopThreads;

        public void init()
        {
                setBackground(Color.lightGray);
                Label describe = new Label("ThreadDemo3: Version 1.0");
                add(describe);

                stopThreads = new Button("Stop Threads");
                add(stopThreads);
                stopThreads.addActionListener(this);
        }

        // creates and starts up my threads --- is executed every time the
        // user comes into the applet context after moving away?
        public void start()
        {
                // for each of the 3 threads not yet started,
                // start them up
                if (myThread1 == null)
                {
                        // ...start it up for this applet,
                        // naming this thread "My Thread #1"
                        myThread1 = new Thread(this, "My Thread #1");
                        myThread1.start();
                }

                if (myThread2 == null)
                {
                        myThread2 = new Thread(this, "My Thread #2");
                        myThread2.start();
                }

                if (myThread3 == null)
                {
                        myThread3 = new Thread(this, "My Thread #3");
                        myThread3.start();
                }
        }

        // run() is called by the system after thread has been started
        // by the system
        public void run()
        {
                Thread  executingThread;
                String  threadName = "";

                // this will help me to determine the currently-
                // executing thread, for display purposes;
                executingThread = Thread.currentThread();

                while ((myThread1 == executingThread)||
		       (myThread2 == executingThread)||
		       (myThread3 == executingThread))
                {
			System.out.println("This thread is: " +
				executingThread.getName());
                        try
                        {
                                // one second pause
                                Thread.sleep(1000);
                        }
                        catch(InterruptedException e)
                        {
                                // no action, but catch is required
                        }
                }
        }

        // suspends execution when the user leaves the page
	// (this is APPLET's stop method --- do not confuse
	// this with deprecated Thread.stop() method...)
        public void stop()
        {
                if (myThread1 != null)
                {
			// this is the preferred way to "stop" a
			// thread... set it to null?
			// (note that this *will* stop its
			// while loop in run()!)
			myThread1 = null;
		}
                if (myThread2 != null)
                {
                        // release system resources for that thread
                        myThread2 = null;
                }
                if (myThread3 != null)
                {
                        // release system resources for that thread
                        myThread3 = null;
                }
        }

        public void actionPerformed(ActionEvent e)
        {
                if (e.getActionCommand().equals("Stop Threads"))
                {
                        myThread1 = null;
                        myThread2 = null;
                        myThread3 = null;
                }
        }

}