目录1。停止不了的线程2。判断线程是否停止状态3。能停止的线程异常法4。在沉睡中停止5。能停止的线程暴力停止6。方法stop()与java。lang。ThreadDeath异常7。释放锁的不良后果8。使用return停止线程 停止一个线程意味着在任务处理完任务之前停掉正在做的操作,也就是放弃当前的操作。停止一个线程可以用Thread。stop()方法,但最好不要用它。虽然它确实可以停止一个正在运行的线程,但是这个方法是不安全的,而且是已被废弃的方法。在java中有以下3种方法可以终止正在运行的线程:使用退出标志,使线程正常退出,也就是当run方法完成后线程终止。使用stop方法强行终止,但是不推荐这个方法,因为stop和suspend及resume一样都是过期作废的方法。使用interrupt方法中断线程。1。停止不了的线程 interrupt()方法的使用效果并不像forbreak语句那样,马上就停止循环。调用interrupt方法是在当前线程中打了一个停止标志,并不是真的停止线程。publicclassMyThreadextendsThread{publicvoidrun(){super。run();for(inti0;i500000;i){System。out。println(i(i1));}}}publicclassRun{publicstaticvoidmain(Stringargs〔〕){ThreadthreadnewMyThread();thread。start();try{Thread。sleep(2000);thread。interrupt();}catch(InterruptedExceptione){e。printStackTrace();}}} 输出结果:。。。i499994i499995i499996i499997i499998i499999i5000002。判断线程是否停止状态 Thread。java类中提供了两种方法:this。interrupted():测试当前线程是否已经中断;this。isInterrupted():测试线程是否已经中断; 那么这两个方法有什么图区别呢?我们先来看看this。interrupted()方法的解释:测试当前线程是否已经中断,当前线程是指运行this。interrupted()方法的线程。publicclassMyThreadextendsThread{publicvoidrun(){super。run();for(inti0;i500000;i){i;System。out。println(i(i1));}}}publicclassRun{publicstaticvoidmain(Stringargs〔〕){ThreadthreadnewMyThread();thread。start();try{Thread。sleep(2000);thread。interrupt();System。out。println(stop1??thread。interrupted());System。out。println(stop2??thread。interrupted());}catch(InterruptedExceptione){e。printStackTrace();}}} 运行结果:stop1??falsestop2??false 类Run。java中虽然是在thread对象上调用以下代码:thread。interrupt(),后面又使用System。out。println(stop1??thread。interrupted());System。out。println(stop2??thread。interrupted()); 来判断thread对象所代表的线程是否停止,但从控制台打印的结果来看,线程并未停止,这也证明了interrupted()方法的解释,测试当前线程是否已经中断。这个当前线程是main,它从未中断过,所以打印的结果是两个false。 如何使main线程产生中断效果呢?publicclassRun2{publicstaticvoidmain(Stringargs〔〕){Thread。currentThread()。interrupt();System。out。println(stop1??Thread。interrupted());System。out。println(stop2??Thread。interrupted());System。out。println(End);}} 运行效果为:stop1??truestop2??falseEnd 方法interrupted()的确判断出当前线程是否是停止状态。但为什么第2个布尔值是false呢?官方帮助文档中对interrupted方法的解释:测试当前线程是否已经中断。线程的中断状态由该方法清除。换句话说,如果连续两次调用该方法,则第二次调用返回false。 下面来看一下inInterrupted()方法。publicclassRun3{publicstaticvoidmain(Stringargs〔〕){ThreadthreadnewMyThread();thread。start();thread。interrupt();System。out。println(stop1??thread。isInterrupted());System。out。println(stop2??thread。isInterrupted());}} 运行结果:stop1??truestop2??true isInterrupted()并未清除状态,所以打印了两个true。3。能停止的线程异常法 有了前面学习过的知识点,就可以在线程中用for语句来判断一下线程是否是停止状态,如果是停止状态,则后面的代码不再运行即可:publicclassMyThreadextendsThread{publicvoidrun(){super。run();for(inti0;i500000;i){if(this。interrupted()){System。out。println(线程已经终止,for循环不再执行);}System。out。println(i(i1));}}}publicclassRun{publicstaticvoidmain(Stringargs〔〕){ThreadthreadnewMyThread();thread。start();try{Thread。sleep(2000);thread。interrupt();}catch(InterruptedExceptione){e。printStackTrace();}}} 运行结果:。。。i202053i202054i202055i202056线程已经终止,for循环不再执行 上面的示例虽然停止了线程,但如果for语句下面还有语句,还是会继续运行的。看下面的例子:publicclassMyThreadextendsThread{publicvoidrun(){super。run();for(inti0;i500000;i){if(this。interrupted()){System。out。println(线程已经终止,for循环不再执行);}System。out。println(i(i1));}System。out。println(这是for循环外面的语句,也会被执行);}} 使用Run。java执行的结果是:。。。i180136i180137i180138i180139线程已经终止,for循环不再执行这是for循环外面的语句,也会被执行 如何解决语句继续运行的问题呢?看一下更新后的代码:publicclassMyThreadextendsThread{publicvoidrun(){super。run();try{for(inti0;i500000;i){if(this。interrupted()){System。out。println(线程已经终止,for循环不再执行);thrownewInterruptedException();}System。out。println(i(i1));}System。out。println(这是for循环外面的语句,也会被执行);}catch(InterruptedExceptione){System。out。println(进入MyThread。java类中的catch了);e。printStackTrace();}}} 使用Run。java运行的结果如下:。。。i203798i203799i203800线程已经终止,for循环不再执行进入MyThread。java类中的catch了java。lang。InterruptedExceptionatthread。MyThread。run(MyThread。java:13)4。在沉睡中停止 如果线程在sleep()状态下停止线程,会是什么效果呢?publicclassMyThreadextendsThread{publicvoidrun(){super。run();try{System。out。println(线程开始);Thread。sleep(200000);System。out。println(线程结束。);}catch(InterruptedExceptione){System。out。println(在沉睡中被停止,进入catch,调用isInterrupted()方法的结果是:this。isInterrupted());e。printStackTrace();}}} 使用Run。java运行的结果是:线程开始在沉睡中被停止,进入catch,调用isInterrupted()方法的结果是:falsejava。lang。InterruptedException:sleepinterruptedatjava。lang。Thread。sleep(NativeMethod)atthread。MyThread。run(MyThread。java:12) 从打印的结果来看,如果在sleep状态下停止某一线程,会进入catch语句,并且清除停止状态值,使之变为false。 前一个实验是先sleep然后再用interrupt()停止,与之相反的操作在学习过程中也要注意:publicclassMyThreadextendsThread{publicvoidrun(){super。run();try{System。out。println(线程开始);for(inti0;i10000;i){System。out。println(ii);}Thread。sleep(200000);System。out。println(线程结束。);}catch(InterruptedExceptione){System。out。println(先停止,再遇到sleep,进入catch异常);e。printStackTrace();}}}publicclassRun{publicstaticvoidmain(Stringargs〔〕){ThreadthreadnewMyThread();thread。start();thread。interrupt();}} 运行结果:i9998i9999先停止,再遇到sleep,进入catch异常java。lang。InterruptedException:sleepinterruptedatjava。lang。Thread。sleep(NativeMethod)atthread。MyThread。run(MyThread。java:15)5。能停止的线程暴力停止 使用stop()方法停止线程则是非常暴力的。publicclassMyThreadextendsThread{privateinti0;publicvoidrun(){super。run();try{while(true){System。out。println(ii);i;Thread。sleep(200);}}catch(InterruptedExceptione){e。printStackTrace();}}}publicclassRun{publicstaticvoidmain(Stringargs〔〕)throwsInterruptedException{ThreadthreadnewMyThread();thread。start();Thread。sleep(2000);thread。stop();}} 运行结果:i0i1i2i3i4i5i6i7i8i9Processfinishedwithexitcode06。方法stop()与java。lang。ThreadDeath异常 调用stop()方法时会抛出java。lang。ThreadDeath异常,但是通常情况下,此异常不需要显示地捕捉。publicclassMyThreadextendsThread{privateinti0;publicvoidrun(){super。run();try{this。stop();}catch(ThreadDeathe){System。out。println(进入异常catch);e。printStackTrace();}}}publicclassRun{publicstaticvoidmain(Stringargs〔〕)throwsInterruptedException{ThreadthreadnewMyThread();thread。start();}} stop()方法以及作废,因为如果强制让线程停止有可能使一些清理性的工作得不到完成。另外一个情况就是对锁定的对象进行了解锁,导致数据得不到同步的处理,出现数据不一致的问题。7。释放锁的不良后果 使用stop()释放锁将会给数据造成不一致性的结果。如果出现这样的情况,程序处理的数据就有可能遭到破坏,最终导致程序执行的流程错误,一定要特别注意:publicclassSynchronizedObject{privateSprivateSpublicsynchronizedvoidprintString(Stringname,Stringpassword){try{this。Thread。sleep(100000);this。}catch(InterruptedExceptione){e。printStackTrace();}}publicStringgetName(){}publicvoidsetName(Stringname){this。}publicStringgetPassword(){}publicvoidsetPassword(Stringpassword){this。}}publicclassMyThreadextendsThread{privateSynchronizedObjectsynchronizedOpublicMyThread(SynchronizedObjectsynchronizedObject){this。synchronizedObjectsynchronizedO}publicvoidrun(){synchronizedObject。printString(b,bb);}}publicclassRun{publicstaticvoidmain(Stringargs〔〕)throwsInterruptedException{SynchronizedObjectsynchronizedObjectnewSynchronizedObject();ThreadthreadnewMyThread(synchronizedObject);thread。start();Thread。sleep(500);thread。stop();System。out。println(synchronizedObject。getName()synchronizedObject。getPassword());}} 输出结果:baa 由于stop()方法以及在JDK中被标明为过期作废的方法,显然它在功能上具有缺陷,所以不建议在程序张使用stop()方法。8。使用return停止线程 将方法interrupt()与return结合使用也能实现停止线程的效果:publicclassMyThreadextendsThread{publicvoidrun(){while(true){if(this。isInterrupted()){System。out。println(线程被停止了!);}System。out。println(Time:System。currentTimeMillis());}}}publicclassRun{publicstaticvoidmain(Stringargs〔〕)throwsInterruptedException{ThreadthreadnewMyThread();thread。start();Thread。sleep(2000);thread。interrupt();}} 输出结果:。。。Time:1467072288503Time:1467072288503Time:1467072288503线程被停止了! 不过还是建议使用抛异常的方法来实现线程的停止,因为在catch块中还可以将异常向上抛,使线程停止事件得以传播。最后 在这里我再分享一份由多位大佬亲自收录整理的Android学习PDF架构视频面试文档源码笔记,高级架构技术进阶脑图、Android开发面试专题资料,高级进阶架构资料 这些都是我现在闲暇时还会反复翻阅的精品资料。里面对近几年的大厂面试高频知识点都有详细的讲解。相信可以有效地帮助大家掌握知识、理解原理,帮助大家在未来面试取得一份不错的答卷。 当然,你也可以拿去查漏补缺,提升自身的竞争力。 如果你有需要的话,只需私信我【进阶】即可获取