Repeat action every 2 seconds in java
1 answer
If your application will remain responsive, you need to do it on a different thread. Or you can just create timer and schedule .
Whichever stream you are in when you tell it to sleep will do it flawlessly ...
Something like that:
Timer timer = new Timer();
TimerTask myTask = new TimerTask() {
@Override
public void run() {
// whatever you need to do every 2 seconds
}
};
timer.schedule(myTask, 2000, 2000);
+12
source to share