interrupt是给线程打上一个状态,意思是告诉线程:童鞋,你要结束了。但是线程不会在任意地方就返回,也没有必要任意地方返回,因为大部分的操作都很快就完成了,只有小部分还需要花上不知道多长时间的操作,才需要被中断,这个长时间的操作,就是阻塞,如磁盘io阻塞、网络io阻塞、同步阻塞等等,这些API调用的时候都要捕获异常就是这个原因,一旦被打上interrupt状态以后,线程执行到可中断阻塞(有些中断是不可以被中断的)的时候,就会抛出异常并且结束当前的操作,然后重置状态

解决方案 »

  1.   

    还是设置boolean型isCancelled好些,很安全
      

  2.   


    interrupt是操作系统级别的支持
      

  3.   


    interrupt是操作系统级别的支持恩,实际使用中还是设置boolean标记好些
      

  4.   

    线程里面有个GUI窗口怎么办?要是有50个窗口,那么50个县城都不退出?
      

  5.   

    The interrupt method of class Thread never helps you terminate immediately the running of the thread.It only sets a flag which you can make use of to make logic judgement in the code.In order to implement the fuction of terminating the thread after calling the interrupt method,you can refer to the following code:
    public static void main(String[] args) throws Exception { 

    Thread t1 = new Thread(new Runnable(){ @Override
    public void run() {
    System.out.println("task start...");

    for( int k = 0;k<100;k++){
    try {
    Thread.sleep(1000);
    } catch (InterruptedException e) {
    e.printStackTrace();
    }

    if(Thread.currentThread().isInterrupted()){
    System.out.println("interrupt");
    break; // exit the loop
    }else{
    System.out.println("k:"+k);
    }

    }

    System.out.println("task end ...");
    }
    });

    t1.start();

    Thread.sleep(5000);

    t1.interrupt();

    Thread.sleep(100000);
    }
      

  6.   

    线程里面有个GUI窗口怎么办?
      

  7.   

    Check whether the thread has been interrupted in the run method,if so, mannually write some codes to close your GUI window.
    About how to terminate a JFrame window, please refer to relevant API.
    Any questions, please let me know.
      

  8.   

    the relevant question is how to terminate a JFrame window. the dispos() method ? 
      

  9.   

    哈哈哈,实验了,就是.dispose();!