• 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 ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Paul Clapham
  • paul wheaton
Sheriffs:
  • Tim Cooke
  • Devaka Cooray
  • Rob Spoor
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:

TimerTask, Thread

 
Ranch Hand
Posts: 802
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
is it possible to make a Thread and or two TimerTasks run parralel?

or at the same time I mean...

because from what ive done, a thread pauses the whole application, then

continues on..

but a Timer object calls a Timertask and either does the task once after a

pause, or only does one task over and over again on a regular interval...

plz help,
Justin
 
Justin Fox
Ranch Hand
Posts: 802
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
help?!
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Whenever you create a Timer, it creates and starts a single thread of its own, which it uses to fun all the tasks you schedule for it, in order, at the times you schedule (or as soon as possible thereafter). A single Timer should never run its scheduled TimerTasks in parallel - but if you had two different Timers, they could run their TimerTasks in parallel. Does that help?
 
Justin Fox
Ranch Hand
Posts: 802
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok but if you make to different TimerTasks, how do you define each of their run() methods, in the same class?...

because i dont want them to do the same thing..

thx,
Justin
 
Justin Fox
Ranch Hand
Posts: 802
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i tried to create an inner class that extends TimerTask,

and just redefine the run() methond in that class,

and then in my outer class just instatiate a new timertask through

that inner class, but i get static errors....
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Show some code where you define the inner class. That whole "inner class can access variables in the enclosing class" business can be confusing. You can almost always avoid it by defining the TimerTask in its own top level class and source file.
 
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Whenever you create a Timer, it creates and starts a single thread of its own, which it uses to fun all the tasks you schedule for it, in order, at the times you schedule (or as soon as possible thereafter). A single Timer should never run its scheduled TimerTasks in parallel - but if you had two different Timers, they could run their TimerTasks in parallel.



does this mean that I can create an application that uses Timers for different objects without having to put my Timers in their own threads?

say for example I have a calulator program that I can only use for a fixed amount of time, one I start punching a key the timer automatically starts, I can continue using the calculator while the Timer runs on it's own thread without putting it in a new thread?
 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes.
 
Megs Maquito
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks so much Jim, boy that makes my coding a lot easier
 
Do the next thing next. That’s a pretty good rule. Read the tiny ad, that’s a pretty good rule, too.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic