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

New Developer
Posts: 6
Registered: ‎12-26-2011
My Device: none/android phone
My Carrier: T-Mobile

Eclipse Simulator: timer behaves erratically



I'm building a simple game. I use a timer to repeatedly run code in a function named runGame(). Here's what I do:

    	timer = new Timer();
    	timer.schedule(new TimerTask() { public void run() { runGame(); }}, (long)0, (long)800);

This should execute every approximately 0.8 seconds with no initial delay. It doesn't. I've inserted a counter into runGame() to see how often it actually runs - as soon as start it, the counter moves up to a value between 20 and 30, and hangs there. The game state changes accordingly (it's a snake game where the snake currently only moves up  -  it's pretty straightforward to see if it moved and how far it approximately did).

Even more weirdly, when I close the app - using Menu > close, then checking it it's still in the tray (it isn't) - and start it again, the runGame() counter hangs at 1, with an according game state. This is surprising, because you would expect the software to behave similarly at each startup. Closing the simulator and restarting it will again cause a hang at 20 to 30 and then hangs at 1.


There are no compile or runtime errors.


This strongly suggests something is wrong with the way I use the timer or the timer implementation of the simulator. Any thoughts?


Help would be very much appreciated!



New Contributor
Posts: 6
Registered: ‎11-29-2011
My Device: 9810
My Carrier: Verizon

Re: Eclipse Simulator: timer behaves erratically

The Timer class is just a Thread that runs a single task at a time.  If the current task hasn't finished it won't start the next one.  The easiest way to see what it is doing at any given time is to pause the simulator from the debug view in Eclipse and then look at the stack for the Timer thread.  You should be able to see if it is sleeping or running a task.