中断java程序代码 java程序终止

Java如何中断代码的执行嗨中断java程序代码你好
在Java中中断java程序代码,中断java程序代码你可以使用判断if........eles或是用try.......catch中断java程序代码的方法也是可以实现你想要的结果的哦,具体的你可以参考jdk来
希望可以帮到你
祝你生活愉快
Java如何中断一个正在运行的线程 程序是很简易的 然而 在编程人员面前 多线程呈现出了一组新的难题 如果没有被恰当的解决 将导致意外的行为以及细微的 难以发现的错误
在本篇文章中 我们针对这些难题之一 如何中断一个正在运行的线程
背景中断(Interrupt)一个线程意味着在该线程完成任务之前停止其正在进行的一切 有效地中止其当前的操作 线程是死亡 还是等待新的任务或是继续运行至下一步 就取决于这个程序 虽然初次看来它可能显得简单 但是 你必须进行一些预警以实现期望的结果 你最好还是牢记以下的几点告诫
首先 忘掉Thread stop方法 虽然它确实停止了一个正在运行的线程 然而 这种方法是不安全也是不受提倡的 这意味着 在未来的JAVA版本中 它将不复存在
一些轻率的家伙可能被另一种方法Thread interrupt所迷惑 尽管 其名称似乎在暗示著什么 然而 这种方法并不会中断一个正在运行的线程(待会将进一步说明) 正如Listing A中描述的那样 它创建了一个线程 并且试图使用Thread interrupt方法停止该线程 Thread sleep()方法的调用 为线程的初始化和中止提供了充裕的时间 线程本身并不参与任何有用的操作
class Example extends Thread {boolean stop=false;public static void main( String args[] ) throws Exception {Example thread = new Example ();System out println( Starting thread );thread start();Thread sleep( );System out println( Interrupting thread );thread interrupt();Thread sleep( );System out println( Stopping application );//System exit( );}public void run() {while(!stop){System out println( Thread is running );long time = System currentTimeMillis();while((System currentTimeMillis() time)) {}}System out println( Thread exiting under request );}}
如果你运行了Listing A中的代码 你将在控制台看到以下输出
Starting thread Thread is running Thread is running Thread is running Interrupting thread Thread is running Thread is running Thread is running Stopping application Thread is running Thread is running Thread is running
甚至 在Thread interrupt()被调用后 线程仍然继续运行
真正地中断一个线程
中断线程最好的 最受推荐的方式是 使用共享变量(shared variable)发出信号 告诉线程必须停止正在运行的任务 线程必须周期性的核查这一变量(尤其在冗余操作期间) 然后有秩序地中止任务 Listing B描述了这一方式
Listing Bclass Exampleextends Thread {volatile boolean stop = false;public static void main( String args[] ) throws Exception {Examplethread = new Example ();System out println(Starting thread);thread start();Thread sleep();System out println(Asking thread to stop);thread stop = true;Thread sleep();System out println(Stopping application);//System exit();}public void run() {while ( !stop ) {System out println(Thread is running);long time = System currentTimeMillis();while ( (System currentTimeMillis() time)(!stop) ) {}}System out println(Thread exiting under request);}}
运行Listing B中的代码将产生如下输出(注意线程是如何有秩序的退出的)
Starting thread Thread is running Thread is running Thread is running Asking thread to stop Thread exiting under request Stopping application
虽然该方法要求一些编码 但并不难实现 同时 它给予线程机会进行必要的清理工作 这在任何一个多线程应用程序中都是绝对需要的 请确认将共享变量定义成volatile 类型或将对它的一切访问封入同步的块/方法(synchronized blocks/methods)中

推荐阅读