Cancelling a TimerTask does not interrupt it's execution - it just prevents it from being executed again.
If you want the thread to stop executing you need to stash a reference to the java.lang.Thread somewhere and interrupt it.
Assuming that you're doing real work in the thread you also need to poll Thread.interrupted() to check if it should continue.
private Thread taskThread;
@Override
public void run() {
taskThread = Thread.currentThread();
System.out.println("going to sleep at: " + new Date());
doSomeWorks();
System.out.println("returned back at: " + new Date());
}
private void dosomeWorks() {
for (int i = 0; !Thread.interrupted() && i < 10; ++i) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
...
mTimer.cancel();
timerTask.taskThread.interrupt();
...
Be sure to read the javadoc for Thread.interrupt() to be sure you know it actually does.