java - TimerTask() not doing what was supposed to, only printing one time -


public static void main(string[] args) {          timer ttt = new timer();         timertask test = new timertask() {              @override             public void run() {                  system.out.println("in");                         }                  };          ttt.schedule(test, 1000); } 

this supposed print "in" every second printing 1 time. tips? thank you

what

you're using one-shot version of schedule. use overloaded version accepts interval period:

ttt.schedule(test, 0, 1000); 

aside: newer executorservice preferred on java.util.timer. timer has 1 executing thread, long-running task can delay other tasks. executorservice can operate using thread pool. discussed more here


Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -