Thread.sleep() sleeps way too long in emulator

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

Thread.sleep() sleeps way too long in emulator

Postby Larsen » Sat Jan 15, 2011 5:24 am

Hi, this is a continuation of the problem here:

sdk-adt-emulator-problems-f16/scheduling-problem-with-timer-and-timertask-in-emulator-t50410.html

I later found out that the problem seems to have to do with when you do a Thread.sleep(x). If x is smaller than 70ms, the thread always sleeps (or at least does not continue before) 70ms, and if I increase the x to more than 70ms, lets say 100ms, the result becomes just as explained earlier (around 135ms). I tested with this code:

long time = 0;
while(true){
try {
Thread.sleep(20);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Log.i("Timers", "" + (System.currentTimeMillis() - time));
time = System.currentTimeMillis();
}

After trying to find the reason, I have found out a very strange thing; if I go into DDMS, and view the File Explorer, viewing the folder /system/app/ the result is actually altering between the "correct" output (20ms, if I set 20ms as the sleeping time) and the old "wrong" output (around 70ms, if I set 20ms as the sleeping time). This altering alter in periods of around 2-3 seconds each. After discovering this, I might think that the com.android.phone process does not have anything to do with this, the reason why it looked like that, was because only that process turned itself back on automatically (which I think cause some special events in the emulator). If you are confused, I understand, you are not alone.. This seems like a very strange problem, and I hope someone could help me or tell me if this is not the case on your computer/emulator! =)

In addition, I testet this code:

while(true){
try {
Thread.sleep(20);
Thread.sleep(20);
Thread.sleep(20);
Thread.sleep(20);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Log.i("Timers", "" + (System.currentTimeMillis() - time));
time = System.currentTimeMillis();
}

and the result became.. (not 80ms as it should have been) but around 280ms! Which is 4 * 70ms!! This shows that the strange behavior repeats for every Thread.sleep().
Larsen
Freshman
Freshman
 
Posts: 3
Joined: Fri Jan 14, 2011 11:49 am

Top

Re: Thread.sleep() sleeps way too long in emulator

Postby Larsen » Mon Jan 17, 2011 2:38 am

I tried the same code in Ubuntu (64-bit), and the problem is not there! Guess I have to stop using windows..
Larsen
Freshman
Freshman
 
Posts: 3
Joined: Fri Jan 14, 2011 11:49 am

Top

Return to SDK/ADT/Emulator Problems

Who is online

Users browsing this forum: No registered users and 2 guests