【发布时间】:2014-02-25 07:42:35
【问题描述】:
考虑如下代码sn-ps
class time implement Runnable{
long t=0L;
public void run(){
try{while(true){Thread.sleep(1000);t++;/*show the time*/}}catch(Throwable t){}
}
}
////
long long t=0L;
void* time(void* a){//pthread thread start
sleep(1);t++;//show the time
}
我在一些教程中读到,在 Java 中 Thread.sleep(1000) 不完全是 1 秒,如果当时系统很忙,可能会更多,然后操作系统切换到线程较晚。
问题:
这种情况到底是真的还是假的?
对于本机 (C/C++) 代码,这种情况是否相同?
在应用程序中计算秒数的准确方法是什么?
【问题讨论】:
-
即使你不睡觉,只是轮询
System.nanoTime(),你也会在一台安静的机器上看到长达几毫秒的抖动。 vanillajava.blogspot.co.uk/2013/07/…
标签: java c++ c multithreading pthreads