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