日期:2014-05-20  浏览次数:20594 次

这是线程问题还是对象的问题
class MyThread9 implements Runnable{
public void run(){
System.out.println("1、进入run方法");
try{
Thread.sleep(10000);                      //休眠10s
System.out.println("2、已经完成休眠");
}catch(Exception e){
System.out.println("3、休眠被终止");
return;                                   //让程序返回被调用处
}
System.out.println("4、方法正常结束");
}
}
public class ThreadInterruptDemo {

public static void main(String[] args) {
MyThread9 mt = new MyThread9();
Thread t =new Thread(mt,"线程");
t.start();
try{
Thread.sleep(2000);                       //稍微停2s再继续中断
}catch(Exception e){

}
t.interrupt();                               //中断线程执行

}

}

这个的结果是
1、进入run方法
3、休眠被终止
当我改成这样的时候
class MyThread9 implements Runnable{
public void run(){
System.out.println("1、进入run方法");
try{
Thread.sleep(10000);                      //休眠10s
System.out.println("2、已经完成休眠");
}catch(Exception e){
System.out.println("3、休眠被终止");
return;                                   //让程序返回被调用处
}
System.out.println("4、方法正常结束");
}
}
public class ThreadInterruptDemo {

public static void main(String[] args) {
MyThread9 mt = new MyThread9();
new Thread(mt,"线程").start();;
try{
Thread.sleep(2000);                       //稍微停2s再继续中断
}catch(Exception e){

}
new Thread(mt,"线程").interrupt();                               //中断线程执行