Scheduling problem with Timer and TimerTask in emulator

Common bugs/problems with the Android SDK the Emulator and the ADT-Plugin.

Scheduling problem with Timer and TimerTask in emulator

Postby Larsen » Fri Jan 14, 2011 12:17 pm


I am kind of new to android programming, but I found a problem in my program. I have not found any solutions on Google.

I use Eclipse on Windows 7 (64-bit) and have a problem regarding Timer and TimerTask. Here is the code which I run on the emulator:

Timer t = new Timer();
t.schedule(new TimerTask() {
long time;
public void run() {
Log.i("Timers", "Timediff is: " + (System.currentTimeMillis() - time));
time = System.currentTimeMillis();
}, 0, 50);

The result of the log becomes like this most of the time:

01-14 00:05:36.105: INFO/Timers(333): Timediff is: 68
01-14 00:05:36.174: INFO/Timers(333): Timediff is: 70
01-14 00:05:36.245: INFO/Timers(333): Timediff is: 68
01-14 00:05:36.314: INFO/Timers(333): Timediff is: 68
01-14 00:05:36.383: INFO/Timers(333): Timediff is: 70

The problem is that the time is around 20ms more than it should, and I cant believe that the logging itself takes more time than 50ms. After trying the same code, but with 100ms, the result of the log is around 130-135ms. I have also tried to stop many of the processes which is running on the emulator in DDMS, and that have not affected the results in the log, except when i stopped this process; namely, then the results is like this:

01-14 00:05:16.041: INFO/Timers(333): Timediff is: 51
01-14 00:05:16.091: INFO/Timers(333): Timediff is: 49
01-14 00:05:16.141: INFO/Timers(333): Timediff is: 51
01-14 00:05:16.191: INFO/Timers(333): Timediff is: 51
01-14 00:05:16.241: INFO/Timers(333): Timediff is: 52

This is more normal results. The problem I now face is that this process ( is coming back (on its own) after around 1 second after i stopped it. I wish i had an oppertunity to permanently stop this process (such that it did not come back), or - the best solution - get to know the reason for the strange behavior on the emulator (these extra milliseconds). When I try to develop a network game with communication between my android-phone and the emulator on my PC, it becomes really hopelessly when my phone and the emulator does not have a (almost) same referance-clock. The aberration is now around 20ms, which is not acceptable.

An extra information is that this code were tested on my android phone (HTC Desire HD) and worked with results which were just like they on the emulator when the process were stopped, which you should expect from this scheduling.

I hope someone have a clue on why this problem just appear on the emulator, and hopefully have a solution to the problem. Thanks in advance.

Lars Kristian Johansen

Edit: Forgot to mention, I tried this on emulator with android 2.0 and 2.2, both with the same result. I have also tested this on another android phone (HTC something..) and this behaves just like my HTC Desire HD.

Posts: 3
Joined: Fri Jan 14, 2011 11:49 am


Return to SDK/ADT/Emulator Problems

Who is online

Users browsing this forum: No registered users and 4 guests