waheebyaqub
waheebyaqub

Reputation: 293

in multi threaded code does System.nanoTime() produces small inaccuracy

in multi-threaded code does System.nanoTime() produces small inaccuracy whenever the threads are rescheduled? and if yes does this error accumulates and is this also true in single-threaded code?

for example when threads start executing it get the time using System.nanoTime() at the beginning and then just before exiting from thread block it records the time using same System.nanoTime()

Upvotes: 0

Views: 854

Answers (1)

Ted Hopp
Ted Hopp

Reputation: 234857

What do you mean by "small inaccuracy"? Although System.nanoTime() gives you nanosecond resolution, there's no guarantee as to the accuracy of the elapsed time that it measures. As far as I know, calling System.nanoTime() won't disrupt thread scheduling; there's a small cost of the method call and execution, but that's it. (Of course, doing lots of those calls will accumulate significant CPU time if you do enough of them.)

Upvotes: 1

Related Questions