有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java计时器结束得太早?

朋友们,我有一个简单的计时器,假设每10毫秒一次,持续2秒

        breathTimer = new CountDownTimer(2000, 10) {

        int currStep = 0;

        public void onTick(long millisUntilFinished) {
            Log.e(String.valueOf(currStep*10),String.valueOf(mSecPerBreath));
            currStep++;
        }

        public void onFinish() {
            Log.e("END: "+String.valueOf(currStep*10),String.valueOf(mSecPerBreath));
        }

    };

    breathTimer.start();

然而,它总是停得太早,例如

完:1640:2000

你认为可能有什么问题

或者,如果我将计时器设置为3000,它将以E/end:2520:2000结束


共 (1) 个答案

  1. # 1 楼答案

    根据documentation,与倒计时间隔(10ms)相比,onTick执行时间似乎很重要。这可能是延长期限(即2520年)的原因

    The calls to onTick(long) are synchronized to this object so that one call to onTick(long) won't ever occur before the previous callback is complete. This is only relevant when the implementation of onTick(long) takes an amount of time to execute that is significant compared to the countdown interval.