Solved

When will Thread stop in above Class

Posted on 2007-03-28
7
165 Views
Last Modified: 2013-11-23
Below Threads keep executing until when??

public class RetriveWeatherData extends Thread{
      int sleepTime = 15*60*1000;
      String localweather;
      
      public void run()
      {
            while (true)
            {
                  Weather tw=new Weather();
                         loaclweather=tw.fillLocalWeather(tw.getCity(),tw.getCountry());
                  try {
                        Thread.sleep(sleepTime);
                        
                  } catch (InterruptedException e) {
                        e.printStackTrace();
                  }
            }
      }
      
}


I run above using
c:\>javac RetrieveweatherData

c:\> Exited usind cntl+C ( Did the thread stop running)???
0
Comment
Question by:cutie_smily
  • 5
  • 2
7 Comments
 
LVL 30

Expert Comment

by:Mayank S
ID: 18813659
>> Exited usind cntl+C ( Did the thread stop running)???

Yes that is when it will stop.
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 18813664
You can put debug statements to verify:

            while (true)
            {
                  Weather tw=new Weather();
                         loaclweather=tw.fillLocalWeather(tw.getCity(),tw.getCountry());
                  try {
                        System.out.println ( "Sleeping" ) ;
                        Thread.sleep(sleepTime);
                       
                  } catch (InterruptedException e) {
                        e.printStackTrace();
                  }
            }
0
 

Author Comment

by:cutie_smily
ID: 18817445
Exited usind cntl+C ( Did the thread stop running)???
Yes that is when it will stop.

When i run the class it will start . Am i right??

 try {
                        System.out.println ( "Sleeping" ) ;
                        Thread.sleep(sleepTime);
                       
                  }

In the above debug statement, Thread is sleeping its not killed. When i do cntl+c does that mean we killed the thread?
0
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
LVL 30

Accepted Solution

by:
Mayank S earned 125 total points
ID: 18819144
>> When i run the class it will start . Am i right??

Yes, it will start.

>> In the above debug statement, Thread is sleeping its not killed. When i do cntl+c does that mean we killed the thread?

Yes the JVM will terminate all threads while exiting. The debug statement was given to verify:

If you get an output like:

Sleeping
Sleeping (this will come after sleepTime milliseconds, so you need to wait)
Sleeping (this will come after another sleepTime milliseconds, so you need to wait)

Now press Ctrl C - the Sleeping output stops coming and won't come after sleepTime milliseconds. This means that the thread was terminated.
0
 

Author Comment

by:cutie_smily
ID: 18820510
Hey Can you look at this class and i am not sure why Bangkok, Thailand outputting thrice

public class RetriveWeatherData extends Thread{
      int sleepTime = 15*60*1000;
      String weather;
      
      public void run()
      {
            
            String[] cities=new String[6];
            String[] country=new String[6];
            
            
            cities[0]= "Bangkok";
            cities[1]= "Bangalore";
            cities[2]= "Beijing";
            cities[3]= "Ho%20Chi%20Minh%20City";
            cities[4]= "Hong%20Kong";
            cities[5]= "Jakarta";
            
            country[0]= "Thailand";
            country[1]= "India";
            country[2]= "China";
            country[3]= "Vietnam";
            country[4]= "China";
            country[5]= "Indonesia";
            int i=0;
            while (true)
            {
                  WunderWorld tw=new WunderWorld();            
                  weather=tw.fillLocalWeather(cities[i],country[i]);
                  System.out.println("Weather is"+cities[i]+","+country[i]+"\n"+weather);
                  i++;
                  if(i>5)
                        i=0;
                  try {
                        Thread.sleep(sleepTime);
                        
                  } catch (InterruptedException e) {
                        e.printStackTrace();
                  }
            }
      }
      
      public static void main(String args[])
      {
            RetriveWeatherData rw=new RetriveWeatherData();
            rw.start();
      }
}


result Actual Url-->http://www.wunderground.com/cgi-bin/findweather/getForecast?query=B
angkok%2CThailand
1.............
1.............
1.2.............
2.............
3.............
1.2.............
2.............
3.............
****Mandarin Weather*******
1...Url Result
Iam out of while loop in getCurrentIntlWeather()-String token whiile
1>Weather for Bangkok, TH

---->Weather for Bangkok, TH
Currently:82F / 28C(Scattered Clouds)
Humidity: 79%
28C Scattered Clouds
Dew Point: 75°F
/ 32C / 6.2miles

In filHashTable()-->Weather for Bangkok, TH
Currently:82F / 28C(Scattered Clouds)
Humidity: 79%
28C Scattered Clouds
Dew Point: 75°F
/ 32C / 6.2miles

Weather isBangkok,Thailand
Weather for Bangkok, TH
Currently:82F / 28C(Scattered Clouds)
Humidity: 79%
28C Scattered Clouds
Dew Point: 75°F
/ 32C / 6.2miles

****Mandarin Weather*******
1...Url Result
Iam out of while loop in getCurrentIntlWeather()-String token whiile
1>Weather for Bangkok, TH

---->Weather for Bangkok, TH
Currently:82F / 28C(Scattered Clouds)
Humidity: 79%
28C Scattered Clouds
Dew Point: 75°F
/ 32C / 6.2miles

In filHashTable()-->Weather for Bangkok, TH
Currently:82F / 28C(Scattered Clouds)
Humidity: 79%
28C Scattered Clouds
Dew Point: 75°F
/ 32C / 6.2miles

Weather isBangkok,Thailand
Weather for Bangkok, TH
Currently:82F / 28C(Scattered Clouds)
Humidity: 79%
28C Scattered Clouds
Dew Point: 75°F
/ 32C / 6.2miles


Here thread sleeping waiting ...then it displays other cities after some time
fill method grabs data from the web. has not threads there.Why is it outputting ity 3 times
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 18821197
Does it print Thailand 3 times and then just terminate?
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 18832625
Any reason for the B grade :( ? You could've responded to my comment earlier and we could've taken it forward....
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
map related example 6 55
servlet web applications   metadata-complete="true" or false 3 131
maven disable workspace resolution 1 24
collection output issue 9 39
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question