67

What is the most accurate way to call a function every N milliseconds?

  • Thread with Thread.sleep
  • TimerTask
  • Handler with postDelayed

I modified this example using Thread.sleep and it's not very accurate.

I'm developing a music app that will play sounds at a given BPM. I understand it's impossible to create an entirely accurate metronome and I don't need to - just looking to find the best way to do this.

Thanks

asked Sep 4, 2013 at 4:26
2

4 Answers 4

76

There are some disadvantages of using Timer

  • It creates only single thread to execute the tasks and if a task takes too long to run, other tasks suffer.
  • It does not handle exceptions thrown by tasks and thread just terminates, which affects other scheduled tasks and they are never run

ScheduledThreadPoolExecutor deals properly with all these issues and it does not make sense to use Timer.. There are two methods which could be of use in your case.. scheduleAtFixedRate(...) and scheduleWithFixedDelay(..)

class MyTask implements Runnable {
 @Override
 public void run() {
 System.out.println("Hello world");
 } 
}
ScheduledThreadPoolExecutor exec = new ScheduledThreadPoolExecutor(1);
long period = 100; // the period between successive executions
exec.scheduleAtFixedRate(new MyTask(), 0, period, TimeUnit.MICROSECONDS);
long delay = 100; //the delay between the termination of one execution and the commencement of the next
exec.scheduleWithFixedDelay(new MyTask(), 0, delay, TimeUnit.MICROSECONDS);
Andrew
1,08811 silver badges14 bronze badges
answered Sep 4, 2013 at 5:34
Sign up to request clarification or add additional context in comments.

2 Comments

Wow! Thank you so much - this is almost flawless! Significantly better timing than my old method
I am trying this method out for a periodic task, but it does not seem to work stackoverflow.com/questions/27872016/…
7

On Android you can create Thread with it's own Handler/Message Queue. It's quite accurate. When you see Handler documentation you can see, that it was designed for that.

There are two main uses for a Handler: (1) to schedule messages and runnables to be executed as some point in the future; and (2) to enqueue an action to be performed on a different thread than your own.

answered Sep 4, 2013 at 5:34

Comments

1

They are all the same precision-wise. Java timing precision is subject to the precision and accuracy of system timers and schedulers and is not guaranteed. See Thread.sleep and Object.wait API.

answered Sep 4, 2013 at 4:47

Comments

-13

Using TimerTask for the loop action is the better one. Recommend

answered Sep 4, 2013 at 4:33

1 Comment

I seriously doubt it, check this androidtrainningcenter.blogspot.in/2013/12/…

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.