Why is the task(TimerTask) triggered many times before late resume?

[Issue]

Why is the task(TimerTask) triggered many times before late resume?

 

[Sample code]

private Timer timer;

private BISTTimerTask bistTimerTask;

 

           timer = new Timer();

           bistTimerTask = new BISTTimerTask();

           timer.scheduleAtFixedRate(bistTimerTask, 0, 1000);

 


 

[Source code]

Java.util.timer.java

   private void mainLoop() {

       while (true) {

           try {

               TimerTask task;

               boolean taskFired;

               synchronized(queue) {

                   //……………………

                   // Queue nonempty; look at first evt and do the right thing

                   long currentTime, executionTime;

                   task = queue.getMin();

                   synchronized(task.lock) {

                       //………………………….

                       currentTime = System.currentTimeMillis();

                       executionTime = task.nextExecutionTime;

                       if (taskFired = (executionTime<=currentTime)){

                               //……………………………..

                               queue.rescheduleMin(

                                               executionTime+ task.period);

                       }

                   }

                   if (!taskFired) // Task hasn't yet fired; wait

                       queue.wait(executionTime - currentTime);

               }

               if (taskFired)  // Task fired;run it, holding no locks

                   task.run();

           } catch(InterruptedException e) {

           }

       }

}

 

 

 

Before suspend

currentTime + task.period  = executionTime

 

The timer task needs to wait a period(1s).

After resume

The value of the variable(currentTime) is changed, but  the variable(executionTime) still keeps the value before suspend.

Here’s the equation between both variables.

executionTime + suspend time = currentTime

 

It’ll trigger timer task without waiting many times.



[log Analysis]

logs_1012_1828\6.20161012_094834

 

 

kernel_20161012_094834-1.log

 

<6>[  306.308898] Suspended for 83.258 seconds

<6>[  307.029785] suspend: exit suspend, ret = 0 (2016-10-12 09:54:48.738128655 UTC)

 

main_20161012_094834-1.log

 

We can only check the string highlighted in the code below.

The string had appeared 84 times near that time in main log, so It also means  that the timer task was triggered 84 times.

private class BISTTimerTask extends TimerTask {

        public void run() {

            //………………………….

            new_tic = System.currentTimeMillis();

            if(new_tic-last_tic<100){

                Log.d("feong", "resume");

                last_tic = new_tic;

                return;

            }

            last_tic = new_tic;

            //……………………………….

}

Please check the file(timer_trigger.txt).

 

 

 

 



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值