aspose file tools*
The moose likes Beginning Java and the fly likes TimerTask, Thread Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "TimerTask, Thread" Watch "TimerTask, Thread" New topic
Author

TimerTask, Thread

Justin Fox
Ranch Hand

Joined: Jan 24, 2006
Posts: 802
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


You down with OOP? Yeah you know me!
Justin Fox
Ranch Hand

Joined: Jan 24, 2006
Posts: 802
help?!
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
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?


"I'm not back." - Bill Harding, Twister
Justin Fox
Ranch Hand

Joined: Jan 24, 2006
Posts: 802
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

Joined: Jan 24, 2006
Posts: 802
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

Joined: Jan 29, 2003
Posts: 8791
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.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Megs Maquito
Ranch Hand

Joined: May 18, 2005
Posts: 84
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?


I'm a Hood Ornament
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Yes.
Megs Maquito
Ranch Hand

Joined: May 18, 2005
Posts: 84
thanks so much Jim, boy that makes my coding a lot easier
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: TimerTask, Thread