• Post Reply Bookmark Topic Watch Topic
  • New Topic

is TimerTask a reusable object?  RSS feed

 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a working program that has a Timer that gets its start date and time from a Date JSpinner, then fires a TimerTask to complete a task. It works perfectly well. As an experiment, I set up a second Date JSpinner with a date much later than the first to see if it would fire the same TimerTask again at the later date and time. It did not. Is a TimerTask ever reusable, or am I doing something wrong? Should the second Date JSpinner be associated with its own separate TimerTask? I could post the code, but it may be quicker to ask the question first in case I'm barking up the wrong tree.

Thank you.
 
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A Timer does not have a single Date that it uses to run a task. It is essentially a queue of tasks that each have their own dates to run, and it runs each task at the date you requested.

I don't know how you called the timer with the second date, so I have no idea whether you should expect it to run the task or not. As to whether TimerTasks are reusable, that will depend on your implementation which again I don't know.

Do note that TimerTasks are cancelable though, so if you cancelled the task it won't run again. For that reason I would suggest not submitting the same task multiple times.

Also Timer is very old. In future you may want to look at ScheduledThreadPoolExecutor which does the same kind of thing.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!