线程休眠sleep

package edu.wtbu;

import java.text.SimpleDateFormat;
import java.util.Date;

//模拟倒计时
public class Demo02 {
public static void tenDown() throws InterruptedException {
int num=10;
while (true){
//sleep放大问题的发生性
Thread.sleep(1000);
System.out.println(num--);
if (num<=0){
break;
}
}
}

public static void main(String[] args) {
// try {
// tenDown();
// } catch (InterruptedException e) {
// throw new RuntimeException(e);
// }

//打印当前系统时间
Date startTime = new Date(System.currentTimeMillis());//获取系统当前时间

while (true){
try {
Thread.sleep(1000);
System.out.println(new SimpleDateFormat("HH:mm:ss").format(startTime));
startTime=new Date(System.currentTimeMillis());
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}

}
}
posted @ 2023-03-18 09:52  惊鸿宴远赴人间  阅读(15)  评论(0)    收藏  举报