menemenemu
menemenemu

Reputation: 209

Set the update rate for a method

How can I handle the "update rate" of a method, let's say I want to run a method every 200 milliseconds, how would I do that in an very accurate way without using Thread.sleep(...) methods?

Upvotes: 0

Views: 213

Answers (5)

pushy
pushy

Reputation: 9635

You could use ScheduledThreadPoolExecutor.

Upvotes: 0

Peter Lawrey
Peter Lawrey

Reputation: 533590

I would try using ScheduleExecutorService.scheduleAtFixedRate Its a built in library with fairly high accuracy. (It uses nano timings where available instead of mill-second timing which older libraries use)

Upvotes: 2

I think you are referring to a way of repeatedly executing the same task with a set time interval. In that case you are looking for java.util.Timer and java.util.TimerTask.

Upvotes: 1

nfechner
nfechner

Reputation: 17525

Java is (normally) not a real-time system. If you need to call a method precisely every 200 miliseconds, you will run into problems. If some deviation is ok, use a scheduler like Quartz for this.

Upvotes: 3

Manish
Manish

Reputation: 3968

The way I understand your question - you want to "execute" a method every 200 milliseconds and you do not want to use the Thread.sleep() stuff.

The solution to your problem lies in the Timer/TimerTask class combination. Please read http://enos.itcollege.ee/~jpoial/docs/tutorial/essential/threads/timer.html

Have a look at the section "Performing a Task Repeatedly" on the above link.

Upvotes: 2

Related Questions