简体   繁体   English

如何衡量Java线程执行时间?

[英]How to measure Java thread execution time?

I want to measure thread execution time in Java. 我想用Java测量线程执行时间。 Now I'm monitoring thread start and end times, but I think it's not so accurate because thread could be suspended during it execution. 现在我正在监视线程的开始和结束时间,但我认为它不是那么准确,因为线程可能在执行期间被挂起。

Java MXBeans can provide per-thread CPU time: Java MXBeans可以提供每线程CPU时间:

import java.lang.management.ManagementFactory;
import java.lang.management.ThreadMXBean;

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

This is nontrivial. 这非常重要。 Your best bet is to use a profiler which can accumulate the actual CPU cycles used. 最好的办法是使用可以累积实际CPU周期的分析器。

Use a profiling solutions that supports multiple metrics. 使用支持多个指标的分析解决方案。 We call them (metrics) meters in our product supporting standard ones like CPU time, blocking time, waiting time even custom meters based on key performance indicators. 我们称他们的产品为(指标)米,支持标准的产品,如CPU时间,阻塞时间,等待时间,甚至是基于关键性能指标的定制仪表。 I assume you would also like to see what exactly a thread is doing which in that case you definitely need to consider a product rather than a home grown ad hoc solution. 我假设你也想看看一个线程到底在做什么,在这种情况下你肯定需要考虑一个产品而不是一个本土的ad hoc解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM