I schedule a timertask using timer and in that progress I use Thread.sleep( 10*60000) -> Sleep for ten minutes. And few times I need to cancel the running timertask and schedule a new one, as my timer task is in Thread.sleep for ten minutes I am unable to cancel the sleeping thread in timetask using .cancel(). Please something help me :(
Asked
Active
Viewed 313 times
0
-
what is that you are doing in the timer task? – Raghunandan Oct 17 '13 at 15:55
-
@Raghunandan - I schedule timer task and run httpconnection and after running the httpupdate task I use Thread.sleep for ten minutes and then repeat it.. so in between if I have an immediate update then I should come out of Thread.sleep ( cancel the timertask) and schedule a new one.. – user1810931 Oct 17 '13 at 16:00
-
Try to interrupt current thread... via thread.currentThread().interrupt(); – Harshit Rathi Oct 17 '13 at 17:05
-
@HarshitRathi- tried using .interrupt(), it did not work. – user1810931 Oct 17 '13 at 17:31
-
@user1810931 Have you considered using [AlarmManager](http://developer.android.com/reference/android/app/AlarmManager.html), which is the recommended method for scheduling long-running timers? Alarms are easy to cancel and allow your user to leave the app without affecting the timers. Managing them is easier, too, since you don't need to deal with threads. – acj Oct 18 '13 at 18:18