ITeye博客

关于Java thread的Interrupt, isInterrupt, interrupted

jay_kid · 2009-12-26 00:58 · JavaSE · 编程语言 · Markdown

在《Java网络编程》上看到一个例子, 说是用thread.interrupt()去关闭在等待的线程。不太明白,于是去查了一下Java的API。

有兴趣的同行们可以先看看API的内容,再看以下总结:

 

1. thread.isInterrupt()和Thread.interrupted()都返回当前线程interrupt的状态

  • thread.isInterrupt()是非静态函数,作用目标是“线程实例”,一般用法如下,
TestInterrupt t = new TestInterrupt();
t.start();
System.out.println(t.isInterrupt());

 

  • 而Thread.interrupted()是静态函数,作用目标是“当前线程”
System.out.println(Thread.interrupted());

 并且它会把当前线程的interrupt状态“复位”,假设当前线程的isInterrupt状态为true,它会返回true,但过后isInterrupt的状态会复位为false。之后调用(Thread)t.isInterrupt或Thread.interrupted都会返回false

 

 

2. interrupt()被调用后有几个可能,下面挑几种常见的归类一下:

 

线程阻塞的情况下:

	public void run() {

		while (!Thread.currentThread().isInterrupted()) {
			try{
				Thread.sleep(1000);
			} catch (InterruptedException ie){
				ie.printStackTrace();
			}
		}
	}

 如上所示,如果线程运行到Thread.sleep(1000)时,被其他线调用了该线程的interrupt()的方法,它将会进入catch的段落里面,然后再运行while (!Thread.currentThread().isInterrupted()) 时退出run, 线程被销毁。

 

 

 

线程正常运行的情况下:

  • 该线程不受影响,继续运行,但该线程的isInterrupt状态将被设置成true
	public void run() {

		while (!Thread.currentThread().isInterrupted()) {
			try{
				//A: 无阻塞的代码.......
				......
				//B: 被调用interrupt.......
				......
				//C: 无阻塞的代码.......
			} catch (InterruptedException ie){
				ie.printStackTrace();
			}
		}
	}

 如上所示, 当线程正常运行时,在运行到B点时被调用了interrupt(), 此时该线程将继续正常运行,但isInterrupt的状态会被设为true,当做完 A,B,C的代码时,再进入while (!Thread.currentThread().isInterrupted()) 时,该线程被销毁。假如while的检查条件改为(true),该线程不会受影响,将会一直运行下去。

 

 

 

先写到这里,欢迎大家留言,如果有说得不对的地方,一起讨论 ^________^

评论(8)

  1. #8 楼 · real_yqdt · 2015-09-08 15:11

    :arrow: :idea: :?: :?: :?: :?: :idea: :idea: :idea: :D :) :) :) :) :( :( :o :o :lol: 8) :? :shock: :x :P :oops: :cry: :wink: :roll: :twisted: :evil: :!: :?: :idea: :arrow:

  2. #7 楼 · 二当家的 · 2015-08-30 11:06

    while需要写在try里面

  3. #6 楼 · sampras87 · 2014-08-18 10:56

    mojunbin writes:

    [code=java] public void run() {

    while (!Thread.currentThread().isInterrupted()) {  
        try{  
            Thread.sleep(1000);  
        } catch (InterruptedException ie){  
            ie.printStackTrace();  
        }  
    }  
    

    } [/code]

    这段代码确实会[color=red]无限循环[/color]..

    假设有两个线程:t1:代表run()方法所在线程;main:代表主线程.

    对于一个新生线程,其中断状态位为"false".

    当t1睡眠时,main线程调用t1.interrupt(),t1的中断状态位变成"true",t1脱离阻塞状态,并抛出InterruptedException异常,同时[color=red]重置中断状态位为"false"[/color].

    之后Thread.currentThread().isInterrupted()仍然[color=red]返回false[/color],因此循环无法退出.

    这个try语句的确应该把while也try住,才能如预期的运行。

  4. #5 楼 · boaixiaohai · 2014-08-11 15:58

    mojunbin writes:

    [code=java] public void run() {

    while (!Thread.currentThread().isInterrupted()) {  
        try{  
            Thread.sleep(1000);  
        } catch (InterruptedException ie){  
            ie.printStackTrace();  
        }  
    }  
    

    } [/code]

    这段代码确实会[color=red]无限循环[/color]..

    假设有两个线程:t1:代表run()方法所在线程;main:代表主线程.

    对于一个新生线程,其中断状态位为"false".

    当t1睡眠时,main线程调用t1.interrupt(),t1的中断状态位变成"true",t1脱离阻塞状态,并抛出InterruptedException异常,同时[color=red]重置中断状态位为"false"[/color].

    之后Thread.currentThread().isInterrupted()仍然[color=red]返回false[/color],因此循环无法退出.

    也不是肯定死循环,要看运气,不过这段代码确实不能用.

  5. #4 楼 · cnlinkin · 2012-06-07 16:46
    
    public void run() {  
      
        while (!Thread.currentThread().isInterrupted()) {  
            try{  
                Thread.sleep(1000);  
            } catch (InterruptedException ie){  
                System.out.printf("%s caught interrupt signal...", this);
                Thread.currentThread().interrupt();   
            }  
        }
       System.out.printf("thread %s done...", this);  
    } 
    
    
    
  6. #3 楼 · mojunbin · 2012-04-21 12:12
    
    public void run() {  
      
        while (!Thread.currentThread().isInterrupted()) {  
            try{  
                Thread.sleep(1000);  
            } catch (InterruptedException ie){  
                ie.printStackTrace();  
            }  
        }  
    } 
    
    

    这段代码确实会无限循环..

    假设有两个线程:t1:代表run()方法所在线程;main:代表主线程.

    对于一个新生线程,其中断状态位为"false".

    当t1睡眠时,main线程调用t1.interrupt(),t1的中断状态位变成"true",t1脱离阻塞状态,并抛出InterruptedException异常,同时重置中断状态位为"false".

    之后Thread.currentThread().isInterrupted()仍然返回false,因此循环无法退出.

  7. #2 楼 · habzyhs · 2011-09-05 17:40

    乱讲。 你去实践下,写个代码。再敲好不?

  8. #1 楼 · jackybobo · 2011-03-10 18:55
    
    	public void run() {
    
    		while (!Thread.currentThread().isInterrupted()) {
    			try{
    				Thread.sleep(1000);
    			} catch (InterruptedException ie){
    				ie.printStackTrace();
    			}
    		}
    	}
    
    
    

    这个不会退出吧??

评论功能维护中,当前仅展示历史评论。