I am making a code where I want definite precision in my timing. I use a robot to make some actions, then I use Thread.sleep(some_time)
for some_time
to be elapsed between the actions. But I don't get best results because as I search it, sleep
is not accurate. What would be the best way to accomplish this? I mean simulate Thread.sleep
with other methods.

- 54,432
- 29
- 203
- 199
-
type this "java precision timing" into a search engine... – Mitch Wheat Apr 19 '14 at 11:40
-
You can check the time difference using system current time. – Braj Apr 19 '14 at 11:47
-
4Neither Java nor C or C++ can make any guarantees beyond those made by the operating system. If you need real-time behaviour you need a real-time operating system. – user207421 Apr 19 '14 at 11:59
-
@Bratty You can call C/C++ from Java, in fact this is what Thread.sleep() does, can you explain what you mean? – Peter Lawrey Apr 19 '14 at 12:37
-
What accuracy do you need? The accuracy is sleep() is rarely a real issue which is why it no more accurate than the OS provides. – Peter Lawrey Apr 19 '14 at 12:40
-
1@PeterLawrey - Reading between the lines, the OP most likely wants his application thread to "wake up" with an accuracy of 1 millisec or better ... to avoid the robot overshooting in some physical movement. Hard real-time stuff. The real problem is that a non-real-time OS can't guarantee sub-millisecond wakeups. – Stephen C Apr 19 '14 at 12:56
-
@StephenC The problem is; even if you don't go to sleep and use a CPU 100% you can get 5 ms delays on a linux machine. On linux you can isolate the cpu and busy wait for the time and you get around 10 micro-second "jumps" in time. – Peter Lawrey Apr 19 '14 at 12:58
-
One option is to wake about 5 ms before the timeout and start slowing down so you have very little chance of overshooting. i.e. engineer a tolerance to a few ms jitter. – Peter Lawrey Apr 19 '14 at 13:01
-
I think this different from the marked duplicate: [The other question](http://stackoverflow.com/q/18736681/1804173) only asks how precise `Thread.sleep` is, while this question here asks for alternative sleep approaches. Both questions should have different answers. – bluenote10 Feb 24 '16 at 07:10
2 Answers
Timing in modern OSes is never precise, unless you use a language/framework that was explicitly designed for this. You can however work with a reasonable uncertainty in most operation systems. But "reasonable" depends on the problem you are trying to solve.
In Java Thread.sleep is a very basic implementation that is way too overused in my opinion. Java offers these basic threading tools not because they are the best solution, but because they are basic tools. Java as well offers many other, more sophisticated tools, that might suit your needs much better.
In example if you want "precise" timing, you can instead use a ScheduledExecutorService, which uses the OS scheduling service to offer a precision of at least milliseconds, and often even nanoseconds. Although it is not exact to the nanosecond (despite the offer), it will usually be much more accurate than Thread.sleep. But both will not be accurate on a heavily overloaded system. If this is sufficient for your problem, then you should go with this. Otherwise you need a different language/execution environment.

- 13,879
- 6
- 30
- 54
In fact, Java relies on OS facilities to implement its timers1. The real reason that you don't get better than 1 millisecond accuracy on timers is that this is the limit of what a typical (non-realtime) OS provides. And even if the time resolution was finer, the OS still could not guarantee to "wake up" the user process at precisely the time that the application asked for.
If you need real (i.e. hard) realtime behaviour, including high precision timers and clocks, you need a realtime OS, and you need to run a Realtime Java on it.
1 - These are the same OS facilities that the C and C++ libraries use. So simply switching to C or C++ won't help much.

- 698,415
- 94
- 811
- 1,216