Instead of calling timer.cancel()
, you should be canceling the task that is assigned to that timer (doAsynchronousTask
in your case). Since multiple TimerTasks can be assigned to one timer, calling timer.cancel()
will not interfere with a currently running task.
From the Timer JavaDoc:
public void cancel()
Terminates this timer, discarding any currently scheduled tasks. Does not interfere with a currently executing task (if it exists). Once a timer has been terminated, its execution thread terminates gracefully, and no more tasks may be scheduled on it.