HP-UNIX上通過jvm獲取的時間與操作系統時間不一致的原因與解決方案
最近碰到一個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系統調用,但需要注意的是,這會使性能下降。