I'm not 100% sure, but I think that's a correct way to make a best effort time measurement for a specific
thread, excluding the time that other treads take in the mean time. However, it includes the time that the thread has spent in system space (calls to the kernel). If you only want the time it has spent inside your application,
you should use
getThreadUserTime().
Note that
ThreadMXBean has
getCurrentThreadCpuTime() and
getCurrentThreadUserTime(), which are more convenient to call than what you're doing.