0

我熟悉

http://download.oracle.com/javase/6/docs/api/java/lang/management/ThreadMXBean.html

我想测试\配置文件\测量线程获得了多少 CPU 时间。我将以下内容添加到我的线程构造函数中:

ManagementFactory.getThreadMXBean().setThreadCpuTimeEnabled(true);
if(!ManagementFactory.getThreadMXBean().isCurrentThreadCpuTimeSupported())
    System.err.println("measure is not supported.");
ManagementFactory.getThreadMXBean().setThreadContentionMonitoringEnabled(true);
if(!ManagementFactory.getThreadMXBean().isCurrentThreadCpuTimeSupported())
    System.err.println("contention is not supported.");

然后当线程完成它的执行时,我执行:

long tID = Thread.currentThread().getId();
long execTime = ManagementFactory.getThreadMXBean().getThreadCpuTime(tID);

但是对于我的大多数线程,我绝对确定它们确实占用了 CPU,我得到的执行时间为零(execTime == 0)。

我如何测量我的线程?上述过程中可能存在什么问题?

先谢谢了。

4

1 回答 1

0

看看Thread Top,一切都搞定了,无需重新发明轮子。

于 2011-06-23T10:08:11.900 回答