• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

TimerTask, Thread

 
Justin Fox
Ranch Hand
Posts: 802
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
help?!
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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....
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • 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.
 
Megs Maquito
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes.
 
Megs Maquito
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks so much Jim, boy that makes my coding a lot easier
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic