I have to repeat a part of my code every 2 seconds how could I do that? don't tell me to use
try {
Thread.sleep(millisecondi);
}
catch (Exception e) {}
because freeze the application
I have to repeat a part of my code every 2 seconds how could I do that? don't tell me to use
try {
Thread.sleep(millisecondi);
}
catch (Exception e) {}
because freeze the application
If your application is to stay responsive you need to do this in another thread. Or you could simply create a timer and schedule it.
Whatever thread you're in when you tell it to sleep - will impeccably do so...
Something like this:
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);