Creating a repeating timer reminder in Java

后端 未结 4 1239
旧时难觅i
旧时难觅i 2021-01-11 18:17

I want to have a class that changes its own private variables every 2 seconds. I know that if I do something like

import java.util.Timer;
//...
Timer timer;         


        
4条回答
  •  别那么骄傲
    2021-01-11 18:50

    To be more precise here: ChangeSomething() is the constructor of your ChangeSomething class. The constructor will be executed immediately when you pass the ChangeSomething instace object to the timer, not after 2 seconds. It's that object's run() method will be triggered after 2 seconds.

    To execute that run() method repeatedly all 2 seconds, use schedule(TimerTask task, long delay, long period)

提交回复
热议问题