您的位置:首页 > 编程语言

在代码片段中添加时间间隔

2015-09-10 17:15 302 查看
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());
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: