最近碰到一个jvm时间(System.currentTimeMillis)与操作系统时间不一致的问题,用的HP-UNIX系统,多台服务器的时间同步是用了一个NTP服务。时间同步策略:时间和主服务器的误差大于1秒。
经过查找资料,终于发现原因,如下:
The HotSpot JVM uses the gettimeofday() system call to obtain date and time
information. For performance reasons a new mechanism that uses the number of CPU
ticks since the application started is used to calculate the current time. As a result,
changes to the system date or time using the date command, adjtime() function, or
time synchronization utilities such as ntp will not be reflected in the date and time
that the Java program returns until the process is restarted.
If your application requires that system time changes are immediately reflected, you
can use the -XX:+UseGetTimeOfDay option to tell the JVMto use the gettimeofday
call instead of the new, lightweight mechanism. However you may notice a drop in
performance
-XX:+UseGetTimeOfDa
Instructs the JVM to use the GetTimeOfDay call instead of the mechanism used in
earlier versions whereby the number of cpu ticks since the application started is used
to calculate the current time. With this new mechanism, changes to the system date or
time using date(1), adjtime(2), or time synchronization utilities such as ntp are
not reflected in the date and time that Java™ returns, until the process is restarted. If
your application requires that Java™ immediately reflects such system time changes,
you can use the -XX:+UseGetTimeOfDay option, however you may notice a drop in
performance.
以上信息引用处:http://bizsupport2.austin.hp.com/bc/docs/support/SupportManual/c02697864/c02697864.pdf
即hotspot jvm使用gettimeofday系统调用来获取日期和时间信息。在HP-UX中,由于性能原因,在应用启动后,采用了一种使用CPU 时钟周期的机制来计算当前时间。带来的后果就是,使用date命令,adjtime函数或者形如ntp的时间同步服务工具修改系统时间后,java应用未能在重启之前反映出这种修改。如果要求操作系统时间修改立马反映到java应用中,可以使用-XX:+UseGetTimeOfDay选项来告诉JVM使用gettimeofday系统调用,但需要注意的是,这会使性能下降。