停止一个线程意味着在任务处理完任务之前停掉正在做的操作也就是放弃当前的操作。停止一个线程可以用Thread.stop()方法但最好不要用它。虽然它确实可以停止一个正在运行的线程但是这个方法是不安全的而且是已被废弃的方法。在java中有以下3种方法可以终止正在运行的线程使用退出标志使线程正常退出也就是当run方法完成后线程终止。使用stop方法强行终止但是不推荐这个方法因为stop和suspend及resume一样都是过期作废的方法。使用interrupt方法中断线程。1. 停止不了的线程interrupt()方法的使用效果并不像forbreak语句那样马上就停止循环。调用interrupt方法是在当前线程中打了一个停止标志并不是真的停止线程。publicclassMyThreadextendsThread{ publicvoidrun(){ super.run(); for(int i0; i500000; i){ System.out.println(i(i1)); } } } publicclassRun{ publicstaticvoidmain(String args[]){ Thread thread new MyThread(); thread.start(); try { Thread.sleep(2000); thread.interrupt(); } catch (InterruptedException e) { e.printStackTrace(); } } }输出结果... i499994 i499995 i499996 i499997 i499998 i499999 i5000002. 判断线程是否停止状态Thread.java类中提供了两种方法this.interrupted(): 测试当前线程是否已经中断this.isInterrupted(): 测试线程是否已经中断那么这两个方法有什么图区别呢我们先来看看this.interrupted()方法的解释测试当前线程是否已经中断当前线程是指运行this.interrupted()方法的线程。publicclassMyThreadextendsThread{ publicvoidrun(){ super.run(); for(int i0; i500000; i){ i; // System.out.println(i(i1)); } } } publicclassRun{ publicstaticvoidmain(String args[]){ Thread thread new MyThread(); thread.start(); try { Thread.sleep(2000); thread.interrupt(); System.out.println(stop 1?? thread.interrupted()); System.out.println(stop 2?? thread.interrupted()); } catch (InterruptedException e) { e.printStackTrace(); } } }运行结果stop 1??false stop 2??false类Run.java中虽然是在thread对象上调用以下代码thread.interrupt(), 后面又使用System.out.println(stop 1?? thread.interrupted()); System.out.println(stop 2?? thread.interrupted());来判断thread对象所代表的线程是否停止但从控制台打印的结果来看线程并未停止这也证明了interrupted()方法的解释测试当前线程是否已经中断。这个当前线程是main它从未中断过所以打印的结果是两个false.如何使main线程产生中断效果呢publicclassRun2{ publicstaticvoidmain(String args[]){ Thread.currentThread().interrupt(); System.out.println(stop 1?? Thread.interrupted()); System.out.println(stop 2?? Thread.interrupted()); System.out.println(End); } }运行效果为stop 1??true stop 2??false End方法interrupted()的确判断出当前线程是否是停止状态。但为什么第2个布尔值是false呢官方帮助文档中对interrupted方法的解释测试当前线程是否已经中断。线程的中断状态由该方法清除。换句话说如果连续两次调用该方法则第二次调用返回false。下面来看一下inInterrupted()方法。publicclassRun3{ publicstaticvoidmain(String args[]){ Thread thread new MyThread(); thread.start(); thread.interrupt(); System.out.println(stop 1?? thread.isInterrupted()); System.out.println(stop 2?? thread.isInterrupted()); } }运行结果stop 1??true stop 2??trueisInterrupted()并为清除状态所以打印了两个true。3. 能停止的线程--异常法有了前面学习过的知识点就可以在线程中用for语句来判断一下线程是否是停止状态如果是停止状态则后面的代码不再运行即可publicclassMyThreadextendsThread{ publicvoidrun(){ super.run(); for(int i0; i500000; i){ if(this.interrupted()) { System.out.println(线程已经终止 for循环不再执行); break; } System.out.println(i(i1)); } } } publicclassRun{ publicstaticvoidmain(String args[]){ Thread thread new MyThread(); thread.start(); try { Thread.sleep(2000); thread.interrupt(); } catch (InterruptedException e) { e.printStackTrace(); } } }运行结果... i202053 i202054 i202055 i202056 线程已经终止 for循环不再执行上面的示例虽然停止了线程但如果for语句下面还有语句还是会继续运行的。看下面的例子publicclassMyThreadextendsThread{ publicvoidrun(){ super.run(); for(int i0; i500000; i){ if(this.interrupted()) { System.out.println(线程已经终止 for循环不再执行); break; } System.out.println(i(i1)); } System.out.println(这是for循环外面的语句也会被执行); } }使用Run.java执行的结果是... i180136 i180137 i180138 i180139 线程已经终止 for循环不再执行 这是for循环外面的语句也会被执行如何解决语句继续运行的问题呢看一下更新后的代码publicclassMyThreadextendsThread{ publicvoidrun(){ super.run(); try { for(int i0; i500000; i){ if(this.interrupted()) { System.out.println(线程已经终止 for循环不再执行); thrownew InterruptedException(); } System.out.println(i(i1)); } System.out.println(这是for循环外面的语句也会被执行); } catch (InterruptedException e) { System.out.println(进入MyThread.java类中的catch了。。。); e.printStackTrace(); } } }使用Run.java运行的结果如下... i203798 i203799 i203800 线程已经终止 for循环不再执行 进入MyThread.java类中的catch了。。。 java.lang.InterruptedException at thread.MyThread.run(MyThread.java:13)4. 在沉睡中停止如果线程在sleep()状态下停止线程会是什么效果呢publicclassMyThreadextendsThread{ publicvoidrun(){ super.run(); try { System.out.println(线程开始。。。); Thread.sleep(200000); System.out.println(线程结束。); } catch (InterruptedException e) { System.out.println(在沉睡中被停止, 进入catch 调用isInterrupted()方法的结果是 this.isInterrupted()); e.printStackTrace(); } } }使用Run.java运行的结果是线程开始。。。 在沉睡中被停止, 进入catch 调用isInterrupted()方法的结果是false java.lang.InterruptedException: sleep interrupted at java.lang.Thread.sleep(Native Method) at thread.MyThread.run(MyThread.java:12)从打印的结果来看 如果在sleep状态下停止某一线程会进入catch语句并且清除停止状态值使之变为false。前一个实验是先sleep然后再用interrupt()停止与之相反的操作在学习过程中也要注意publicclassMyThreadextendsThread{ publicvoidrun(){ super.run(); try { System.out.println(线程开始。。。); for(int i0; i10000; i){ System.out.println(i i); } Thread.sleep(200000); System.out.println(线程结束。); } catch (InterruptedException e) { System.out.println(先停止再遇到sleep进入catch异常); e.printStackTrace(); } } } publicclassRun{ publicstaticvoidmain(String args[]){ Thread thread new MyThread(); thread.start(); thread.interrupt(); } }运行结果i9998 i9999 先停止再遇到sleep进入catch异常 java.lang.InterruptedException: sleep interrupted at java.lang.Thread.sleep(Native Method) at thread.MyThread.run(MyThread.java:15)5. 能停止的线程---暴力停止使用stop()方法停止线程则是非常暴力的。publicclassMyThreadextendsThread{ privateint i 0; publicvoidrun(){ super.run(); try { while (true){ System.out.println(i i); i; Thread.sleep(200); } } catch (InterruptedException e) { e.printStackTrace(); } } } publicclassRun{ publicstaticvoidmain(String args[])throws InterruptedException { Thread thread new MyThread(); thread.start(); Thread.sleep(2000); thread.stop(); } }运行结果i0 i1 i2 i3 i4 i5 i6 i7 i8 i9 Process finished with exit code 06.方法stop()与java.lang.ThreadDeath异常调用stop()方法时会抛出java.lang.ThreadDeath异常但是通常情况下此异常不需要显示地捕捉。publicclassMyThreadextendsThread{ privateint i 0; publicvoidrun(){ super.run(); try { this.stop(); } catch (ThreadDeath e) { System.out.println(进入异常catch); e.printStackTrace(); } } } publicclassRun{ publicstaticvoidmain(String args[])throws InterruptedException { Thread thread new MyThread(); thread.start(); } }stop()方法以及作废因为如果强制让线程停止有可能使一些清理性的工作得不到完成。另外一个情况就是对锁定的对象进行了解锁导致数据得不到同步的处理出现数据不一致的问题。7. 释放锁的不良后果使用stop()释放锁将会给数据造成不一致性的结果。如果出现这样的情况程序处理的数据就有可能遭到破坏最终导致程序执行的流程错误一定要特别注意publicclassSynchronizedObject{ private String name a; private String password aa; publicsynchronizedvoidprintString(String name, String password){ try { this.name name; Thread.sleep(100000); this.password password; } catch (InterruptedException e) { e.printStackTrace(); } } public String getName(){ return name; } publicvoidsetName(String name){ this.name name; } public String getPassword(){ return password; } publicvoidsetPassword(String password){ this.password password; } } publicclassMyThreadextendsThread{ private SynchronizedObject synchronizedObject; publicMyThread(SynchronizedObject synchronizedObject){ this.synchronizedObject synchronizedObject; } publicvoidrun(){ synchronizedObject.printString(b, bb); } } publicclassRun{ publicstaticvoidmain(String args[])throws InterruptedException { SynchronizedObject synchronizedObject new SynchronizedObject(); Thread thread new MyThread(synchronizedObject); thread.start(); Thread.sleep(500); thread.stop(); System.out.println(synchronizedObject.getName() synchronizedObject.getPassword()); } }输出结果b aa由于stop()方法以及在JDK中被标明为“过期/作废”的方法显然它在功能上具有缺陷所以不建议在程序张使用stop()方法。8. 使用return停止线程将方法interrupt()与return结合使用也能实现停止线程的效果publicclassMyThreadextendsThread{ publicvoidrun(){ while (true){ if(this.isInterrupted()){ System.out.println(线程被停止了); return; } System.out.println(Time: System.currentTimeMillis()); } } } publicclassRun{ publicstaticvoidmain(String args[])throws InterruptedException { Thread thread new MyThread(); thread.start(); Thread.sleep(2000); thread.interrupt(); } }输出结果... Time: 1467072288503 Time: 1467072288503 Time: 1467072288503 线程被停止了不过还是建议使用“抛异常”的方法来实现线程的停止因为在catch块中还可以将异常向上抛使线程停止事件得以传播。