线程休眠
- sleep(时间)指定当前线程阻塞的毫秒数
- sleep存在异常InterruptedException
- sleep时间达到后线程进入就绪状态
- sleep可以模拟网络延时,倒计时等
- 每一个对象都有一个锁,sleep不会释放锁
public class TestSleep {
public static void main(String[] args) {
timeDown();
}
public static void timeDown(){
int i=10;
while (true){
System.out.println("倒计时"+i--);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
if(i<=0){
break;
}
}
}
}