public class SleepThread extends Thread{
@Override
public void run() {
try {
while(true){
System.out.println("sleep start");
Thread.sleep(10000);
System.out.println("sleep end");
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public class ThreadTest {
/**
* @param args
*/
public static void main(String[] args) {
Thread t1=new SleepThread();
t1.start();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
t1.interrupt();
}
}
运行:
[quote]sleep start
java.lang.InterruptedException: sleep interrupted
at java.lang.Thread.sleep(Native Method)
at SleepThread.run(SleepThread.java:9)[/quote]