PowerMsg3 LocalServer SDK Scheduler 优化:提高 scheduleWithFixDelay 方法效率
{'title':'PowerMsg3 LocalServer SDK Scheduler 优化:提高 scheduleWithFixDelay 方法效率','description':'本文介绍了 PowerMsg3 LocalServer SDK 中 Scheduler 模块的 scheduleWithFixDelay 方法的优化,通过去除重复的 tryCancel 调用,提高了方法的效率。','keywords':'PowerMsg3, LocalServer, SDK, Scheduler, 优化, scheduleWithFixDelay','content':'
优化后的 scheduleWithFixDelay 方法如下:
public void scheduleWithFixDelay(@NonNull final Object timerKey,
@NonNull final Duration initialDelay,
@NonNull final Duration delay,
@NonNull final Runnable runnable,
@NonNull final Context context) {
timerKey2Timeouts.compute(timerKey, (key, timeout) -> {
if (timeout != null) {
tryCancel(timeout);
}
return scheduleWithTimerKey(initialDelay, new ScheduledTimerTask(timerKey, delay, true, runnable, context));
});
}
优化点:
- 去掉了重复的 tryCancel 调用,直接在 compute 方法中调用即可。
- 对 timeout 为 null 的情况进行了判断,避免空指针异常。
原文地址: https://www.cveoy.top/t/topic/lH95 著作权归作者所有。请勿转载和采集!