All the code snippets above measure the approximate time elapsed from the moment the method is called until the method returns / throws an exception. Such methods are not intended for thread scheduling; they are suspended due to GC, etc.
Yes, some profilers will do a reasonable job.
If you are using Java 1.6, you can use JMX-based VM management and monitoring support. For example, you can find ThreadMXBean.getCurrentThreadCpuTime () . Calculating the difference of this value before and after the method call will give you:
"... the total processor time for the current thread in nanoseconds. The return value is nanosecond accuracy, but not necessarily the nanosecond accuracy. If the implementation differs in user mode time and system mode time, the return CPU time is the amount of time that the current thread has completed in the user mode or system mode. "
If your method spawns workflows, then your calculations should be more complex; -)
In general, I recommend using the java.lang.mangement package .
Dilum Ranatunga Mar 28 '09 at 16:10 2009-03-28 16:10
source share