Java System. currenttimemillis and system. nanotime

Source: Internet
Author: User

In Java, timestamps are commonly used in system. currenttimemillis (), because it can be easily exchanged with date/Calendar, system. nanotime is not frequently used (at least I have never used it ). When I learned scheduledthreadpoolexecutor in the Java thread pool, I saw that it was used in many places, so I read the API curiously.

 

Currenttimemillis
public static long currentTimeMillis()
Returns the current time in milliseconds. note that while the unit of time of the return value is a millisecond, the granularity of the value depends on the underlying operating system and may be larger. for example, when operating systems measure time in units of tens of milliseconds.

See the description of the classDateFor a discussion of slight discrepancies that may arise between "computer time" and Coordinated Universal Time (UTC ).

Returns:
The difference, measured in milliseconds, between the current time and midnight, January 1, 1970 UTC.

 

Nanotime
public static long nanoTime()
Returns the current value of the most precise available system timer, in nanoseconds.

This method can only be used to measure elapsed time and is not related to any other notion of system or wall-clock time. the value returned represents nanoseconds since some fixed but arbitrary time (perhaps in the future, so values may be negative ). this method provides nanosecond precision, but not necessarily nanosecond accuracy. no guarantees are made about how frequently values change. differences in successive cballs that span greater than approximately 292 years (263 nanoseconds) will not accurately compute elapsed time due to numerical overflow.

For example, to measure how long some code takes to execute:

 long startTime = System.nanoTime(); // ... the code being measured ... long estimatedTime = System.nanoTime() - startTime; 
Returns:
The current value of the system timer, in nanoseconds.
Since:
1.5

 

I thought that nanotime was a more accurate time description than millistime, but after reading the above instructions, I found that this understanding is basically completely wrong.

First, the result returned by currenttimemills is from UTC to the current time, in milliseconds. However, nanotime may return any time, or even a negative number ...... According to the API description, the main purpose of nanotime is to measure a time period, such as the time used for code execution, the time used to obtain the database connection, and the time used for network access. In addition, nanotime provides the precision of the nanoseconds, but the actually obtained value may not be accurate to the nanoseconds.

But in general, these two functions have different purposes!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.