interrupt()只是改变中断状态而已.in<wbr style="line-height:25px">terrupt()</wbr>不会中断一个正在运行的线程。这一方法实际上完成的是,给受阻塞的线程抛出一个中断信号,
这样受阻线程就得以退出阻塞的状态<wbr style="line-height:25px"><span style="color:#003366; line-height:25px">。更确切的说,如果线程被</span><span style="color:#99cc00; line-height:25px">Object.wait,</span><span style="color:#808000; line-height:25px">Thread.join</span><span style="color:#003366; line-height:25px">和</span><span style="color:#339966; line-height:25px">Thread.sleep</span><span style="color:#003366; line-height:25px">三种方法之一阻塞,那么,它将接收到一个</span><wbr style="line-height:25px"><span style="color:#003366; line-height:25px">中断异常</span><wbr style="line-height:25px"><span style="color:#003366; line-height:25px">(</span><span style="color:#0000ff; line-height:25px">InterruptedException</span><span style="color:#003366; line-height:25px">),从而</span><span style="color:#003366; line-height:25px">提早地终结被阻塞状态</span><wbr style="line-height:25px"><span style="color:#003366; line-height:25px">。</span><br style="line-height:25px"> 如果线程没有被阻塞,这时调用<span style="color:#ff6600; line-height:25px">interrupt()</span>将不起作用;否则,线程就将得到<span style="line-height:25px; color:rgb(0,0,255)">InterruptedException</span>异常(该线程必须事先预备好处理此状况),接着逃离阻塞状态。<br style="line-height:25px"><span style="color:#000080; line-height:25px"></span><span style="color:#99cc00; line-height:25px">线程A</span><span style="color:#000080; line-height:25px">在执行</span><span style="color:#0000ff; line-height:25px">sleep</span><span style="color:#000080; line-height:25px">,</span><span style="color:#0000ff; line-height:25px">wait</span><span style="color:#000080; line-height:25px">,</span><span style="color:#0000ff; line-height:25px">join</span><span style="color:#000080; line-height:25px">时,</span><span style="color:#808000; line-height:25px">线程B</span><span style="color:#000080; line-height:25px">调用线程A的</span><span style="color:#ff6600; line-height:25px">interrupt</span><span style="color:#000080; line-height:25px">方法,的确这一个时候A会有</span><span style="color:#ff9900; line-height:25px">InterruptedException</span><span style="color:#000080; line-height:25px">异常抛出来.<br style="line-height:25px"> 但这其实是在</span><span style="color:#0000ff; line-height:25px">sleep,wait,join</span><span style="color:#000080; line-height:25px">这些方法内部会不断检查中断状态的值,而自己抛出的</span><span style="color:#ff9900; line-height:25px">InterruptedException</span><span style="color:#000080; line-height:25px">。<br style="line-height:25px"> 如果线程A正在执行一些指定的操作时如赋值,for,while,if,调用方法等,都不会去检查中断状态<br style="line-height:25px"> ,所以线程A不会抛出</span><span style="color:#ff9900; line-height:25px">InterruptedException</span><span style="color:#000080; line-height:25px">,而会一直执行着自己的操作.<br style="line-height:25px"> 当线程A终于执行到</span><span style="color:#0000ff; line-height:25px">wait(),sleep(),join()</span><span style="color:#000080; line-height:25px">时,才马上会抛出InterruptedException.<br style="line-height:25px"> 若没有调用sleep(),wait(),join()这些方法,即没有在线程里自己检查中断状态自己抛出InterruptedException的话,<br style="line-height:25px"> 那InterruptedException是不会被抛出来的.</span><br style="line-height:25px"> 具体使用见实例1,实例2。<br style="line-height:25px"> 注意1:当线程A执行到wait(),sleep(),join()时,抛出InterruptedException后,<wbr style="line-height:25px"><span style="color:#000080; line-height:25px">中断状态已经被系统复位<wbr style="line-height:25px">了</wbr></span>,<br style="line-height:25px"> 线程A调用<span style="color:#ff9900; line-height:25px">Thread.interrupted()</span>返回的是false <div style="line-height:25px"> <span style="line-height:normal; color:rgb(51,51,51); font-family:arial,sans-serif; font-size:13px"></span> <h4 style="line-height:25px; margin-top:1.25em; margin-right:0px; margin-bottom:0.65em; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial; font-size:1.1em; background-color:rgb(226,226,226); color:rgb(58,58,58)"> <span style="line-height:23px; font-size:0.9em">public static boolean</span><span style="line-height:25px; margin-right:2px">interrupted</span><span style="line-height:23px; font-size:0.9em">()</span> </h4> <div style="line-height:21px; margin-top:-25px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:0px; padding-right:7px; padding-bottom:0px; padding-left:7px; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial; font-size:0.8em; color:rgb(153,153,153); float:right"> <div style="line-height:21px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial"> Since:<a rel="nofollow" href="http://developer.android.com/guide/appendix/api-levels.html#level1" style="color:rgb(153,153,153); line-height:21px; text-decoration:none">API Level 1</a> </div> </div> <div style="line-height:23px; margin-top:0.5em; margin-right:0.25em; margin-bottom:0.5em; margin-left:0.25em; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial"> <div style="line-height:1em; margin-top:0.25em; margin-right:0px; margin-bottom:0.75em; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial"> <p style="line-height:1.3em; margin-top:0.5em; margin-bottom:0.5em; padding-top:0px; padding-bottom:0px; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial"> Returns a<code style="line-height:1em; color:rgb(0,112,0); font-family:monospace">boolean</code>indicating whether the current Thread (<code style="line-height:1em; color:rgb(0,112,0); font-family:monospace">currentThread()</code>) has a pending interrupt request (<code style="line-height:1em; color:rgb(0,112,0); font-family:monospace">true</code>) or not (<code style="line-height:1em; color:rgb(0,112,0); font-family:monospace">false</code>). It also has the side-effect of clearing the flag.</p> </div> <div style="line-height:23px; margin-top:0.5em; margin-right:1em; margin-bottom:0.5em; margin-left:1em; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial"> <h5 style="line-height:23px; margin-top:1em; margin-right:0px; margin-bottom:0.65em; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial; font-size:1em; color:rgb(58,58,58)"> Returns</h5> <ul style="line-height:1.3em; margin-top:0px; margin-right:0px; margin-bottom:1em; margin-left:1em; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial; list-style-type:none; list-style-position:initial"><li style="line-height:1.3em; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:2px; padding-left:0px; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial"> a<code style="line-height:1em; color:rgb(0,112,0); font-family:monospace">boolean</code>indicating the interrupt status</li></ul> </div> </div> </div> </wbr></wbr></wbr></wbr></wbr>
这样受阻线程就得以退出阻塞的状态<wbr style="line-height:25px"><span style="color:#003366; line-height:25px">。更确切的说,如果线程被</span><span style="color:#99cc00; line-height:25px">Object.wait,</span><span style="color:#808000; line-height:25px">Thread.join</span><span style="color:#003366; line-height:25px">和</span><span style="color:#339966; line-height:25px">Thread.sleep</span><span style="color:#003366; line-height:25px">三种方法之一阻塞,那么,它将接收到一个</span><wbr style="line-height:25px"><span style="color:#003366; line-height:25px">中断异常</span><wbr style="line-height:25px"><span style="color:#003366; line-height:25px">(</span><span style="color:#0000ff; line-height:25px">InterruptedException</span><span style="color:#003366; line-height:25px">),从而</span><span style="color:#003366; line-height:25px">提早地终结被阻塞状态</span><wbr style="line-height:25px"><span style="color:#003366; line-height:25px">。</span><br style="line-height:25px"> 如果线程没有被阻塞,这时调用<span style="color:#ff6600; line-height:25px">interrupt()</span>将不起作用;否则,线程就将得到<span style="line-height:25px; color:rgb(0,0,255)">InterruptedException</span>异常(该线程必须事先预备好处理此状况),接着逃离阻塞状态。<br style="line-height:25px"><span style="color:#000080; line-height:25px"></span><span style="color:#99cc00; line-height:25px">线程A</span><span style="color:#000080; line-height:25px">在执行</span><span style="color:#0000ff; line-height:25px">sleep</span><span style="color:#000080; line-height:25px">,</span><span style="color:#0000ff; line-height:25px">wait</span><span style="color:#000080; line-height:25px">,</span><span style="color:#0000ff; line-height:25px">join</span><span style="color:#000080; line-height:25px">时,</span><span style="color:#808000; line-height:25px">线程B</span><span style="color:#000080; line-height:25px">调用线程A的</span><span style="color:#ff6600; line-height:25px">interrupt</span><span style="color:#000080; line-height:25px">方法,的确这一个时候A会有</span><span style="color:#ff9900; line-height:25px">InterruptedException</span><span style="color:#000080; line-height:25px">异常抛出来.<br style="line-height:25px"> 但这其实是在</span><span style="color:#0000ff; line-height:25px">sleep,wait,join</span><span style="color:#000080; line-height:25px">这些方法内部会不断检查中断状态的值,而自己抛出的</span><span style="color:#ff9900; line-height:25px">InterruptedException</span><span style="color:#000080; line-height:25px">。<br style="line-height:25px"> 如果线程A正在执行一些指定的操作时如赋值,for,while,if,调用方法等,都不会去检查中断状态<br style="line-height:25px"> ,所以线程A不会抛出</span><span style="color:#ff9900; line-height:25px">InterruptedException</span><span style="color:#000080; line-height:25px">,而会一直执行着自己的操作.<br style="line-height:25px"> 当线程A终于执行到</span><span style="color:#0000ff; line-height:25px">wait(),sleep(),join()</span><span style="color:#000080; line-height:25px">时,才马上会抛出InterruptedException.<br style="line-height:25px"> 若没有调用sleep(),wait(),join()这些方法,即没有在线程里自己检查中断状态自己抛出InterruptedException的话,<br style="line-height:25px"> 那InterruptedException是不会被抛出来的.</span><br style="line-height:25px"> 具体使用见实例1,实例2。<br style="line-height:25px"> 注意1:当线程A执行到wait(),sleep(),join()时,抛出InterruptedException后,<wbr style="line-height:25px"><span style="color:#000080; line-height:25px">中断状态已经被系统复位<wbr style="line-height:25px">了</wbr></span>,<br style="line-height:25px"> 线程A调用<span style="color:#ff9900; line-height:25px">Thread.interrupted()</span>返回的是false <div style="line-height:25px"> <span style="line-height:normal; color:rgb(51,51,51); font-family:arial,sans-serif; font-size:13px"></span> <h4 style="line-height:25px; margin-top:1.25em; margin-right:0px; margin-bottom:0.65em; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial; font-size:1.1em; background-color:rgb(226,226,226); color:rgb(58,58,58)"> <span style="line-height:23px; font-size:0.9em">public static boolean</span><span style="line-height:25px; margin-right:2px">interrupted</span><span style="line-height:23px; font-size:0.9em">()</span> </h4> <div style="line-height:21px; margin-top:-25px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:0px; padding-right:7px; padding-bottom:0px; padding-left:7px; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial; font-size:0.8em; color:rgb(153,153,153); float:right"> <div style="line-height:21px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial"> Since:<a rel="nofollow" href="http://developer.android.com/guide/appendix/api-levels.html#level1" style="color:rgb(153,153,153); line-height:21px; text-decoration:none">API Level 1</a> </div> </div> <div style="line-height:23px; margin-top:0.5em; margin-right:0.25em; margin-bottom:0.5em; margin-left:0.25em; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial"> <div style="line-height:1em; margin-top:0.25em; margin-right:0px; margin-bottom:0.75em; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial"> <p style="line-height:1.3em; margin-top:0.5em; margin-bottom:0.5em; padding-top:0px; padding-bottom:0px; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial"> Returns a<code style="line-height:1em; color:rgb(0,112,0); font-family:monospace">boolean</code>indicating whether the current Thread (<code style="line-height:1em; color:rgb(0,112,0); font-family:monospace">currentThread()</code>) has a pending interrupt request (<code style="line-height:1em; color:rgb(0,112,0); font-family:monospace">true</code>) or not (<code style="line-height:1em; color:rgb(0,112,0); font-family:monospace">false</code>). It also has the side-effect of clearing the flag.</p> </div> <div style="line-height:23px; margin-top:0.5em; margin-right:1em; margin-bottom:0.5em; margin-left:1em; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial"> <h5 style="line-height:23px; margin-top:1em; margin-right:0px; margin-bottom:0.65em; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial; font-size:1em; color:rgb(58,58,58)"> Returns</h5> <ul style="line-height:1.3em; margin-top:0px; margin-right:0px; margin-bottom:1em; margin-left:1em; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial; list-style-type:none; list-style-position:initial"><li style="line-height:1.3em; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:2px; padding-left:0px; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial"> a<code style="line-height:1em; color:rgb(0,112,0); font-family:monospace">boolean</code>indicating the interrupt status</li></ul> </div> </div> </div> </wbr></wbr></wbr></wbr></wbr>
public booleanisInterrupted()
Since:
API Level 1
Returns aboolean
indicating whether the receiver has a pending interrupt request (true
) or not (false
)
Returns
- a
boolean
indicating the interrupt status
1.sleep()&interrupt()<wbr style="line-height:25px"><br style="line-height:25px"><span style="color:#000080; line-height:25px"></span><span style="color:#993300; line-height:25px">线程A</span><span style="color:#000080; line-height:25px">正在使用sleep()暂停着:</span><span style="color:#0000ff; line-height:25px">Thread.sleep(100000);</span><br style="line-height:25px"><span style="color:#000080; line-height:25px">如果要取消他的等待状态,可以在正在执行的线程里(比如这里是B)调用</span><br style="line-height:25px"><span style="color:#000080; line-height:25px">a.interrupt();</span><br style="line-height:25px"><span style="color:#000080; line-height:25px">令线程A放弃睡眠操作,这里a是线程A对应到的Thread实例</span><span style="color:#000080; line-height:25px"><br style="line-height:25px"> 当在sleep中时 线程被调用interrupt()时,就马上会放弃暂停的状态.并抛出</span><span style="color:#ff9900; line-height:25px">InterruptedException</span><span style="color:#000080; line-height:25px">.丢出异常的,是A线程.</span><br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">2.wait()&interrupt()</wbr></span><wbr style="line-height:25px"><br style="line-height:25px"> 线程A调用了wait()进入了等待状态,也可以用interrupt()取消.<br style="line-height:25px"> 不过这时候要小心锁定的问题.线程在进入等待区,<wbr style="line-height:25px"><span style="color:#000080; line-height:25px">会把锁定解除</span><wbr style="line-height:25px">,当对等待中的线程调用<span style="color:#ff9900; line-height:25px">interrupt()</span>时<br style="line-height:25px"> ,会<span style="color:#000080; line-height:25px"><wbr style="line-height:25px">先重新获取锁定,再抛出异常<wbr style="line-height:25px">.</wbr></wbr></span>在获取锁定之前,是无法抛出异常的.<br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">3.join()&interrupt()</wbr></span><wbr style="line-height:25px"><br style="line-height:25px"><span style="color:#003366; line-height:25px">当线程以join()等待其他线程结束时,当它被调用interrupt(),它与sleep()时一样,会马上跳到</span><span style="color:#0000ff; line-height:25px">catch</span><span style="color:#003366; line-height:25px">块里.</span><br style="line-height:25px"> 注意<span style="color:#000080; line-height:25px">,是对谁调用interrupt()方法,一定是调用被阻塞线程的interrupt方法.如在线程a中调用来线程t.join().<br style="line-height:25px"> 则a会等t执行完后在执行t.join后的代码,当在线程b中调用来a.interrupt()方法,则会抛出InterruptedException,当然join()也就被取消了。</span><br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">实例1</wbr></span><wbr style="line-height:25px">:<br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">staticvoid</span><span style="color:#3366ff; line-height:25px">interruptDemo()</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">{</span><br style="line-height:25px"><span style="color:#0000ff; line-height:25px">Threadt1=newThread(newInterruptDemoRunnable(),"first");<br style="line-height:25px"> Threadt2=newThread(newInterruptDemoRunnable(t1),"second");<br style="line-height:25px"> t1.start();<br style="line-height:25px"> t2.start();</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#993300; line-height:25px">class</span><span style="color:#3366ff; line-height:25px">InterruptDemoRunnableimplementsRunnable</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">longid=0;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">staticlongcount=0;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">Threadt;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">InterruptDemoRunnable()</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">this(null);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">InterruptDemoRunnable(Threadt)</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">id=count++;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">this.t=t;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">booleanblRun=true;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">publicvoid</span><span style="color:#3366ff; line-height:25px">run()</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">{intcnt=0;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="line-height:25px; background-color:rgb(255,255,255)"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">while</span><span style="color:#3366ff; line-height:25px">(</span></span><span style="color:#3366ff; line-height:25px">blRun)</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">if(t!=null)</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">if(cnt==2)</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">t.interrupt();</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">if(</span><span style="color:#ff9900; line-height:25px">t.isInterrupted())</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">System.out.println("setthethread"+t.getName()+"tobeInterrupted");</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">else</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">try</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">Thread.sleep(1000);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">catch(InterruptedExceptione)</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">System.out.println("FirstInterruptedwhensleep");</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">System.out.println("Interruptedis"+Thread.interrupted());//@1</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">try</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">Thread.sleep(1);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">catch(InterruptedExceptione)</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">System.out.println("secondInterruptedwhensleep");</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">System.out.println(id+"run"+cnt);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">cnt++;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">if(cnt==5)</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">blRun=false;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">System.out.println("thread:"+id+"exit");</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">注意1</wbr></span><wbr style="line-height:25px">:当线程A执行到wait(),sleep(),join()时,抛出InterruptedException后,中断状态已经被系统复位了。<br style="line-height:25px"> 线程AThread.interrupted()返回的是false<br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">实例2</wbr></span><wbr style="line-height:25px">:<br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">staticvoid</span><span style="color:#3366ff; line-height:25px">interruptDemo()<br style="line-height:25px"> {<br style="line-height:25px"> Threadt1=newInterruptDemoThread();<br style="line-height:25px"> t1.setName("t1");<br style="line-height:25px"> Threadt2=newInterruptDemoThread(t1);<br style="line-height:25px"> t1.setName("t2");<br style="line-height:25px"> t1.start();<br style="line-height:25px"> t2.start();<br style="line-height:25px"> }</span><br style="line-height:25px"><span style="color:#993300; line-height:25px">class</span><span style="color:#3366ff; line-height:25px">InterruptDemoThreadextendsThread<br style="line-height:25px"> {<br style="line-height:25px"> longid=0;<br style="line-height:25px"> staticlongcount=0;<br style="line-height:25px"> Threadt;<br style="line-height:25px"> InterruptDemoThread()<br style="line-height:25px"> {<br style="line-height:25px"> this(null);<br style="line-height:25px"> }<br style="line-height:25px"><br style="line-height:25px"> InterruptDemoThread(Threadt)<br style="line-height:25px"> {<br style="line-height:25px"> id=count++;<br style="line-height:25px"> this.t=t;<br style="line-height:25px"> }<br style="line-height:25px"> booleanblRun=true;<br style="line-height:25px"></span><span style="color:#993300; line-height:25px">publicvoid</span><span style="color:#3366ff; line-height:25px">run()<br style="line-height:25px"> {intcnt=0;<br style="line-height:25px"></span><span style="color:#993300; line-height:25px">while</span><span style="color:#3366ff; line-height:25px">(blRun)</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">if(t!=null)</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">if(cnt==2)</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">t.interrupt();</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">if(</span><span style="color:#ff00ff; line-height:25px">t.isInterrupted()</span><span style="color:#3366ff; line-height:25px">)</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">System.out.println("setthethread"+t.getName()+"tobeInterrupted");</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">elseif(</span><span style="color:#ff00ff; line-height:25px">isInterrupted()</span><span style="color:#808080; line-height:25px">)@1</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">{</span><br style="line-height:25px"><span style="color:#808080; line-height:25px">/*Thread.interrupted();//@2<br style="line-height:25px"> System.out.println("afterinterrupted(),thestateofInterruptedis"+isInterrupted());*/</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">try{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">Thread.sleep(1);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">catch(InterruptedExceptione)</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">System.out.println("Interruptedwhensleep");</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">System.out.println("Interruptedis"+Thread.interrupted());</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">Thread.yield();</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">System.out.println(id+"run"+cnt);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">cnt++;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">if(cnt==5)</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">blRun=false;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">System.out.println("thread:"+id+"exit");</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">注意1</wbr></span><wbr style="line-height:25px">:如果线程被调用了interrupt(),那么如果此次该线程并不在wait(),sleep(),join()时,<br style="line-height:25px"> 下次执行<span style="color:#0000ff; line-height:25px">wait(),sleep(),join()</span>时,一样会抛出InterruptedException,当然抛出后该线程的中断状态也回被系统复位。<br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">注意2</wbr></span><wbr style="line-height:25px">:我们可以<wbr style="line-height:25px"><span style="color:#333399; line-height:25px">手动的使用Thread.interrupted()</span><wbr style="line-height:25px">来使当前线程的中断状态系统复位(即清除中断状态)</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>