|
這是個有趣的問題。
我們的一個weblogic應(yīng)用服務(wù)器,運行于HP-UX,啟動后,發(fā)現(xiàn)操作系統(tǒng)時間與時間服務(wù)器不一致(差一分多鐘),所以就把操作系統(tǒng)時間與時間服務(wù)器同步了一下。這時候怪事出現(xiàn)了,從weblogic應(yīng)用中取得的時間與操作系統(tǒng)時間不一致了(沒注意weblogic中取得的時間是否與操作系統(tǒng)修改之前的時間相同,待會兒驗證。已經(jīng)確認(rèn),目前發(fā)現(xiàn)HP的JDK會有這個問題,SUN的JDK沒有問題)。 在網(wǎng)上查了一下,發(fā)現(xiàn)這個問題可能是由于HP-UX的JDK取時間機(jī)制造成的。
HP-UX SDK 1.3.1.08的release notes,里面有這么一段說明,原文如下:
- date/time methods - new defaults
-
- Since SDK 1.2.2.09 and SDK 1.3.1, there has been a change in the way the HotSpot JVM uses the gettimeofday() system call to obtain date and time information.
-
- For performance reasons a new mechanism is used that uses the number of cpu ticks since the application started, to calculate the current time.
-
- As a result, changes to the system date or time using date(1), adjtime(2) or time synchronization utilities such as ntp will not be 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 to tell the JVM to use the gettimeofday call instead of the new, lightweight mechanism. However you may notice a drop in performance.
上面說的意思大致是,HP-UX SDK 1.2.2.09 和HP-UX SDK 1.3.1之后,由于performance的原因,JVM中讀取系統(tǒng)時間的時候,是在應(yīng)用程序啟動之后,讀一次OS的系統(tǒng)時間,然后JVM中根據(jù)CPU ticks來自己計算時間,而不是實時地去OS 取系統(tǒng)時間。這樣的話,在應(yīng)用程序運行當(dāng)中,如果有ntp改變了OS的系統(tǒng)時間,那么JVM和OS的系統(tǒng)時間就不一致了。只有等下一次應(yīng)用程序重新啟動的時候,JVM才會重新讀取OS系統(tǒng)時間。
解決方案就是啟動應(yīng)用程序的時候,用-XX:+UseGetTimeOfDay參數(shù),雖然這會導(dǎo)致performancex下降,但是可以保證JVM和OS的系統(tǒng)時間一致。
以前一直以為JVM是實時取OS的時間的,今天的問題再次教育了我:一切不能想當(dāng)然
|