Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Thread.sleep sometimes waits far too long

Edit:

This is not talking about precision issues, from the code and log below, you can see that I requested to sleep for 1 second, but the result was almost 200 seconds, sometimes it could jump to 600 seconds, this cannot be a precision issue..


I was using handlerthread before and sometimes the job posted to handler just does not start on time, to get more details I changed it to the basic Thread, and it turns out the Thread.sleep() is the issue, but I'm not sure how to fix this, what could be the possible reasons?

hGpsThread = new Thread(mGpsWorker);
hGpsThread.start();

private final Runnable mGpsWorker = new Runnable() {
    @Override
    public void run() {
        long lastGpsRequestTime = 0;
        l.Write("GPS thread started.");
        while (isRunning) {
            l.Write("GPS thread loop start.");
            try {
                long currentTimeMillis = System.currentTimeMillis();
                if (currentTimeMillis >= lastGpsRequestTime + gpsUpdateInterval) {
                    l.Write("Requesting location update");
                    gpslib.getLocation();
                    lastGpsRequestTime = currentTimeMillis;
                }
                l.Write("GPS thread before sleep");
                Thread.sleep(1000);
                l.Write("GPS thread after sleep");
            } catch (InterruptedException e) {
            }
            l.Write("GPS thread loop end.");
        }
        l.Write("GPS thread ended.");
    }
};

NOTE: getLocation() calls requestLocationUpdates using a looper from another thread, so location updating should not affect this thread, as I understand.

getLocation() also creates a Timer and schedules a timeout, could that be the issue? as I understand it should not be an issue.

This is the log: (this only happens occasionally, say the chance is close to 0.5%)

Wed Jul 10 11:45:46 AEST 2013 GPS thread loop start.
Wed Jul 10 11:45:46 AEST 2013 GPS thread before sleep
Wed Jul 10 11:49:04 AEST 2013 GPS thread after sleep
Wed Jul 10 11:49:04 AEST 2013 GPS thread loop end.

Thanks

The testing environment is: HTC Aria, Android 2.2 And looks like it only happens when running on battery, but my app doesn't act differently on charging status.

like image 673
agou Avatar asked Jan 18 '26 10:01

agou


1 Answers

Basically Thread.sleep() and Handlers work fine as long as the screen is on (implies Android should not be in a deep sleep state/screen off state).

When the Screen is turned off, default Android behavior is to suspend Thread.sleep() and Handlers() until the screen is turned back on again or if it is woken up by some application grabbing a wakelock. Hence your application will work perfectly if the screen is on throughout, but when it is turned off, it will behave erratically.

The best work around is to switch to AlarmManager because when the Alarm is triggered, onRecieve() always grabs a wakelock by default causing Android to wake up and execute.

like image 69
Royston Pinto Avatar answered Jan 21 '26 01:01

Royston Pinto



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!