The Artima Developer Community
Sponsored Link

Java Answers Forum
Thread executes without stopping !

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Stan Dominski

Posts: 5
Nickname: stanley
Registered: Nov, 2002

Thread executes without stopping ! Posted: Nov 7, 2002 4:09 AM
Reply to this message Reply
Advertisement
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++)
{

System.out.print(firstName);
sleep(aWhile); // wait aWhile milliseconds
System.out.print(secondName + "\n");

if(i == 9)
value = false;
}

}
}
catch(InterruptedException e)
{
System.out.println(firstName + secondName + e);
}
}
}

Topic: Server Authentication, Cookie and Servlet Program Previous Topic   Next Topic Topic: Networking questions:

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use