Java中线程用户时间和线程cpu时间的区别 [英] Difference between thread user time and thread cpu time in Java
问题描述
ThreadMXBean有两种检索线程时间用法的方法:
The ThreadMXBean has two methods for retrieving thread time usage:
- getThreadUserTime
- getThreadCpuTime
两者有什么区别?
更新2 :如果我能够链接到javadocs,请不引用它们 - 我已经阅读过了。
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);
,输出为:
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吗?
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?
推荐答案
因为您链接到自己的API文档已经指出
As the API docs you linked to yourself already point out
getThreadCpuTime
如果实现区分$用户模式时间和系统模式之间的b
$ b时间,返回的CPU时间是线程在用户模式或系统模式下执行
的
时间。
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.
如果JVM的实现区分了用户模式和内核模式时间,那么两个函数的结果可能会有所不同。
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.
此外,该值仅精确到纳秒,如果偏移> 2 ^ 63,则该值具有溢出问题。 JVM还必须支持测量当前线程的CPU时间,并且必须启用它。
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.
在Win32上,返回值应与从 GetThreadTimes函数
On Win32 the return values should be the same as the ones you get from the GetThreadTimes Function
getThreadUserTime()
- > lpUserTime * 100
//或类似的东西
getThreadUserTime()
-> lpUserTime * 100
//or something like this
getThreadCpuTime ()
- > (lpKernelTime + lpUserTime)* 100
//或类似的东西
getThreadCpuTime()
-> (lpKernelTime + lpUserTime) * 100
//or something like this
更明确地参考用户模式与内核模式
这篇关于Java中线程用户时间和线程cpu时间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!