1

I know this question is somewhat beaten but i haven't been able to get an answer to my specific problem.

I have several components that use TimerTask that i need to pause and resume afterwards.

What is the best way to do it? (Even if not with the Timer/TimerTask system)

Thanks in advance JQCorreia

JQCorreia
  • 727
  • 6
  • 15

2 Answers2

1

What are you trying to do? TimerTask is actually pretty old and has been part of java since 1.3. The newer preferred method is using executor service. Look at using ScheduledExecutorService. Here is another question with a similar answer Pausing/stopping and starting/resuming Java TimerTask continuously?

Community
  • 1
  • 1
Amir Raminfar
  • 33,777
  • 7
  • 93
  • 123
1

I have several components that use TimerTask that i need to pause and resume afterwards. What is the best way to do it?

Use Handler. With this you can post a Runnable to run using Handler.post(yourRunnable) and remove it by using Handler.removeCallbacks(yourRunnable). There are also methods in the API to make the Runnable execute after a specified delay.

Combine these two methods to implement a pause / resume function in your application.

Wroclai
  • 26,835
  • 7
  • 76
  • 67
  • Thanks m8! I've implemented a simple Timer using handler messages that executes a runnable. Simple clean and quite easy. – JQCorreia Apr 13 '11 at 16:41