Solved

When will Thread stop in above Class

Posted on 2007-03-28
7
163 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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 was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

861 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

23 Experts available now in Live!

Get 1:1 Help Now