System.currentTimeMillis() compile to native, accurate?
Craig A. Vanderborgh
craigv@voxware.com
Thu Feb 9 16:10:00 GMT 2006
Rui Wang wrote:
>Hi,
>
>I wonder that how gcj maps Java's "System.currentTimeMillis()" to native
>machine code.
>
>In JVM, the time returned from "System.currentTimeMillis()" is not
>accurate due to various of issues, such as: garbage collection,
>thread management model, configurations, etc.
>
>In native environment, what will happen? Is it accurate?
>
>Thank you
>Rui
>
>
>
>
The source code for System.currentTimeMillis(), from natSystem.cc, looks
like this:
jlong
java::lang::System::currentTimeMillis (void)
{
return _Jv_platform_gettimeofday ();
}
In turn, _Jv_platform_gettimeofday() looks like this:
// gettimeofday implementation.
jlong
_Jv_platform_gettimeofday ()
{
#if defined (HAVE_GETTIMEOFDAY)
timeval tv;
gettimeofday (&tv, NULL);
return (tv.tv_sec * 1000LL) + (tv.tv_usec / 1000LL);
#elif defined (HAVE_TIME)
return time (NULL) * 1000LL;
#elif defined (HAVE_FTIME)
struct timeb t;
ftime (&t);
return (t.time * 1000LL) + t.millitm;
#elif defined (ECOS)
// FIXME.
return _clock();
#else
// In the absence of any function, time remains forever fixed.
return 23000;
#endif
}
So in answer to your question: "is it accurate"? It depends. It
depends on what else is going on in your program. Under conditions
where currentTimeMillis() is called and your next lines of code receive
the result without being interrupted by another thread or by garbage
collection, currentTimeMillis() is sure to very closely reflect what
gettimeofday() would produce in a C program.
By the way, if I'm not mistaken "real Java" also uses a native method
implentation in JNI to provide about the same implementation for
currentTimeMillis(). In this regard, GCJ and JVM are more similar than
different.
hth,
craig vanderborgh
voxware incorporated
Confidentiality Note: This message may contain information which is privileged or confidential and exempt from disclosure under applicable law. If the reader of this message is not the intended recipient, or the employee responsible for delivering the message to the intended recipient, you are hereby NOTIFIED that any dissemination, distribution, retention, archiving, or copying of this communication is strictly prohibited. If you received this email in error, please notify Voxware immediately by return email to the sender.
More information about the Java
mailing list