1905 元起,最具價效比的 A15 手機!蘋果 iPhone SE 3 5G 渲染圖曝光:還是熟悉的味道
阿新 • • 發佈:2022-02-08
執行緒休眠
1.sleep(時間)指定當前執行緒阻塞的毫秒數
2.sleep存在異常InterruptedException
3.sleep時間達到後執行緒進入就緒狀態
4.sleep可以模擬網路延時,倒計時等。
5.每一個物件都有一個鎖,sleep不會釋放鎖
public class TestSleep {
public static void main(String[] args) {
tenDown();
}
//模擬倒計時
public static void tenDown(){
int num=10;
while (true){
try {
Thread.sleep(1000);
System.out.println(num--);
if(num<0){
break;
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
import java.text.SimpleDateFormat;
import java.util.Date;
public class TestSleep2 {
//顯示當前時間
public static void main(String[] args) {
Date date=new Date(System.currentTimeMillis());
while (true){
try {
Thread.sleep(1000);
System.out.println( new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date));
date=new Date(System.currentTimeMillis());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}