《Java高并發核心程式設計》 尼恩
1.5.2 線程Sleep操作
1.5.2 線程Sleep操作
public class ThreadTest1 {
public static final int SLEEP_GAP = 5000;
public static final int MAX_TURN = 50;
static class SleepThread extends Thread{
static int threadSepNumber = 1;
public SleepThread(){
super("sleepThread" + threadSepNumber);
threadSepNumber ++;
}
@Override
public void run() {
try {
for (int i = 1; i < MAX_TURN; i++){
System.out.println(getName() + ", 睡眠輪次 : " + i);
Thread.sleep(SLEEP_GAP);
}
} catch (InterruptedException e) {
e.printStackTrace();
System.out.println("中斷異常");
}
System.out.println("運作結束");
}
}
public static void main(String[] args) {
for (int i = 0; i < 5; i++){
Thread thread = new SleepThread();
thread.start();
}
System.out.println("main end");
}
}
結論
結論