This is the mail archive of the
mailing list for the Java project.
Re: Patch: FYI: System.nanoTime
- From: David Daney <ddaney at avtrex dot com>
- To: Cédric Berger <cedric at berger dot to>
- Cc: tromey at redhat dot com, java-patches at gcc dot gnu dot org
- Date: Fri, 10 Mar 2006 00:30:08 -0800
- Subject: Re: Patch: FYI: System.nanoTime
- References: <email@example.com>
Cédric Berger wrote:
+ struct timespec now;
+ if (clock_gettime (CLOCK_REALTIME, &now) == 0)
+ jlong result = (jlong) now.tv_sec;
+ result = result * 1000 * 1000 + now.tv_nsec;
+ return result;
+ // clock_gettime failed, but we can fall through.
+#endif // HAVE_CLOCK_GETTIME
+ return _Jv_platform_gettimeofday () * 1000LL;
Why do you use CLOCK_REALTIME? it represents the time
from the epoch, but nanoTime() javadoc atate that
nanoTime() value should be used for measuring elapsed
time and is unrelated to wall-clock time.
The problem is when you use CLOCK_REALTIME to measure
elapsed time and the user changes the time...
I had to write the same code for another project, and
I used the following snipped for Linux/BSD/Solaris:
id = CLOCK_MONOTONIC;
id = CLOCK_HIGHRES;
#error bad platform
if (clock_gettime(id, &tv) - 0)
I think this is an excellent idea.
I cannot begin to tell you how much a pain in the a** it is to manage
multiple time outs on a single thread while at the same time trying to
maintain a 'wall clock' the can be changed.