class MyThread implements Runnable{
public void run(){
System.out.println(Thread.currentThread().getName()+"  进入run()方法");
try{
Thread.sleep(2000);
System.out.println(Thread.currentThread().getName()+"  休眠正常结束");
}catch(InterruptedException e){
System.out.println(Thread.currentThread().getName()+"  休眠被终止");
return;
}
System.out.println(Thread.currentThread().getName()+"  run()正常退出");
}
}
public class InterruptThreadDemo1{
public static void main(String args[]){
MyThread mt=new MyThread();
Thread t=new Thread(mt,"线程");
t.start();
try{
Thread.sleep(10000);
}catch(Exception e){}
t.interrupt();
}
}
以上是代码1,下面是代码2.两者睡眠时间调换了一下,运行结果完全不同,求大神解析
class MyThread implements Runnable{
public void run(){
System.out.println(Thread.currentThread().getName()+"  进入run()方法");
try{
Thread.sleep(10000);
System.out.println(Thread.currentThread().getName()+"  休眠正常结束");
}catch(InterruptedException e){
System.out.println(Thread.currentThread().getName()+"  休眠被终止");
return;
}
System.out.println(Thread.currentThread().getName()+"  run()正常退出");
}
}
public class InterruptThreadDemo2{
public static void main(String args[]){
MyThread mt=new MyThread();
Thread t=new Thread(mt,"线程");
t.start();
try{
Thread.sleep(2000);
}catch(Exception e){}
t.interrupt();
}
}线程 中断

解决方案 »

  1.   

    帮你代码高亮格式调整了下 同样期待解答class MyThread implements Runnable{
    public void run(){
    System.out.println(Thread.currentThread().getName()+"  进入run()方法");
    try{
    Thread.sleep(2000);
    //Thread.sleep(10000);
    System.out.println(Thread.currentThread().getName()+"  休眠正常结束");
    }catch(InterruptedException e){
    System.out.println(Thread.currentThread().getName()+"  休眠被终止");
    return;
    }
    System.out.println(Thread.currentThread().getName()+"  run()正常退出");
    }
    }
    public class InterruptThreadDemo1{
    public static void main(String args[]){
    MyThread mt=new MyThread();
    Thread t=new Thread(mt,"线程");
    t.start();
    try{
    Thread.sleep(10000);
    //Thread.sleep(2000);
    }catch(Exception e){}
    t.interrupt();
    }
    }
      

  2.   


    class MyThread implements Runnable{
    public void run(){
    System.out.println(Thread.currentThread().getName()+"  进入run()方法");
    try{
    Thread.sleep(10000);
    System.out.println(Thread.currentThread().getName()+"  休眠正常结束");
    }catch(InterruptedException e){
    System.out.println(Thread.currentThread().getName()+"  休眠被终止");
    return;
    }
    System.out.println(Thread.currentThread().getName()+"  run()正常退出");
    }
    }
    public class InterruptThreadDemo2{
    public static void main(String args[]){
    MyThread mt=new MyThread();
    Thread t=new Thread(mt,"线程");
    t.start();
    try{
    Thread.sleep(2000);
    }catch(Exception e){}
    t.interrupt();
    }
      

  3.   

    demo1,你的t在睡觉,主线程里有个睡觉比t还厉害的,因此,在t.interrupt();之前,t已经睡醒了。
    demo2,你的t正在睡觉,这次睡的比较凶,比主线程睡的还久,然后主线程睡醒了,就突然给你t.interrupt();t的睡眠被打断了,就抛异常了。
      

  4.   

    这个问题很简单
    第一个例子你的主线程睡了10000,t线程虽然睡眠了2000但,是完全有时间可以执行结束。当t线程已结束再调用t.interrupt()已不会影响你的打印结果。
    第二个例子,你把时间换了一下,当主线程执行到t.interrupt()时t线程还处于睡眠状态就会抛出打断异常,就和每一个例子的打印结果不一样了。
    解释完毕,要给分哦。