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
  • 133,244
  • 25
  • 260
  • 270

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
  • 155,977
  • 14
  • 207
  • 269