开发者

How to measure Java thread execution time?

开发者 https://www.devze.com 2022-12-29 03:39 出处:网络
I want to measure thread execution开发者_如何学C time in 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.J

I want to measure thread execution开发者_如何学C time in 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:

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.


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. 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号