Java中线程用户时间和线程CPU时间的区别

发布于 2024-07-24 21:55:23 字数 1271 浏览 1 评论 0原文

ThreadMXBean 有两种检索线程时间使用情况的方法:

两者有什么区别?


更新 2:如果我能够链接到 javadoc,请不要引用它们 - 我已经阅读过它们。

更新:这里有一些代码,我试图用它们来了解这些时间的含义,但收效甚微:

ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();
threadMXBean.setThreadContentionMonitoringEnabled(true);
long mainThreadId = getMainThreadId(threadMXBean);

logTimes("Start", threadMXBean, mainThreadId);

URL url = new URL("https://hudson.dev.java.net");
URLConnection connection = url.openConnection();

connection.getContent();

logTimes("After loading", threadMXBean, mainThreadId);

输出为:

Start Tue Jun 16 16:13:40 EEST 2009 Cpu time : 80, user time: 60, waited: 0, blocked: 0
After loading Tue Jun 16 16:13:43 EEST 2009 Cpu time : 1,020, user time: 960, waited: 0, blocked: 0

因此 cpu 和用户时间之间的差异从 20 毫秒增加到 60 毫秒。 这是因为使用 HttpUrlConnection 确实包含一些网络 I/O 吗?

The ThreadMXBean has two methods for retrieving thread time usage:

What is the difference between the two?


Update 2: If I'm able to link to the javadocs, please don't quote them - I've read them already.

Update: here's some code which I tried to use to learn what these times mean, with little success:

ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();
threadMXBean.setThreadContentionMonitoringEnabled(true);
long mainThreadId = getMainThreadId(threadMXBean);

logTimes("Start", threadMXBean, mainThreadId);

URL url = new URL("https://hudson.dev.java.net");
URLConnection connection = url.openConnection();

connection.getContent();

logTimes("After loading", threadMXBean, mainThreadId);

and the output is:

Start Tue Jun 16 16:13:40 EEST 2009 Cpu time : 80, user time: 60, waited: 0, blocked: 0
After loading Tue Jun 16 16:13:43 EEST 2009 Cpu time : 1,020, user time: 960, waited: 0, blocked: 0

So the difference between cpu and user time increased from 20 to 60 milliseconds. Is that because using a HttpUrlConnection does include some network I/O?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

墨洒年华 2024-07-31 21:55:23

正如您链接到自己的 API 文档已经指出的那样

getThreadCpuTime

如果实现区分
用户模式时间和系统模式之间
时间,返回的CPU时间是
线程拥有的时间量
在用户模式或系统模式下执行。

如果 JVM 的实现区分用户模式和内核模式时间,则两个函数的结果可能会有所不同。

此外,该值仅精确到纳秒,并且如果偏移量> 则该值存在溢出问题。 2^63。 JVM 还必须支持测量当前线程的 CPU 时间并且必须启用它。

在 Win32 上,返回值应与从 GetThreadTimes 函数

getThreadUserTime() -> lpUserTime * 100 //或者像这样的

getThreadCpuTime() -> (lpKernelTime + lpUserTime) * 100 //或类似的内容

以及对 用户模式与内核模式

As the API docs you linked to yourself already point out

getThreadCpuTime

If the implementation distinguishes
between user mode time and system mode
time, the returned CPU time is the
amount of time that the thread has
executed in user mode or system mode.

If the implementation of the JVM distinguishes between user mode and kernel mode time there could be a difference in the results of the two functions.

Further the value is only precise to the nanosecond and the value has an overflow problem if the offset is > 2^63. The JVM must also support measuring the CPU time for the current thread and it must be enabled.

On Win32 the return values should be the same as the ones you get from the GetThreadTimes Function

getThreadUserTime() -> lpUserTime * 100 //or something like this

getThreadCpuTime() -> (lpKernelTime + lpUserTime) * 100 //or something like this

And a more clear reference to User Mode vs Kernel Mode

夜空下最亮的亮点 2024-07-31 21:55:23

javadoc 中对此进行了解释:-)。

获取ThreadCpuTime:
返回指定 ID 的线程的总 CPU 时间。

获取线程用户时间:
返回指定ID的线程在用户模式下执行的CPU时间。

不同之处在于 getThreadCpuTime 还包括线程使用 CPU 但不处于用户模式的时间。 这就像在设备驱动程序内部、轮询 I/O 或类似的事情。

It's explained in the javadocs :-).

getThreadCpuTime:
Returns the total CPU time for a thread of the specified ID.

getThreadUserTime:
Returns the CPU time that a thread of the specified ID has executed in user mode.

The difference is that getThreadCpuTime also includes time that the thread used the CPU but was not in user mode. That would be things like being inside a device driver, polling for I/O or similar.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文