最近在压测一个接口,使用如下方法获取时间差:
long start = System.currentTimeMillis();
object.methodinvoke();
long end = System.currentTimeMillis();
long cost= end - start;
打印的日志记录时间差,后来发现日志里出现了很多负数,感到很奇怪,查了资料,发现这个方法里面有坑!
来看看currentTimeMillis()这个方法的源码注释:
/**
* Returns the current time in milliseconds. Note that
* while the unit of time of the return value is a millisecond,
* the granularity of the value depends on the underlying
* operating system and may be larger. For example, many
* operating systems measure time in units of tens of
* milliseconds.
*
* <p> See the description of the class <code>Date</code> for
* a discussion of slight discrepancies that may arise between
* "computer time" and coordinated universal time (UTC).
*
* @