首页>>后端>>java->Java多线程编程之停止线程的3种方法

Java多线程编程之停止线程的3种方法

时间:2023-12-01 本站 点击:0

前言

前面的文章介绍了线程的使用。线程除了在执行处理完成其任务后会停止外,还可以通过一些方法进行干预停止其运行。停止一个线程意味着在线程处理完成任务之前结束其正在执行的操作。

在java中可以使用以下三种终止线程的方法。

1、使用退出标志

使用退出标志,使线程正常退出,也就是当run方法完成后线程终止。

当run方法执行完后,线程就会退出。但有时run方法是永远不会结束的。如在服务端程序中使用线程进行监听客户端请求,或是其他的需要循环处理的任务。在这种情况下,一般是将这些任务放在一个循环中,如while循环。比如:

packagecom.jiangxia.chap1;/***线程停止的方法之:使用退出标志*author:jiangxia*date:2021-04-15*/publicclassDemo12{publicstaticvoidmain(String[]args)throwsInterruptedException{Demo12Threadt=newDemo12Thread();t.start();Thread.sleep(2000);t.stopThread();}}classDemo12ThreadextendsThread{privatebooleanflag=true;@Overridepublicvoidrun(){try{while(flag){System.out.println("Time="+System.currentTimeMillis());Thread.sleep(1000);}System.out.println("线程执行结束");}catch(InterruptedExceptione){e.printStackTrace();}}/***定义一个停止线程的方法*/publicvoidstopThread(){//将标志位改为flaseflag=false;}}

2、stop方法强制结束线程

java种提供了Thread.stop()方法停止一个线程,虽然它确实可以停止一个正在运行的线程,但是这个方法是不安全的,而且是已被废弃的方法。调用stop()方法时会抛出java.lang.ThreadDeath异常。官方文档对其的解释如下:

该方法天生是不安全的。使用thread.stop()停止一个线程,导致释放(解锁)所有该线程已经锁定的监视器(因沿堆栈向上传播的未检查异常ThreadDeath而解锁)。如果之前受这些监视器保护的任何对象处于不一致状态,则不一致状态的对象(受损对象)将对其他线程可见,这可能导致任意的行为。

packagecom.jiangxia.chap1;/***停止线程之stop方法*author:jiangxia*date:2021-04-15*/publicclassDemo13{publicstaticvoidmain(String[]args)throwsInterruptedException{Threadt=newDemo13Thread();t.start();Thread.sleep(2000);//该方法有个删除线表示已经被删除不建议使用//stop方法会产生异常t.stop();}}classDemo13ThreadextendsThread{@Overridepublicvoidrun(){try{while(true){System.out.println("run方法Time="+System.currentTimeMillis());Thread.sleep(1000);}}catch(InterruptedExceptione){e.printStackTrace();}catch(ThreadDeaththreadDeath){System.out.println("进入catch块");threadDeath.printStackTrace();}}}

通过idea可以发现stop方法有个删除线,说明stop方法已经被作废,如果强制让线程停止有可能使一些清理性的工作得不到完成。另外一个原因是对锁定的对象进行『解锁』,导致数据得不同同步的处理,出现数据不一致性的问题。所以使用stop()方法停止线程则是非常暴力的,由于stop()方法以及在JDK中被标明为“过期/作废”的方法,所以它在功能上具有缺陷,所以不建议在程序张使用stop()方法。比如:

packagecom.jiangxia.chap1;publicclassDemo14{publicstaticvoidmain(String[]args)throwsInterruptedException{Demo14Infoinfo=newDemo14Info();Threadt=newDemo14Thread(info);t.start();Thread.sleep(200);t.stop();//因为stop方法结束了线程,所以只更新了username的值,而password的值还是之前的值没有被更新System.out.println("username="+info.getUsername()+":password="+info.getPassword());}}classDemo14ThreadextendsThread{privateDemo14Infoinfo;publicDemo14Thread(Demo14Infoinfo){this.info=info;}@Overridepublicvoidrun(){info.updateInfo("张飞","1111111");}}classDemo14Info{privateStringusername="刘备";privateStringpassword="222222";publicStringgetUsername(){returnusername;}publicStringgetPassword(){returnpassword;}publicvoidsetUsername(Stringusername){this.username=username;}publicvoidsetPassword(Stringpassword){this.password=password;}synchronizedpublicvoidupdateInfo(Stringusername,Stringpassword){try{this.username=username;Thread.sleep(1000);this.password=password;}catch(InterruptedExceptione){e.printStackTrace();}}}

因为stop方法结束了线程,所以上述代码只更新了username的值,而password的值还是之前的值没有被更新。

3、使用interrupt方法中断线程

interrupt()其作用是中断此线程,注意此线程不一定是当前线程,而是值调用该方法的Thread实例所代表的线程,调用interrupt方法不会真正的结束线程,在当前线程中打上一个停止的标记,线程仍然会继续运行。

Thread类提供了interrupted方法测试当前线程是否中断,即检查中断的标志,返回一个boolean值并清除中断的状态,第二次再调用时中断状态已经被清楚,所以会返回false。

isInterrupted方法测试线程是否已经中断,不清除中断状态。

对这三个方法的总结可以归纳如下:

publicvoidThread.interrupt()//无返回值publicbooleanThread.isInterrupted()//有返回值publicstaticbooleanThread.interrupted()//静态,有返回值

比如:

packagecom.jiangxia.chap1;/***停止线程之interrupt方法*author:jiangxia*date:2021-04-15*/publicclassDemo15{publicstaticvoidmain(String[]args){Threadthread=newThread15();thread.start();//使用interrupt方法中断线程thread.interrupt();}}classThread15extendsThread{@Overridepublicvoidrun(){for(inti=0;i<1000;i++){System.out.println("run方法:i="+i);}}}

调用interrupt方法不会真正的结束线程,而是在当前线程上打上一个停止的标记。

packagecom.jiangxia.chap1;/***停止线程之isInterrupted方法*author:jiangxia*date:2021-04-15*/publicclassDemo15{publicstaticvoidmain(String[]args){Threadthread=newThread15();thread.start();//使用interrupt方法中断线程thread.interrupt();System.out.println("是否已经停止1:"+thread.isInterrupted());System.out.println("是否已经停止2:"+Thread.interrupted());}}classThread15extendsThread{@Overridepublicvoidrun(){for(inti=0;i<1000;i++){System.out.println("run方法:i="+i);}}}

thread.isInterrupted方法是检查Thread15是否被打上停止的标记。

Thread.interrupted方法是检查主线程是否打上停止的标记。

interrupted()与isInterrupted()的唯一区别是,前者会读取并清除中断状态,后者仅读取状态。

测试当前线程是否已经中断,线程的中断的状态由方法清除,如果两次连续调用该方法,第二次调用将返回false。

packagecom.jiangxia.chap1;publicclassDemo17{publicstaticvoidmain(String[]args)throwsInterruptedException{Threadthread=newThred17();//启动线程thread.start();//Thread.sleep(1000);thread.interrupt();}}classThred17extendsThread{@Overridepublicvoidrun(){for(inti=0;i<1000;i++){if(this.isInterrupted()){System.out.println("已经是停止状态!");break;}System.out.println("i="+i);}System.out.println("这里是结束循环后的代码");}}

如果线程中有sleep代码,不管是否进入到sleep的状态,如果调用了interrupt方法,都会产生异常信息。比如:

packagecom.jiangxia.chap1;publicclassDemo17{publicstaticvoidmain(String[]args)throwsInterruptedException{Threadthread=newThred17();//启动线程thread.start();//Thread.sleep(1000);thread.interrupt();}}classThred17extendsThread{@Overridepublicvoidrun(){try{for(inti=0;i<1000;i++){if(this.isInterrupted()){System.out.println("已经是停止状态!");//如果使用break那么会导致其他线程不知道,所以在这里直接抛出异常//break;thrownewInterruptedException();}System.out.println("i="+i);}System.out.println("这里是结束循环后的代码");}catch(InterruptedExceptione){e.printStackTrace();}}}

packagecom.jiangxia.chap1;publicclassDemo18{publicstaticvoidmain(String[]args)throwsInterruptedException{Threadt=newThread18();t.start();Thread.sleep(20);t.interrupt();}}classThread18extendsThread{@Overridepublicvoidrun(){try{for(inti=0;i<Integer.MAX_VALUE;i++){Strings=newString();}System.out.println("开始线程");Thread.sleep(20000);System.out.println("结束线程");}catch(InterruptedExceptione){System.out.println("异常进入catch代码块");e.printStackTrace();}}}

总结

以上就是关于Java中如何停止一个线程的三种方法。在Java中没有提供任何机制来安全地终止线程。stop方法因为不安全已经被废弃了。但java提供了中断机制(Interruption),这是一种协作机制,能够使一个线程终止另一个线程的的工作。除此在外可以通过设置退出标志的方式控制线程的执行,线程内部检查变量状态,外部改变变量值可控制停止执行。


本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:/java/5459.html