1
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 was supposed to print "IN" every second but it is only printing one time. Any tips? Thank you

What

MByD
  • 135,866
  • 28
  • 264
  • 277

1 Answers1

2

You're using the one-shot version of schedule. Simply use the overloaded version that accepts an interval period:

ttt.schedule(test, 0, 1000);

Aside: The newer ExecutorService is preferred over java.util.Timer. Timer has only one executing thread, so long-running task can delay other tasks. An ExecutorService can operate using a thread pool. Discussed more here

Community
  • 1
  • 1
Reimeus
  • 158,255
  • 15
  • 216
  • 276