SOURCE CODE
import java.applet.Applet;
import java.awt.*;
public class PrintTest extends Applet {
private TextArea output;
public void init()
{
output = new TextArea (28, 48);
add (output);
}
public void start ()
{
PrintThread thread1, thread2, thread3, thread4;
thread1 = new PrintThread ( "1" , output);
thread2 = new PrintThread ( "2" , output);
thread3 = new PrintThread ( "3" , output);
thread4 = new PrintThread ( "4" , output);
thread1.start();
thread2.start();
thread3.start();
thread4.start();
}
}
class PrintThread extends Thread
{
int sleepTime;
private TextArea out;
public PrintThread (String id, TextArea output)
// constructor assigns name to thread by calling Thread constructor
{
super (id);
out = output;
// sleep between 0 and 5 seconds
sleepTime = (int) (Math.random() * 5000);
output.appendText ("\n Name: " + getName() + "; sleep: " + sleepTime );
}
// execute the thread
public void run()
{
// put thread to sleep for a random interval
try
{ sleep( sleepTime ); }
catch ( InterruptedException exception )
{ System.err.println ("Exception: " + exception.toString() ); }
// print thread name
out.appendText ("\n Thread " + getName() );
}
}