Home arrow static arrow Java Programming [Archive] - Timer or Infiinte while loop
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - Timer or Infiinte while loop
This topic has 3 replies on 1 page.

Posts:451
Registered: 8/4/03
Timer or Infiinte while loop  
Jul 19, 2004 6:17 PM



 
I have a class that retrieves a file from a URL every 10 minutes. I am wondering if I should set it up to be a while loop that sleeps for 10 minutes and then does the update or if I should implement ActionListener and have a Timer do the work (the reason I suggest the latter is that in almost all the other classes I have I use a timer to do stuff like this, but they are all GUI components and since this one is not I can't keep the object alive with just a Timer so I need to put an infinite while loop in at the end but that seems redundant if I just did it the first way I mentioned). Please let me know what you think if you have expereince with both suggestions.
 

Posts:9,091
Registered: 8/15/01
Re: Timer or Infiinte while loop  
Jul 19, 2004 6:22 PM (reply 1 of 3)



 
Use a [url http://java.sun.com/j2se/1.5.0/docs/api/java/util/Timer.html]timer[/url]. It would be simpler than creating your own.
 

Posts:451
Registered: 8/4/03
Re: Timer or Infiinte while loop  
Jul 19, 2004 6:26 PM (reply 2 of 3)



 
I don't know about it being simpler, all I need to do to recreate it would be:
while(true){   //do something  sleep(TEN_MINUTES);}

With the Timer I have to extend ActionListener and implment ActionPerformed

What I am starting to think I will do is just have my class extend Thread and do the while(true){} loop. It seems much simpler.
 

Posts:2,821
Registered: 7/10/01
Re: Timer or Infiinte while loop  
Jul 19, 2004 7:18 PM (reply 3 of 3)



 
I would suggest using Timer also, its what its there for.
It would also be clearer as to its function when you or some1 else looks at the code at a later date,
also it gives a proper scalable scheduling system, so if you ever need to make your app. perform some other operation at a different interval, you won't need to code anything at all - just add another task to the Timer. (Timers perform all their tasks in a single Thread)
 
This topic has 3 replies on 1 page.