• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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: 22783
131
Eclipse IDE Spring VI Editor 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic