JAVA里的线程中断

JAVA里的线程中断
(这是新浪博客上一朋友的笔记,感觉很不错就转过来了,让我了解不少)
<wbr>在JAVA中,通过其对线程类的内嵌支持,编程人员编写多线程程序是很简易的。然而,在编程人员面前,多线程呈现出了一组新的难题,如果没有被恰当的解决,将导致意外的行为以及细微的、难以发现的错误。在本篇文章中,我们针对这些难题之一:如何中断一个正在运行的线程。<wbr><br><br>背景<br>中断(Interrupt)一个线程意味着在该线程完成任务之前停止其正在进行的一切,有效地中止其当前的操作。线程是死亡、还是等待新的任务或是继续运行至下一步,就取决于这个程序。<br><br>虽然初次看来它可能显得简单,但是,你必须进行一些预警以实现期望的结果。你最好还是牢记以下的几点告诫。<br><br>首先,忘掉Thread.stop方法。虽然它确实停止了一个正在运行的线程,然而,这种方法是不安全也是不受提倡的,这意味着,在未来的JAVA版本中,它将不复存在。<br><br>一些轻率的家伙可能被另一种方法Thread.interrupt所迷惑。尽管,其名称似乎在暗示着什么,然而,这种方法并不会中断一个正在运行的线程(待会将进一步说明),正如Listing<wbr>A中描述的那样。它创建了一个线程,并且试图使用Thread.interrupt方法停止该线程。Thread.sleep()方法的调用,为线程的初始化和中止提供了充裕的时间。线程本身并不参与任何有用的操作。<br><br>Listing<wbr>A<br>class<wbr>Example1<wbr>extends<wbr>Thread<wbr>{<br><br><wbr><wbr>public<wbr>static<wbr>void<wbr>main(<wbr>String<wbr>args[]<wbr>)<wbr>throws<wbr>Exception<wbr>{<br><br><wbr><wbr><wbr><wbr>Example1<wbr>thread<wbr>=<wbr>new<wbr>Example1();<br><br><wbr><wbr><wbr>System.out.println(<wbr>"Starting<wbr>thread..."<wbr>);<br><br><wbr><wbr><wbr>thread.start();<br><br><wbr><wbr><wbr>Thread.sleep(<wbr>3000<wbr>);<br><br><wbr><wbr><wbr>System.out.println(<wbr>"Interrupting<wbr>thread..."<wbr>);<br><br><wbr><wbr><wbr>thread.interrupt();<br><br><wbr><wbr><wbr>Thread.sleep(<wbr>3000<wbr>);<br><br><wbr><wbr><wbr>System.out.println(<wbr>"Stopping<wbr>application..."<wbr>);<br><br><wbr><wbr><wbr>System.exit(<wbr>0<wbr>);<br><br><wbr><wbr>}<br><br><br>如果你运行了Listing<wbr>A中的代码,你将在控制台看到以下输出:<br><br>Starting<wbr>thread...<br><br>Thread<wbr>is<wbr>running...<br><br>Thread<wbr>is<wbr>running...<br><br>Thread<wbr>is<wbr>running...<br><br>Interrupting<wbr>thread...<br><br>Thread<wbr>is<wbr>running...<br><br>Thread<wbr>is<wbr>running...<br><br>Thread<wbr>is<wbr>running...<br><br>Stopping<wbr>application...<br><br>甚至,在Thread.interrupt()被调用后,线程仍然继续运行了一段时间。<br><br>真正地中断一个线程<br><br>中断线程最好的,最受推荐的方式是,使用共享变量(shared<wbr>variable)发出信号,告诉线程必须停止正在运行的任务。线程必须周期性的核查这一变量(尤其在冗余操作期间),然后有秩序地中止任务。Listing<wbr>B描述了这一方式。<br><br>Listing<wbr>B<br>class<wbr>Example2<wbr>extends<wbr>Thread<wbr>{<br><br><wbr><wbr>volatile<wbr>boolean<wbr>stop<wbr>=<wbr>false;<br><br><wbr><wbr>public<wbr>static<wbr>void<wbr>main(<wbr>String<wbr>args[]<wbr>)<wbr>throws<wbr>Exception<wbr>{<br><br><wbr><wbr><wbr><wbr>Example2<wbr>thread<wbr>=<wbr>new<wbr>Example2();<br><br><wbr><wbr><wbr>System.out.println(<wbr>"Starting<wbr>thread..."<wbr>);<br><br><wbr><wbr><wbr>thread.start();<br><br><wbr><wbr><wbr>Thread.sleep(<wbr>3000<wbr>);<br><br><wbr><wbr><wbr>System.out.println(<wbr>"Asking<wbr>thread<wbr>to<wbr>stop..."<wbr>);<br><br><wbr><wbr><wbr>thread.stop<wbr>=<wbr>true;<br><br><wbr><wbr><wbr>Thread.sleep(<wbr>3000<wbr>);<br><br><wbr><wbr><wbr>System.out.println(<wbr>"Stopping<wbr>application..."<wbr>);<br><br><wbr><wbr><wbr>System.exit(<wbr>0<wbr>);<br><br><wbr><wbr>}<br><br><wbr><wbr>public<wbr>void<wbr>run()<wbr>{<br><br><wbr><wbr><wbr><wbr>while<wbr>(<wbr>!stop<wbr>)<wbr>{<br><br><wbr><wbr><wbr><wbr><wbr>System.out.println(<wbr>"Thread<wbr>is<wbr>running..."<wbr>);<br><br><wbr><wbr><wbr><wbr><wbr><wbr>long<wbr>time<wbr>=<wbr>System.currentTimeMillis();<br><br><wbr><wbr><wbr><wbr><wbr><wbr>while<wbr>(<wbr>(System.currentTimeMillis()-time<wbr>&lt;<wbr>1000)<wbr>&amp;&amp;<wbr>(!stop)<wbr>)<wbr>{<br><br><wbr><wbr><wbr><wbr><wbr><wbr>}<br><br><wbr><wbr><wbr><wbr>}<br><br><wbr><wbr><wbr>System.out.println(<wbr>"Thread<wbr>exiting<wbr>under<wbr>request..."<wbr>);<br><br><wbr><wbr>}<br><br>}<br><br><wbr><br><br><br>运行Listing<wbr>B中的代码将产生如下输出(注意线程是如何有秩序的退出的)<br><br>Starting<wbr>thread...<br><br>Thread<wbr>is<wbr>running...<br><br>Thread<wbr>is<wbr>running...<br><br>Thread<wbr>is<wbr>running...<br><br>Asking<wbr>thread<wbr>to<wbr>stop...<br><br>Thread<wbr>exiting<wbr>under<wbr>request...<br><br>Stopping<wbr>application...<br><br>虽然该方法要求一些编码,但并不难实现。同时,它给予线程机会进行必要的清理工作,这在任何一个多线程应用程序中都是绝对需要的。请确认将共享变量定义成volatile<wbr>类型或将对它的一切访问封入同步的块/方法(synchronized<wbr>blocks/methods)中。<br><br>到目前为止一切顺利!但是,当线程等待某些事件发生而被阻塞,又会发生什么?当然,如果线程被阻塞,它便不能核查共享变量,也就不能停止。这在许多情况下会发生,例如调用Object.wait()、ServerSocket.accept()和DatagramSocket.receive()时,这里仅举出一些。<br><br>他们都可能永久的阻塞线程。即使发生超时,在超时期满之前持续等待也是不可行和不适当的,所以,要使用某种机制使得线程更早地退出被阻塞的状态。<br><br>很不幸运,不存在这样一种机制对所有的情况都适用,但是,根据情况不同却可以使用特定的技术。在下面的环节,我将解答一下最普遍的例子。<br><br>使用Thread.interrupt()中断线程<br>正如Listing<wbr>A中所描述的,Thread.interrupt()方法不会中断一个正在运行的线程。这一方法实际上完成的是,在线程受到阻塞时抛出一个中断信号,这样线程就得以退出阻塞的状态。更确切的说,如果线程被Object.wait,<wbr>Thread.join和Thread.sleep三种方法之一阻塞,那么,它将接收到一个中断异常(InterruptedException),从而提早地终结被阻塞状态。<br><br>因此,如果线程被上述几种方法阻塞,正确的停止线程方式是设置共享变量,并调用interrupt()(注意变量应该先设置)。如果线程没有被阻塞,这时调用interrupt()将不起作用;否则,线程就将得到异常(该线程必须事先预备好处理此状况),接着逃离阻塞状态。在任何一种情况中,最后线程都将检查共享变量然后再停止。Listing<wbr>C这个示例描述了该技术。<br></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

你可能感兴趣的:(java)