Dear People, In doing a program to learn about threads, the progam goes without stopping. I had to insert a for loop to halt the thread., otherwise the program can only be stopped by exiting JBuilder7.
The program doesn't indicate the value of "delay" or 'aWhile' which should be a number of milliseconds but there is no actual value of the 'delay' or 'aWhile shown.
below is the program Thank you in advance Stan
package stan_ch15p603;
import java.io.IOException;
public class TryThread extends Thread { private String firstName; private String secondName; private long aWhile; //delay in milliseconds public TryThread(String firstName, String secondName, long delay) { this.firstName = firstName; this.secondName = secondName; aWhile = delay; setDaemon(true); //Thread is daemon }
public static void main(String[] args) { //create 3 threads Thread firstThread = new TryThread(" Hopalong ", " Cassidy ", 200L); Thread secondThread = new TryThread(" Mary " , "Morris ", 300L); Thread thirdThread = new TryThread(" Slim ", " Pickins ", 500L);
System.out.println("Press enter when you have had enough....\n"); firstThread.start(); secondThread.start(); thirdThread.start();
try { System.in.read(); //this pauses the program until 'enter' is pressed System.out.println("Enter pressed...\n");
//interrupt the threads firstThread.interrupt(); secondThread.interrupt(); thirdThread.interrupt(); }
catch(IOException e) { System.out.println(e); }
System.out.println("Ending main()"); return;
}
//method where thread execution will start public void run() { try { boolean value = true ; while(value) { for (int i = 0 ; i < 10; i++) {