Doubt about System.currentTimeMillis ()

3

I was discussing with a friend now little about the origin of the number that results from this method. Where does he come from? Has he been told since? Does it reset ?

    
asked by anonymous 05.11.2017 / 04:50

1 answer

3

It comes from the operating system.

The OS is loaded and takes an initial state contained in the machine's memory (needs to have a battery or be connected) and / or from an external source (internet or other form of input) and picks up how much time passes each cycle of the processor (it even has this information in it).

The processor is incrementing a value in one place at each execution cycle.

The operating system may have several mechanisms to handle this.

For example, it says that it wants this information every time and the processor signals every few cycles (in truth we call ticks which may not be the cycle itself) to calculate how much time has passed from that initial state. Then it can increment the last time it controls.

It can only check when it is requested by some application (this usually happens all the time) and calculate the past time by adding the initial state that it obtained (which can be updated under certain circumstances) with the time spent with the ticks increments and the time each of them takes.

There is hardware that can do this and the OS just needs to get the value ready.

Obviously if the operating system has no way of knowing the exact time when it starts this number will be wrong. There is no magic.

This period can be greater than 1 millisecond, so you can often not use this type of information to check time spent on an operation, as many people do to evaluate performance. Even with shorter times there will be discrepancy. So just use to know the current time, not for time measurements.

The number that Java picks up is a timestamp which is an amount of time that passed from a moment in the predetermined time. In case it uses the standard adopted by Unix, then it is the initial moment of January 1, 1970 ( see how it is now ).

If everything is correct on the machine it does not reset .

If the timestamp control is done with a small die, it may be that one day it gives some trouble and can go back to the initial 1970's, but this is a specific implementation problem. p>

Documentation .

    
05.11.2017 / 14:30