1、在不改变最初的代码结构情况下,最简单的方法是:直接在想休眠的地方添加Thread.sleep(1000l);
如:
public class test {
public static void main(String[] args) {
for(int i=0;i<10;i++){
System.out.println(System.nanoTime());
try {
Thread.sleep(1000l);//休眠1秒
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
2、用 java的定时器Timer实现
public class test {
public static void main(String[] args) {
java.util.Timer t = new java.util.Timer();
t.schedule(new Task(),0,1000);
}
}
class Task extends java.util.TimerTask {
@Override
public void run() {
System.out.println(System.nanoTime());
}
}