我没有发现Thread.sleep是准确的。事实上,我发现它似乎比它承诺的睡眠要少。
配置为休眠 1 毫秒时的结果:
********* Average elapsed time = 957 micro seconds
以下是我的测试:
public static void main(String[] args) throws InterruptedException {
List<Long> list = new ArrayList<Long>();
int i= 0;
while(i <=100){
long authStartTime = System.nanoTime();
Thread.sleep(1);
long elapsedTime = System.nanoTime() - authStartTime;
System.out.println("*****"+elapsedTime/1000+ " mics");
list.add(elapsedTime);
i++;
}
long sum=0;
for(long l : list){
sum = sum + l;
}
System.out.println("********* Average elapsed time = "+sum/list.size()/1000+" micro seconds");
System.exit(0);
}