mport java.util.Date;//使用异常结束线程  使用接口 Runnable 创建线程
import java.lang.Thread;public class TestInterrupt
{
    public static void main(String[] args)
    {
    MyThread3 mt3 = new MyThread3();
    Thread t1 = new Thread(mt3);
    t1.start();
    try
    {
        Thread.sleep(10000);
    } catch (InterruptedException e)
    {
    }//就是在这里为什么不把t1.interrupt()写在这个括号里面 抛出异常后不是应当执行//catch括号里面的语句么  
//而且 分支线程 return语句 写在了catch的花括号里面    System.out.println("使用异常结束");
    t1.interrupt();
    }}class MyThread3 implements Runnable
{
    public void run()
    {
    while (true)
    {
        System.out.println("====" + new Date() + "====");
        try
        {
        Thread.sleep(1000);
        } catch (InterruptedException e)
        {
        return;
        }
    }
    }
}

解决方案 »

  1.   

    t1.interrupt()是去执行中断,导致MyThread3 中产生InterruptedException ,而不是TestInterrupt中的
      

  2.   

    t1.interrupt() 意思是:
       让线程t1中断休眠(因调用sleep,wait,jion等方法产生休眠),并且抛出InterruptedException异常。PS: 如果线程t1还没执行到sleep(),wait(),jion()方法,就在其他的线程中调用了t1.interrupt()方法,那么中断仍然是有效的,当线程t1执行到sleep(),wait(),jion()方法时,立刻就会抛出InterruptedException异常,然后线程t1是继续执行还是返回,就要看用户自己在catch语句里怎么控制了。