My Java Development Learning journey------The difference between >system.nanotime and System.currenttimemillis

Source: Internet
Author: User


Let's start with a question: what is the output of the following code?
Import Java.util.hashmap;import Java.util.map;public class Hashmaptest {public static void main (string[] args) {map< String, string> map=new hashmap<string, string> (), Map.put (String.valueof (System.currenttimemillis ()) + "A", "1"); Map.put (String.valueof (System.currenttimemillis ()) + "A", "2"); Map.put (String.valueof ( System.currenttimemillis ()) + "A", "3"); for (map.entry<string, string> entry:map.entrySet ()) { System.out.println (Entry.getvalue ());}}}

A. 123 b.213 c.123 sequence cannot be determined D. None of the answers are right.
I chose the answer is C.123 order cannot be determined, the understanding of the program is: The key is set in map, the value is Collection,map.entryset () the resulting type is set, so can use foreach (), The set order is random, so the order of 1, 2, and 3 of the output is indeterminate. But when I run the program on Eclipse, the output is: 3. The result is blind my eyes, the original is now the computer is running fast, so it is very likely that the value of System.currenttimemillis () is equal, and set is not allowed to duplicate, so it will overwrite its value, So when the toilet on the computer only output 3.
if you want to achieve the desired output: 123 The order cannot be determined. You can change System.currenttimemillis () to System.nanotime ().
1 nanoseconds =1000 leather seconds  
 
1 nanoseconds =0.001 microseconds
 
1 nanoseconds =0.000001 Ms
 

1 nanoseconds =0.00000 0001 sec.


The following code is changed:
Import Java.util.hashmap;import Java.util.map;public class Hashmaptest {public static void main (string[] args) {map< String, string> map=new hashmap<string, string> (), Map.put (String.valueof (System.nanotime ()) + "A", "1"); Map.put (String.valueof (System.nanotime ()) + "A", "2"), Map.put (String.valueof (System.nanotime ()) + "A", "3"); for ( Map.entry<string, string> Entry:map.entrySet ()) {System.out.println (Entry.getvalue ());}}}

The result of this output is that the 123 order cannot be determined. There could be 213,231,123 ...
=================================================================here is an explanation on the official Chinese document:Currenttimemillis
Currenttimemillis ()
Returns the current time in milliseconds. Note that when the time unit of the return value is milliseconds, the granularity of the value depends on the underlying operating system, and the granularity may be greater. For example, many operating systems measure time in dozens of milliseconds.

See the Date description of the class for a discussion of the subtle differences that may occur between computer time and Coordinated Universal Time (UTC).

Return:
the time difference (measured in milliseconds) between the current and coordinated GMT January 1, 1970 midnight.

Nanotime
Nanotime ()
Returns the current value of the most accurate available system timer, in nanoseconds.

This method can only be used to measure the time that has been spent, regardless of any other time concept of the system or clock time. The return value represents the number of nanoseconds from a fixed but arbitrary time (perhaps from a later date, so the value may be negative). This method provides the accuracy of the nanosecond, but not the accuracy of the nanosecond as necessary. It does not guarantee the change frequency of the value. The difference between successive calls in which the value range is greater than approximately 292 years (263 nanoseconds) is that the time that has been exceeded cannot be accurately calculated due to a digital overflow.

For example, test the length of time some code executes:

   Long startTime = System.nanotime ();   ... the code being measured ...   

Return:
The current value of the system timer, in nanoseconds.

==================================================================================================

Ouyangpeng welcome reprint, sharing with people is the source of progress!

Reprint please keep the original address : Http://blog.csdn.net/ouyang_peng

==================================================================================================


My Java Development Learning journey------The difference between >system.nanotime and System.currenttimemillis

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.