Home > Java > javaTutorial > Is System.nanoTime() Truly Reliable on Modern Multi-Core Systems?

Is System.nanoTime() Truly Reliable on Modern Multi-Core Systems?

Susan Sarandon
Release: 2024-11-17 19:06:02
Original
994 people have browsed it

Is System.nanoTime() Truly Reliable on Modern Multi-Core Systems?

Is System.nanoTime() Still Flawed on Multi-Core Systems?

The blog post "Beware of System.nanoTime() in Java" raised concerns about the reliability of System.nanoTime() on x86 architectures, where the CPU-specific counter it utilizes could lead to negative time measurements due to thread scheduling across differing processors with asynchronous counters.

However, a 2011 response from a Sun engineer (now Oracle) clarified that this issue was addressed in Windows XP Service Pack 2 and is no longer a concern on modern Windows and Linux systems.

Windows:

  • The default implementation of QueryPerformanceCounter, used by System.nanoTime(), has been changed to use the power management timer (PMTimer), which is synchronized across processors on SMP systems.

Linux:

  • As of kernel version 2.6.18, the system logic for detecting asynchronous time sources has been improved.
  • Unreliable time sources are typically detected and fallback to a more reliable clock source is employed.

Other Platforms:

The provided response does not address SPARC or Solaris systems. However, it is reasonable to assume that these platforms have also implemented similar mechanisms to ensure consistent timekeeping across multiple processors.

It is important to note that this information was accurate as of 2011. For the most recent updates on the implementation and behavior of System.nanoTime(), please refer to up-to-date documentation and community discussions.

The above is the detailed content of Is System.nanoTime() Truly Reliable on Modern Multi-Core Systems?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template