Home > OS >  how to set the interval in milliseconds so that it will consistently change once every 100ms
how to set the interval in milliseconds so that it will consistently change once every 100ms

Time:05-11

I want to make my millisecond time change every 100ms, but the time I get is inconsistent sometimes the intervals I get are 99, 100 and 101. The interval I want is 100 and consistent.

as below my problem is now the resulting interval will change like number 4 and 5.

  1. 19:56:16:096
  2. 19:56:16.196
  3. 19:56:16.296
  4. 19:56:16.397
  5. 19:56:16.495

while the result I want is like this below

  1. 19:56:16.096
  2. 19:56:16.196
  3. 19:56:16.296
  4. 19:56:16.396
  5. 19:56:16.496

here's a screenshot of my App enter image description here

here's the code I use

public void startTimer(){
    carousalTimer = new Timer(); // At this line a new Thread will be created
    carousalTimer.scheduleAtFixedRate(new TimerTask() {
        @Override
        public void run() {
            //DO YOUR THINGS

            runOnUiThread(new Runnable() {

                @Override
                public void run() {

                    long time = System.currentTimeMillis();
                    SimpleDateFormat sdf3 = new SimpleDateFormat("HH:mm:ss.SSS");
                    Date date = new Date();
                    data = sdf3.format(time).toString();
                    aList.add(data);
                    get(data);

                }
            });

        }
    }, 100, 100); // delay
}

CodePudding user response:

I believe it is part of how Android works. It is a multi-threaded environment where resources are share among multiple processes.

According to the documentation Timer.scheduleAtFixedRate() is the best approach for the following use cases:

In fixed-rate execution, each execution is scheduled relative to the scheduled execution time of the initial execution. If an execution is delayed for any reason (such as garbage collection or other background activity), two or more executions will occur in rapid succession to "catch up." In the long run, the frequency of execution will be exactly the reciprocal of the specified period (assuming the system clock underlying Object.wait(long) is accurate).

Fixed-rate execution is appropriate for recurring activities that are sensitive to absolute time, such as ringing a chime every hour on the hour, or running scheduled maintenance every day at a particular time. It is also appropriate for recurring activities where the total time to perform a fixed number of executions is important, such as a countdown timer that ticks once every second for ten seconds. Finally, fixed-rate execution is appropriate for scheduling multiple repeating timer tasks that must remain synchronized with respect to one another.

https://developer.android.com/reference/java/util/Timer#scheduleAtFixedRate

If it is only necessary to be visual perfect, you can set the time relative to the start time. For example by increment an index every 100ms:

final long DELAY = 100; // 100ms delay
AtomicInteger index = AtomicInteger(0);
long startTime = System.currentTimeMillis();
carousalTimer = new Timer(); // At this line a new Thread will be created
carousalTimer.scheduleAtFixedRate(new TimerTask() {
    @Override
    public void run() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                long time = startTime   (index.incrementAndGet() * DELAY)
                SimpleDateFormat sdf3 = new SimpleDateFormat("HH:mm:ss.SSS");
                Date date = new Date();
                data = sdf3.format(time).toString();
                aList.add(data);
                get(data);
            }
        });

    }
}, DELAY, DELAY);
  • Related