From reading on Stack Overflow I’ve seen that many of you don’t recommend using Timer Task. Hmmm… but I already implemented this:
I have this code:
detectionHandlerTimer.schedule(myTimerTask, 60 * 1000, 60 * 1000);
The thing is that work of myTimerTask lasts some time.
I would like this behavior:
- wait 60 sec.
- do task for some time (e.g. 40 – 100 sec).
- task finished.
- wait 60 seconds.
- do task for some time (e.g. 40 – 100 sec).
But the code above behaves like this
- wait 60 sec.
- do task for some time (e.g. 40 – 100 sec).
- task finished
- do task for some time (e.g. 40 – 100 sec).
Because the time duration of task is bigger than 60, timer starts task immediately after task is finished. But I would like it to wait again.
This works. The key is to have the task itself (after it completes) schedule the next occurrence of the task.
Which prints:
Here’s what it looks like if you extract the second components of the timestamps (for clarity):
Just replace the
10s with60s.