sleep 与interrupt方法的应用
import java.util.*;
public class TestInterrupt {
public static void main(String[] args) {
MyThread thread = new MyThread();
thread.start();
try {Thread.sleep(10000);} //main主线程休眠10秒钟
catch (InterruptedException e) {}
thread.interrupt(); //主线程10钟后执行thread的interrupt的方法 thread就会抛InterruptedException异常。
}
}
class MyThread extends Thread {
public void run(){
while(true){
System.out.println("==="+new Date()+"===");
try {
sleep(1000); //暂时休眠1秒钟
} catch (InterruptedException e) {
return;
}
}
}
}