• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

why the repeated task does not work ?

 
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class TimerTest extends TimerTask {

public static void main (String[] args) {

TimerTask timerTest = new TimerTest();
Timer timer = new Timer(true);
Date now = new Date();
// let the job start from now with 5 seconds delay
timer.scheduleAtFixedRate(timerTest, now, 5*1000);
}

public void run() {
System.out.println("Running the task...");

}
}

However, in my IDE console I can only see ONE print out. I am supposed to see a print out every 5 seconds but I can't see any but the first.

Why ??
 
Sheriff
Posts: 22849
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because the Timer has been created to use a daemon thread. After that one print, the main method ends and therefore the entire prorgam ends.

If you turn that true into a false (or omit it completely) you'll see that it will continue printing.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic