Question

I found very strange not to have a regular execution time for a TimerTask... I scheduled one , with delay: 1000 millisecs and recorded it when running... sometimes it's +1 millisec scheduledExecutionTime (as sensor timestamp) is System uptime relative, in nanosecs ( which is not stated in the doc )

Is there any reason for this unstable timing ?

        // BEGINNING OF THREAD RUN
        @Override
        public void run() {
            mRunning = true;
            RecordingTimerTask recordingTask = new RecordingTimerTask();
            Timer recordingTimer = new Timer();
        recordingTimer.schedule(recordingTask, 0, 1000);
       .....

            class RecordingTimerTask extends TimerTask {
            public void run() {
        String data = "" + scheduledExecutionTime();
           for (int i = 0; i < 3; i++) {
                data = data + ";" + timestamps[i];
            }
       ....

recorded data :

1391015410050   0
1391015411050   1000
1391015412050   1000
1391015413050   1000
1391015414050   1000
1391015415051   1001  <+ 1
1391015416051   1000
1391015417052   1001  <+ 1
1391015418052   1000
1391015419052   1000
1391015420052   1000
1391015421052   1000
1391015422052   1000
1391015423052   1000
1391015424052   1000
1391015425052   1000
1391015426053   1001  <+ 1
...
Était-ce utile?

La solution

That's actually pretty accurate from most real-world situations. TimerTask (like Timer and CountDownTimer) all run in background threads that are not high priority. Other threads, like the UI thread, may execute and block.

Take a look at this post and my response there:

Android - CountDownTimer interval is not constant

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top