How to measure Java thread execution time?

和自甴很熟 提交于 2019-12-19 16:53:21

问题


I want to measure thread execution 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.


回答1:


This is nontrivial. Your best bet is to use a profiler which can accumulate the actual CPU cycles used.




回答2:


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());



回答3:


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.



来源:https://stackoverflow.com/questions/2780761/how-to-measure-java-thread-execution-time

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!