Solved

When will Thread stop in above Class

Posted on 2007-03-28
7
162 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:mayankeagle
ID: 18813659
>> Exited usind cntl+C ( Did the thread stop running)???

Yes that is when it will stop.
0
 
LVL 30

Expert Comment

by:mayankeagle
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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 30

Accepted Solution

by:
mayankeagle 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:mayankeagle
ID: 18821197
Does it print Thailand 3 times and then just terminate?
0
 
LVL 30

Expert Comment

by:mayankeagle
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

759 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now