package com.Java;标签:休眠,System,Date,num,线程,date,sleep,public From: https://www.cnblogs.com/fc666/p/17177945.html
import java.text.SimpleDateFormat;
import java.util.Date;
//每个对象都有一个锁 sleep不会释放锁
public class TestSleep {
public static void main(String[] args) {
try {
// tendown();
date();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
//10秒倒计时
public static void tendown() throws InterruptedException {
int num = 10;
while (true) {
Thread.sleep(1000);
System.out.println(num--);
if (num <= 0) {
break;
}
}
}
//每秒更新时间
public static void date() throws InterruptedException {
Date date = new Date(System.currentTimeMillis()); //获取系统当前时间
while (true) {
Thread.sleep(1000);
System.out.println(new SimpleDateFormat("HH:mm:ss").format(date)); //打印格式
date = new Date(System.currentTimeMillis()); //更新当前时间
}
}
}