How does System.currentTimeMillis() get its time

青春壹個敷衍的年華 提交于 2019-12-01 04:26:25

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/34596259/how-does-system-currenttimemillis-get-its-time

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!