《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");
}
}
结论
结论