相关疑难解决方法(0)

Android Thread.sleep有时等待太久了

编辑:

这不是谈论精度问题,从下面的代码和日志中,你可以看到我要求睡眠1秒,但结果差不多200秒,有时它可能会跳到600秒,这不是一个精确的问题. .


我用handlerthread之前,有时贴到处理器的工作只是没有准时开始,以获得更多的细节我把它改成了基本的主题,和原来的Thread.sleep()方法是问题,但我不确定如何解决这个问题,可能的原因是什么?

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."); …
Run Code Online (Sandbox Code Playgroud)

multithreading android

3
推荐指数
1
解决办法
2431
查看次数

标签 统计

android ×1

multithreading ×1