Please send questions to
st10@humboldt.edu .
/**
* ThreadDemo2
*
* a second demo for Threads;
* this one modifies the old Spring 2000 CIS 235 ThreadDemo1.java,
* making it avoid deprecated Thread methods.
* It uses an AWT Applet and the Runnable interface.
*
* 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.*;
public class ThreadDemo2 extends Applet implements Runnable
{
// threads to be started up within this applet
Thread myThread1, myThread2, myThread3;
public void init()
{
setBackground(Color.lightGray);
Label describe = new Label("ThreadDemo2: Version 1.0");
add(describe);
}
// 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;
// this will help me to determine the currently-
// executing thread, for display purposes;
executingThread = Thread.currentThread();
for (int i=0; i<10; i++)
{
System.out.println("This thread is: " +
executingThread.getName());
try
{
// one second pause
Thread.sleep(1000);
}
catch(InterruptedException e)
{
// no action, but catch is required
}
}
}
}