public class Thread5 {
public static void main(String[] args) {
Thread5_1 t5 = new Thread5_1();
t5.start();
t5.interrupt();
Thread.currentThread().interrupt();
System.out.println(" 1 "+Thread.interrupted());
System.out.println(" 2 "+Thread.interrupted());
System.out.println(" 3 "+t5.isInterrupted());
System.out.println(" 4 "+t5.isInterrupted());
}
}
class Thread5_1 extends Thread{
public void run(){
for(int i=0;i<100;i++){
System.out.print(i+" ");
}
}
}
输出答案(run方法里的结果没贴出来):
1 true
2 false
3 true
4 true
分析:
1、interrupt分析
public void interrupt() {
if (this != Thread.currentThread())
checkAccess();
synchronized (blockerLock) {
Interruptible b = blocker;
if (b != null) {
interrupt0(); // Just to set the interrupt flag
b.interrupt(this);
return;
}
}
interrupt0();
}
checkAccess方法是检查是否具有更改线程状态的权限的,这里不做具体讨论,感兴趣可自己查看源码
interrupt0 这个方法也是个native方法,从这个方法的注释可以看出,这个方法只是做了一个中断标志。
主要是下面的 interrupt看源码
/**
* Marks the beginning of an I/O operation that might block indefinitely.
*
* <p> This method should be invoked in tandem with the {@link #end end}
* method, using a <tt>try</tt> ... <tt>finally</tt> block as
* shown <a href="#be">above</a>, in order to implement asynchronous
* closing and interruption for this channel. </p>
*/
protected final void begin() {
if (interruptor == null) {
interruptor = new Interruptible() {
public void interrupt(Thread target) {
synchronized (closeLock) {
if (!open)
return;
open = false;
interrupted = target;
try {
AbstractInterruptibleChannel.this.implCloseChannel();
} catch (IOException x) { }
}
}};
}
blockedOn(interruptor);
Thread me = Thread.currentThread();
if (me.isInterrupted())
interruptor.interrupt(me);
}
太复杂,直接看注释吧,in order to implement asynchronous
closing and interruption for this channel.
说的很清楚,为了关闭和中断这个channel,上面同时写了,Marks the beginning of an I/O operation that might block indefinitely.注意用的是might,翻译过来:标记这个io操作可能无限期阻塞,说白了就是标志这个线程,可能要中断了。并没有立刻中断,可能还有其他的操作,只是可能,因为线程的中断不是循环那样,一个break,return就可以停止,这也是为什么stop等方法被慢慢取缔,就是因为太暴力了,和谐的社会嘛,sorry,扯远了,言归正传。
2、interrupted
先看源码:
public static boolean interrupted() {
return currentThread().isInterrupted(true);
}
从源码中可以看到,该方法是静态方法,返回的结果是当前线程调用isInterrupted方法的结果。那么来看isInterrupted
/**
* Tests if some Thread has been interrupted. The interrupted state
* is reset or not based on the value of ClearInterrupted that is
* passed.
*/
private native boolean isInterrupted(boolean ClearInterrupted);
本地方法,来看注释,简单翻译下:检查线程是否已经被中止,中止状态会被重置,不依赖ClearInterrupted 的值,它将被忽略。
注意黑体字,
现在来解释:第一个是true 是因为 Thread.currentThread().interrupt();这里取了当前线程,这时候打印的是true,其实它只是检查到了这个线程具有中止的标志,所以才打印了true,实际上并没有立刻停止,不然main方法后面的就无法执行了。这也证明了 上面说的interruptor.interrupt(me);
第二个是false是因为清除了中断标志,所以,它才打印true。
本人新手:欢迎大家一起交流java,共同进步, QQ群:282034885
再来分析3,4结果,有了上面的理论,就容易多了,直接看源码
/**
* Tests whether this thread has been interrupted. The <i>interrupted
* status</i> of the thread is unaffected by this method.
*
* <p>A thread interruption ignored because a thread was not alive
* at the time of the interrupt will be reflected by this method
* returning false.
*
* @return <code>true</code> if this thread has been interrupted;
* <code>false</code> otherwise.
* @see #interrupted()
* @revised 6.0
*/
public boolean isInterrupted() {
return isInterrupted(false);
}
注释里写了,Tests whether this thread has been interrupted. The interrupted
*status of the thread is unaffected by this method.
只是检查这个线程是否具有中断标志位,这个线程状态并不会被这个方法影响。
那很容易理解为什么最后两个结果一样了。至于为什么是true上面已经解释了。