SuperBiasedMan
SuperBiasedMan

Reputation: 9969

Is there a way to make Timer.ScheduleAtFixedRate more accurate?

I'm writing an Android app in java. Trying to make a simple rhythm game where you just tap the button on a beat. I was using a timer object with Schedule at Fixed Rate to make the button flash but then I discovered that the time is variable by a few milliseconds.

Obviously a rhythm game needs particular timing to come out right, so is it possible to make this more precise and accurate or am I barking up the wrong tree with using this method for precise timing?

Upvotes: 0

Views: 1131

Answers (2)

G. Blake Meike
G. Blake Meike

Reputation: 6715

I suggest you don't use ScheduleAtFixedRate!! Use a Looper and Handler.sendMessageDelayed()

Upvotes: 1

fge
fge

Reputation: 121710

I don't know for Android but here is what happens for "real" Java...

A Timer uses System.currentTimeMillis() to keep track of time; this method is sensitive to system time changes (say, you run an NTP server for instance).

Which is why, if you want better precision, you use a ScheduledExecutorService; this relies o System.nanoTime(), which is a nanosecond precision counter which keeps increasing for the life of the process, even if the system time changes.

So --> try a ScheduledExecutorService instead.

Upvotes: 1

Related Questions