I have been researching and I am struggling to actually choose the best option. I am using processing sketch that runs java code, and I want to start an animation in several computers( OS X and windows) at the same time. The basic idea is to send a OSC message to each computer and after they receive a message they will store the currentTime plus the timespan(let say after 10 second). And each computer track the currentTime and when it reach the intended Time they will start the animation. Now I cannot figure out which System should I use. System.currentTimeMillis() or System.nanoTime(); I already tested with two computers(both Systems) and it seems to work. Both computers are OS X but I never tried with a windows one and it seems for System.currentTimeMillis() can be a lag of 50ms. I'm really confuse in this matter. Someone can me explain or highlight. thank in advance
Asked
Active
Viewed 48 times
0
-
Perhaps you might be interested in this question https://stackoverflow.com/questions/351565/system-currenttimemillis-vs-system-nanotime – phlaxyr Jan 19 '19 at 22:01
-
Yes I already read that post but I got confuse in this part "The value returned represents nanoseconds since some fixed but arbitrary time ". what does it means. Is this arbitrary time different for each computer/operateSystems, which means that the values are going to be different or it is the same for all computer/operateSystems? – Litiec Jan 19 '19 at 22:07
-
From nanoTime() documentation: "This method can only be used to measure elapsed time and is not related to any other notion of system or wall-clock time. " - so we can't use it to associate a timestamp with some event, but we can invoke it two times and get an accurate difference (elapsed time between the two invokes). – Jeppe Jan 19 '19 at 22:13
-
So it means for this purpose is better the System.currentTimeMillis()? – Litiec Jan 19 '19 at 22:24
-
@Litiec Yes, I believe System.currentTimeMillis() is more accurate. Is the 50ms lag unacceptable for your purpose? Maybe this is related, as you're essentially trying to synchronize applications across computers: https://stackoverflow.com/questions/38711730/how-can-i-access-ntp-clock-in-java – Jeppe Jan 19 '19 at 22:26
1 Answers
0
Simultanous simulations on two or more computers is tricky for some reasons.
- First of all I would make sure, all connected computers synchronize their clocks with NTP. (See more https://en.wikipedia.org/wiki/Network_Time_Protocol) Then the biggest difference is at most 50ms as far as I know. Otherwise every approach will fail because of the differences of the clocks.
- Second, clocks on different systems have different accuracy. I can recommend reading Alexey Shipilev's blog: https://shipilev.net/blog/2014/nanotrusting-nanotime/ . It is about the accuracy of clocks on machines in general.
Third you need to know that Linux has a round robin slide of 1ms and Windows about 10-15ms. Therefore Thread.sleep(...) will not work with smaller time spans reliable. If you want to´work with smaller time spans you need to do a kind of "busy waiting" which is ugly but necessary:
public class SleepUtil { public static final long MIN_PRECISION_IN_MICROS = 15L; public static void main(String[] args) { long before = System.nanoTime(); while (true) { final long after = System.nanoTime(); long diff = (after - before) / 1000l; before = after; System.out.println(diff + " micros"); SleepUtil.sleepMicros(500); } } private static void sleepMicros(int waitTimeInMicros) { final long startTimeInNanos = System.nanoTime(); long elapsedTimeInMicros = 0L; while (elapsedTimeInMicros < waitTimeInMicros - MIN_PRECISION_IN_MICROS) { elapsedTimeInMicros = (System.nanoTime() - startTimeInNanos) / 1000L; } } }
However, it will busy your cpu and not be always reliable (but most of the time).