1 回答
TA贡献2037条经验 获得超6个赞
我建议你使用TimeUnit.SECONDS.sleep(1). 看一下代码:
public class Eieruhr {
private int x;
public Eieruhr(int x) {
this.x = x;
}
public static void main(String[] args) throws InterruptedException {
Eieruhr eu = new Eieruhr(10);
eu.start();
}
public void start() throws InterruptedException {
for (int i = 0; i < x; i++) {
TimeUnit.SECONDS.sleep(1);
System.out.println(new Date() + " tick - " + i);
}
}
}
输出:
Sat Oct 19 15:11:37 EEST 2019 tick - 0
Sat Oct 19 15:11:38 EEST 2019 tick - 1
Sat Oct 19 15:11:39 EEST 2019 tick - 2
Sat Oct 19 15:11:40 EEST 2019 tick - 3
Sat Oct 19 15:11:41 EEST 2019 tick - 4
Sat Oct 19 15:11:42 EEST 2019 tick - 5
添加回答
举报