gpt4 book ai didi

android - ScheduledThreadPoolExecutor 只有 "ticking"一次

转载 作者:搜寻专家 更新时间:2023-11-01 07:54:49 24 4
gpt4 key购买 nike

我在我的 Activity 中使用了一个 CountDownTimer 来实现一些倒计时功能。我决定放弃 CountDownTimer 并使用 ScheduledThreadPoolExecutor 因为 CountDownTimer 无法在 onTick() 中自行取消.

由于某些原因,下面代码中我的Runnable只执行了一次。我不确定为什么它不执行多次。 destroyCountdownTimer() 函数没有被命中。

private ScheduledThreadPoolExecutor mCountdownTimer;
private Tick mTick;

class Tick implements Runnable {
@Override
public void run() {
Log.e("tick", String.valueOf(mAccumulatedMilliseconds));
mAccumulatedMilliseconds += 1000;
populateTimeAccumulated();
populateTimeRemaining();
updatePercentages();

if (mTotalMilliseconds <= mAccumulatedMilliseconds) {
destroyCountdownTimer();
}
}
}

private void startCountdown() {
if (mAccumulatedMilliseconds < mTotalMilliseconds) {
mCounterIsRunning = true;

if (mCountdownTimer == null) {
mCountdownTimer = new ScheduledThreadPoolExecutor(1);
}

if (mTick == null) {
mTick = new Tick();
}

mCountdownTimer.scheduleAtFixedRate(mTick, 1000, 1000, TimeUnit.MILLISECONDS);
}
}

private void destroyCountdownTimer() {
if (mCountdownTimer != null) {
mCountdownTimer.shutdownNow();
mCountdownTimer = null;
}

if (mTick != null) {
mTick = null;
}
}

最佳答案

documentation说:

If any execution of the task encounters an exception, subsequent executions are suppressed.

将 try-catch block 添加到您的 Tick runnable。

关于android - ScheduledThreadPoolExecutor 只有 "ticking"一次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29522243/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com