Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does System.currentTimeMillis() get its time

Is the method System.currentTimeMillis() implemented to make a system call to the underlying operating system in order to receive the current time?

I ask since as far as I know, the method runs pretty fast, and takes as little as 6 CPU clocks, but this doesn't make sense because system calls are known to be slow.

What am I missing here?

like image 704
theDima Avatar asked Jan 04 '16 17:01

theDima


1 Answers

System.currentTimeMillis() does not usually require switching to kernel mode. OS provides a mechanism that allows reading current time from user mode by mapping corresponding kernel pages directly into application address space.

E.g. Oracle JDK and OpenJDK implementation of System.currentTimeMillis() on Linux calls glibc gettimeofday function. This call accesses kernel data directly from user space by means of vDSO.

like image 95
apangin Avatar answered Oct 30 '22 20:10

apangin