十四、线程休眠
作者:互联网
- sleep(时间)指定当前线程阻塞的毫秒数;
- sleep存在异常InterruptedException;
- sleep时间达到后线程进入就绪状态;
- sleep可以模拟网络延时,倒计时等。
- 每一个对象都有一个锁,sleep不会释放锁;
倒计时Demo:
public class ThreadSleep2 {
public static void main(String[] args) {
int i =10;
try {
cutDown(i);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
//倒计时
public static void cutDown(int i) throws InterruptedException {
while (true){
Thread.sleep(1000);
if (i>=0){
System.out.println(i--);
}else break;
}
}
}
标签:int,InterruptedException,倒计时,休眠,线程,sleep,十四,public 来源: https://www.cnblogs.com/epiphany8/p/16271451.html