Solved

When will Thread stop in above Class

Posted on 2007-03-28
7
164 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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Suggested Solutions

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This video teaches viewers about errors in exception handling.

770 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