public class Test extends Thread {
public void run() {
try {
sleep(3000);
} catch (InterruptedException e) {
System.out.println("中断");
}
System.out.println("继续执行");
}

public static void main(String[] args) {
Test t = new Test();
Thread th = new Thread(t);
th.start();
th.interrupt();
for (int i = 0; i < 1000; i++)
;
System.out.println("中止");
}
}为什么要我调用了interrupt()这个方法后,
System.out.println("继续执行");这行代码还会打印出来呢?
是因为它我调用interrupt()后出现异常,所以异常后面的代码会执行吗?

解决方案 »

  1.   

    为什么不出来?catch中你又没有return!
      

  2.   

    我明白楼主为什么以为:System.out.println("继续执行");会不执行了只是try{}语句里面如果产生了异常,try{}语句体内剩下的语句才不会被执行,上面的System.out.println("继续执行");是在try...catch语句后面,当然是会执行的啦
      

  3.   

    如果是这样:
    public class TestThreadInter extends Thread
    {
    public void run()
    {
    try
    {
    sleep(3000);
    System.out.println("继续执行");
    }
    catch (InterruptedException e)
    {
    System.out.println(e.getMessage());
    } }
    public static void main(String[] args)
    {
    TestThreadInter t=new TestThreadInter();
    Thread th=new Thread(t);
    th.start();
    th.interrupt();//中断该线程
    for (int i=0;i<1000;i++);
    System.out.println("中止");
    }
    }
    这个System.out.println("继续执行");就不会被执行了
      

  4.   

    我知道catch后边的代码是会执行的而我想明白的是我已经中断线程不让它在执行了,
    难到中断的意思只是告诉它你可能不充许执行了?
    但线程还是把本次执行完毕吧
      

  5.   

    try - catch有点像一个人做了坏事被关了起来(catch),但是一旦刑满释放,他的正常权利照样不能剥夺啊
      

  6.   

    那在这个程序里,我interrupt还有什么作用呢?
    难到我中断这个线程,
    try catch不是通过我这个线程来执行的,
    还有其它的线程来控制try catch语句吗?
      

  7.   

    事实上interrupt是不能终止一个线程,目前没有很好的办法直接终止一个线程.
    当然stop方法是可以的,但是这个方法容易造成死锁,已经强烈建议不要使用了.interrupt方式只是置一个标志位而已,当然如果线程本身在wait或者sleep就会抛出一个异常.
      

  8.   

    interrupt方式只是置一个标志位而已,当然如果线程本身在wait或者sleep就会抛出一个异常.设置这个标志有意义是什么呢?
    如果我没有sleep()这个方法,而在run()方法里面有很长的一段运行时间,
    public void run() {
       while (flag) {}
    }我在主程序里调用interrupt方面,对run有什么作用吗?设置这个标记能告诉jvm什么时候中止线程还是一直在等到我的flag为false时才退出呢?
      

  9.   

    If this thread is blocked in an invocation of the wait(), wait(long), or wait(long, int) methods of the Object class, or of the join(), join(long), join(long, int), sleep(long), or sleep(long, int), methods of this class, then its interrupt status will be cleared and it will receive an InterruptedException.
      

  10.   

    不太懂,但是我觉得这个很有道理:
    ----------------------------------------------- 回复人: treeroot(旗鲁特) ( ) 信誉:106  2005-11-22 13:43:00  得分: 0  
     
     
       事实上interrupt是不能终止一个线程,目前没有很好的办法直接终止一个线程.
    当然stop方法是可以的,但是这个方法容易造成死锁,已经强烈建议不要使用了.interrupt方式只是置一个标志位而已,当然如果线程本身在wait或者sleep就会抛出一个异常.  
     
      

  11.   

    我是说 nighthawk(我们孤单,我们并肩)
      

  12.   

    try{
      while(!interrupted()&&has more work to do){
        do more work...
      }
    }catch(InterruptedException e){
      //interrupted while sleep
    }
    一个线程应该不时地检查自己是否应该退出,如果被别人强制终止,则会带来意想不到的灾难。
    可以用interrupted()来检查,别人调用了interrupt(),则他的interrupted()就返回true
      

  13.   

    回复人: treeroot(旗鲁特) ( ) 信誉:106  2005-11-22 13:43:00  得分: 0  
     
     
       事实上interrupt是不能终止一个线程,目前没有很好的办法直接终止一个线程.
    当然stop方法是可以的,但是这个方法容易造成死锁,已经强烈建议不要使用了.interrupt方式只是置一个标志位而已,当然如果线程本身在wait或者sleep就会抛出一个异常.
    treeroot(旗鲁特)说的对,interrupt是不能真正终止一个线程的nighthawk(我们孤单,我们并肩) ( ) 信誉:76  2005-11-22 15:12:00  得分: 0  
     
     
       If this thread is blocked in an invocation of the wait(), wait(long), or wait(long, int) methods of the Object class, or of the join(), join(long), join(long, int), sleep(long), or sleep(long, int), methods of this class, then its interrupt status will be cleared and it will receive an InterruptedException.
      
     
    nighthawk(我们孤单,我们并肩) 这段JDK描述同样,当调用sleep()方法后,如果调用interrupt之后会抛出异常昨天我资料说,当sleep()抛出异常后,interrupt设置的终止线程标致将同样清除?
    我觉得可能就是这个原因导致后面还可以执行。不知道对不对,请理解的人给解释一下。