这样试试:public class test3 extends Thread{
public void run(){
System.out.println("1");
}
public static void main(String args[]){
test3 a = new test3();
a.start();
try{
a.sleep(3000);
}
catch(InterruptedException e){
System.out.println("2");
}
}
}仅供参考!
调试欢乐多
sleep一定要是当前线程,要不然根本就说不通.
需要调用interrupt方法才行。
只有这个线程处于sleep()状态,别的线程才有可能调用他的interrupt()方法,
所以当这个线程sleep时,要捕获interruptedException异常。public class test3 extends Thread{
public void run(){
System.out.println("1");
while (true){
try{
this.sleep(5);
}
catch(InterruptedException e){
System.out.println("2");
System.exit(0) ;
}
}
//System.out.print("3") ;
}
public static void main(String args[]){
test3 a=new test3();
a.start();
try{
a.sleep(3000) ;
a.interrupt();
}
catch(InterruptedException e){
System.out.println("3");
}
}
}