• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

is TimerTask a reusable object?

Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
Posts: 689
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
He does not suffer fools gladly. But this tiny ad does:
the value of filler advertising in 2021
    Bookmark Topic Watch Topic
  • New Topic