System.currentTimeMillis()如何获得时间 [英] How does System.currentTimeMillis() get its time

查看:351
本文介绍了System.currentTimeMillis()如何获得时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否已实现方法System.currentTimeMillis()以便对基础操作系统进行系统调用以接收当前时间?

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

我问,因为据我所知,该方法运行速度非常快,并且只需要6个CPU时钟,但这没有任何意义,因为已知系统调用很慢.

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.

我在这里想念什么?

推荐答案

System.currentTimeMillis()通常不需要切换到内核模式.操作系统提供了一种机制,该机制允许通过将相应的内核页面直接映射到应用程序地址空间来从用户模式读取当前时间.

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.

例如Linux上System.currentTimeMillis()的Oracle JDK和OpenJDK实现调用glibc gettimeofday 函数.此调用通过 vDSO .

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.

这篇关于System.currentTimeMillis()如何获得时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆