Solved

When will Thread stop in above Class

Posted on 2007-03-28
7
166 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
going to wrong jsp page 2 70
JAVA API design with micro service cloud in mind 1 113
JDBC performance 8 35
Bitwise and to sum elements 2 54
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…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
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.
Suggested Courses

751 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