Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Regular Contributor
shafich
Posts: 74
Registered: ‎01-12-2012
My Device: Developer
Accepted Solution

Call a Thread after a precise interval of time ???

Hello, All help is appreciated...

I am using this to call my thread each interval of time ...but I noticed that if my application goes to background the timertask will not do the calls on the specified interval... somtimes its late a minute || 5minutes || 10 minutes ...
Is there a better way to execute this.
This is the code:
 TimerTask _tt ;
_Timer Timer; 

_tt = new TimerTask() {
public void run() {

new FlightCheckThread(_sqlmanager).run();
} // end run
};
_Timer.scheduleAtFixedRate(_tt, 180000, 300000);

Developer
simon_hain
Posts: 16,347
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport

Re: Call a Thread after a precise interval of time ???

run is not the correct method to start a thread, it is start().
as the documentation of timertask notes it is not meant for blocking tasks, using start() should resolve the timing issue.
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Regular Contributor
shafich
Posts: 74
Registered: ‎01-12-2012
My Device: Developer

Re: Call a Thread after a precise interval of time ???

Ok... Thank you ... ! 
This should be helpful.  

Developer
rajani_dhawan
Posts: 55
Registered: ‎05-30-2011
My Device: BlackBerry torch 9800

Re: Call a Thread after a precise interval of time ???

[ Edited ]

@ simon :

 

Timer class is itself a kind of thread....and can Schedule the TimerTask for repeated fixed-delay execution. So in (timer task) this  code... 

_tt = new TimerTask() {
public void run() {

new FlightCheckThread(_sqlmanager).run();
new FlightCheckThread(_sqlmanager).start();

} // end run
};

 

whts  the need of start a new thread..???

Developer
simon_hain
Posts: 16,347
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport

Re: Call a Thread after a precise interval of time ???

see the API docs of timer:
Timer tasks should complete quickly. If a timer task takes excessive time to complete, it "hogs" the timer's task execution thread. This can, in turn, delay the execution of subsequent tasks, which may "bunch up" and execute in rapid succession when (and if) the offending task finally completes.
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Developer
rajani_dhawan
Posts: 55
Registered: ‎05-30-2011
My Device: BlackBerry torch 9800

Re: Call a Thread after a precise interval of time ???

:smileyhappy:

ok simon.. so if a timer-task takes a long time to complete.....then it will better to do the task in a separate thread (strating from the timer-task's run).

 

 

Developer
simon_hain
Posts: 16,347
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport

Re: Call a Thread after a precise interval of time ???

yes, exactly.
to avoid having too many threads you can also use an executorservice (there is no impl in the API, so you have to write it yourself)
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter