import java.awt.*; import java.awt.image.*; import java.awt.event.*; import java.util.*; import java.applet.*; import java.net.*; import java.awt.Component.*; import java.awt.Color.*; import java.awt.Graphics; import java.util.Random; import java.lang.Math; import java.awt.event.WindowListener; import java.applet.AudioClip; // example book Java in a Nutshell by David Flanagan. // modified by Dr. Roger Webster public class simplethread extends Applet { Frame window; Button openbutton; Button closebutton; TextArea textarea = new TextArea(24, 80); public Mythread b; public Mythread c; public void init() { b = new Mythread("Thread 1"); b.start(); c = new Mythread("Thread 2"); c.start(); openbutton = new Button("KILL Thread1"); closebutton = new Button("KILL Thread2"); this.add(openbutton); this.add(closebutton); window = new Frame ("Webster's Sample Java Thread Window"); window.reshape (100, 100, 400, 400); textarea.setFont(new Font("Helvetica", Font.PLAIN, 12)); textarea.setEditable(false); window.add("Center", textarea); textarea.appendText("hello World Thread example\n"); textarea.appendText("this is the Applet \n"); window.show(); } public void stop() { window.dispose(); b.killme(); c.killme(); } // action - a button pressed. public boolean action(Event event, Object arg) { if (event.target == closebutton) { //window.hide(); c.killme(); } if (event.target == openbutton) { b.killme(); // window.show(); } return true; } // user presses a mouse button. public boolean mouseDown(Event e, int x, int y) { return true; } class Mythread extends Thread { long mytime; boolean keeprunning = true; public Mythread (String name) { super (name); }//end constructor public void killme() { keeprunning = false; } public void run() { while (keeprunning) { try { mytime = (long)(Math.random() * 1000); Thread.sleep(mytime); } catch (Exception e) { System.out.println("exception in thread sleep" + e.toString());} textarea.appendText("this is "+ this.getName()+" sleeptime is:" + mytime + "\n"); } }//end run }//end mythread }//end applet