android - Executors.newSingleThreadScheduledExecutor().schedule 的可靠性

标签 android multithreading scheduledexecutorservice

这个执行器有什么已知问题吗?还是我用错了?我需要安排上传在单独的线程,我希望在当前上传完成后的特定时间后触发下一次上传。

因此,我的服务实现中有一些代码摘录:

ScheduledExecutorService periodicUploadExecutor;

@Override
public void onCreate() {
    super.onCreate();

    // some stuff...

    periodicUploadExecutor = Executors.newSingleThreadScheduledExecutor();
    periodicUploadExecutor.schedule(uploadPointsToServer, getCurrentUploadIntervalInMs(), TimeUnit.MILLISECONDS);
}

private Runnable uploadPointsToServer = new Runnable() {
    public void run() {

        Log.d("SOMETAG", "--->>>  UPLOAD runnable started!");

        // upload stuff here...

        Log.d("SOMETAG", " upload runnable scheduling next after "+getCurrentUploadIntervalInMs());
        periodicUploadExecutor.schedule(uploadPointsToServer, getCurrentUploadIntervalInMs(), TimeUnit.MILLISECONDS);

        Log.d("SOMETAG", "<<<---  upload runnable ENDED!");
    }
}

private final int getCurrentActiveSampIntervalInMs() {
    return 300000; // just an example
}

但是当我检查日志时,我看到了以下内容:

01-08 15:33:42.166 D/SOMETAG ( 4606): --->>>  UPLOAD runnable started!
01-08 15:33:43.166 D/SOMETAG ( 4606):  upload runnable scheduling next after 300000
01-08 15:33:43.166 D/SOMETAG ( 4606): <<<---  upload runnable ENDED!
01-08 15:38:43.166 D/SOMETAG ( 4606): --->>>  UPLOAD runnable started!
01-08 15:38:44.174 D/SOMETAG ( 4606):  upload runnable scheduling next after 300000
01-08 15:38:44.174 D/SOMETAG ( 4606): <<<---  upload runnable ENDED!
01-08 15:43:44.174 D/SOMETAG ( 4606): --->>>  UPLOAD runnable started!
01-08 15:43:45.143 D/SOMETAG ( 4606):  upload runnable scheduling next after 300000
01-08 15:43:45.143 D/SOMETAG ( 4606): <<<---  upload runnable ENDED!
01-08 16:01:38.887 D/SOMETAG ( 4606): --->>>  UPLOAD runnable started!

所以前三个很顺利,但最后一个在 18 分钟后开始,而不是 5 分钟!该服务还在 15:43 和 16:01 之间获取位置更新,但是位置监听器在主线程上运行,并且位置更新之间有几秒钟的时间间隔,所以没有什么应该阻止计划的执行程序触发......但它 延迟时间是原定延迟的三倍多!这怎么可能?

最佳答案

您需要使用 AlarmManager 而不是 Executor/handler 或使用 Partial Wakelock保持CPU开启。

如果 cpu 处于 sleep 模式,您的应用程序将不会运行,直到手机被唤醒。
AFAIK 只有 AlarmManager 可以通过唤醒设备给你一个回调。为此,您需要使用 ELAPSED_REALTIME_WAKEUPRTC_WAKEUP 类型,其他选项将再次导致延迟。

关于android - Executors.newSingleThreadScheduledExecutor().schedule 的可靠性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14236303/

相关文章:

java - 按特定顺序仅使用 5 个线程打印从 1 到 10 的整数

java - 如果上一个任务未完成,如何不启动 ScheduledExecutorService 任务

android - 如何获取在使用其 SD 卡的真实设备上运行的 android 应用程序的日志?

Android 11后台定位权限 "Allow only while using the app"不授予权限

java - 在 Android 10 上保存视频并将其插入图库

java - 为什么程序等待schedule()完成但不等待scheduleWithFixedDelay()?

Java Scheduled Executor 服务功能

java - 获取触摸输入的 x 和 y 坐标(Android studio,Java)

c++ - CRITICAL_SECTION 用于设置和获取单个 bool 值

multithreading - 绑定(bind)和绑定(bind)之间的区别