在多线程开发中停止线程是很重要的技术点。停止线程在Java语言中并不像break语句那样干脆,需要一些技巧性的处理。
一、 异常法
采用异常法来停止一个线程,首先我们需要了解一下两个方法的用法:
1、interrupt()方法
1public class MyThread extends Thread{ 2 @Override 3 public void run() { 4 for (int i = 1; i <= 10000; i++) { 5 System.out.println("i="+i); 6 } 7 } 8 public static void main(String[] args)throws Exception { 9 MyThread thread=new MyThread(); 10 thread.start(); 11 thread.sleep(10); 12 thread.interrupt(); 13 } 14}
上面的例子调用interrupt()方法来停止线程,但interrupt()方法的使用效果并不像for+break语句那样,马上就能停止循环。调用interrupt()方法仅仅是在当前线程打了一个停止的标记,并不是真的停止。那么如果停止线程了?我们接着往下面看。
2、判断线程是否是停止状态
1)、 interrupted()
1public class MyThread extends Thread{ 2 @Override 3 public void run() { 4 for (int i = 1; i <= 10000; i++) { 5 System.out.println("i="+i); 6 } 7 } 8 public static void main(String[] args)throws Exception { 9 try{ 10 MyThread thread=new MyThread(); 11 thread.start(); 12 thread.sleep(100); 13 thread.interrupt(); 14 System.out.println("线程停止了吗1?--->"+thread.interrupted()); 15 System.out.println("线程停止了吗2?--->"+thread.interrupted()); 16 }catch(Exception e){ 17 e.printStackTrace(); 18 } 19 System.out.println("end"); 20 21 } 22}

从控制台打印的结果来看,线程没有停止,这就是说,interrupted()测试当前线程是否中断,因为这个当前线程就是main,它没有中断过,所以打印的结果是两个false。
如何使main线程产生中断效果了。我们在看下,下面的例子:
1public class MyThread{ 2 public static void main(String[] args){ 3 Thread.currentThread().interrupt(); 4 System.out.println("线程停止了吗1?---->"+Thread.interrupted()); 5 System.out.println("线程停止了吗2?---->"+Thread.interrupted()); 6 System.out.println("end"); 7 } 8}

从上面的结果来看,interrupted()方法的确判断当前线程是否是停止状态。但是为什么第2个值是false。原来,连续两次调用该方法第一次会清除中断状态后,第二次调用所以返回flase。
2)、 isInterrupted()
1public class MyThread extends Thread{ 2 @Override 3 public void run() { 4 for (int i = 1; i <= 10000; i++) { 5 System.out.println("i="+i); 6 } 7 } 8 public static void main(String[] args){ 9 try{ 10 MyThread thread=new MyThread(); 11 thread.start(); 12 thread.sleep(10); 13 thread.interrupt(); 14 System.out.println("线程停止了吗1?--->"+thread.isInterrupted()); 15 System.out.println("线程停止了吗2?--->"+thread.isInterrupted()); 16 }catch(Exception e){ 17 e.printStackTrace(); 18 } 19 System.out.println("end"); 20 21 } 22}

从结果看出方法isInterrupted()并未清除,所以打印出了两个true.
3、停止线程
1public class MyThread extends Thread{ 2 @Override 3 public void run() { 4 for (int i = 1; i <= 10000; i++) { 5 if(this.interrupted()){ 6 System.out.println("线程是停止状态了,我要退出了."); 7 break; 8 } 9 System.out.println("i="+i); 10 } 11 System.out.println("如果此处还是循环,那么我就会继续执行.线程并没有停止"); 12 } 13 public static void main(String[] args){ 14 try{ 15 MyThread thread=new MyThread(); 16 thread.start(); 17 thread.sleep(10); 18 thread.interrupt(); 19 System.out.println("end"); 20 }catch(Exception e){ 21 e.printStackTrace(); 22 } 23 } 24}

如果这么写的话,线程并没有停止。现在我们在修改下代码,也就是所谓的异常法停止线程。
1public class MyThread extends Thread{ 2 @Override 3 public void run() { 4 try{ 5 for (int i = 1; i <= 10000; i++) { 6 if(this.interrupted()){ 7 System.out.println("线程是停止状态了,我要退出了."); 8 throw new InterruptedException(); 9 } 10 System.out.println("i="+i); 11 } 12 System.out.println("我被执行了吗?"); 13 }catch(InterruptedException e){ 14 System.out.println("---这次线程停了---"); 15 e.printStackTrace(); 16 } 17 } 18 public static void main(String[] args){ 19 try{ 20 MyThread thread=new MyThread(); 21 thread.start(); 22 thread.sleep(10); 23 thread.interrupt(); 24 System.out.println("end"); 25 }catch(Exception e){ 26 e.printStackTrace(); 27 } 28 } 29}

二、 在沉睡中停止
如果线程在sleep()状态下停止线程会有什么效果了?
1public class MyThread extends Thread{ 2 @Override 3 public void run() { 4 try{ 5 System.out.println("run start"); 6 Thread.sleep(1000000); 7 System.out.println("run end"); 8 }catch(InterruptedException e){ 9 System.out.println("sleep被停止,状态:--->"+this.isInterrupted()); 10 e.printStackTrace(); 11 } 12 } 13 public static void main(String[] args){ 14 try{ 15 MyThread thread=new MyThread(); 16 thread.start(); 17 thread.interrupt(); 18 thread.sleep(1000); 19 }catch(Exception e){ 20 System.out.println("main catch"); 21 e.printStackTrace(); 22 } 23 } 24}

从结果我们可以看出,在线程睡眠时候停止某一线程,会异常,并且清除停止状态。我们前面异常停止线程,都是先睡眠,在停止线程,与之相反的操作,我写代码的时候需要注意下。
1public class MyThread extends Thread{ 2 @Override 3 public void run() { 4 try{ 5 for (int i = 1; i <= 10000; i++) { 6 System.out.println("i="+i); 7 } 8 System.out.println("run start"); 9 Thread.sleep(1000000); 10 System.out.println("run end"); 11 }catch(InterruptedException e){ 12 System.out.println("线程被停止了,在sleep,状态:--->"+this.isInterrupted()); 13 e.printStackTrace(); 14 } 15 } 16 public static void main(String[] args){ 17 try{ 18 MyThread thread=new MyThread(); 19 thread.start(); 20 thread.interrupt(); 21 }catch(Exception e){ 22 System.out.println("main catch"); 23 e.printStackTrace(); 24 } 25 } 26}

三、 暴力停止
1public class MyThread extends Thread{ 2 private int i=0; 3 @Override 4 public void run() { 5 try{ 6 while (true) { 7 i++; 8 System.out.println("i="+i); 9 Thread.sleep(1000); 10 } 11 }catch(Exception e){ 12 e.printStackTrace(); 13 } 14 } 15 public static void main(String[] args){ 16 try{ 17 MyThread thread=new MyThread(); 18 thread.start(); 19 Thread.sleep(6000); 20 thread.stop(); 21 }catch(Exception e){ 22 e.printStackTrace(); 23 } 24 } 25}

stop()方法已经被弃用,如果强制让线程停止,可以会有一些清理工作没得到完成,还有就是对锁定的对象进行了解锁,导致数据不同步的现象,所以开发时候禁止使用该方法去暴力停止线程。
四、 使用return停止线程
1public class MyThread extends Thread{ 2 private int i=0; 3 @Override 4 public void run() { 5 try{ 6 while (true) { 7 i++; 8 if(this.interrupted()){ 9 System.out.println("线程停止了"); 10 return; 11 } 12 System.out.println("i="+i); 13 } 14 }catch(Exception e){ 15 e.printStackTrace(); 16 } 17 } 18 public static void main(String[] args){ 19 try{ 20 MyThread thread=new MyThread(); 21 thread.start(); 22 Thread.sleep(2000); 23 thread.interrupt(); 24 }catch(Exception e){ 25 e.printStackTrace(); 26 } 27 } 28}

PS:不过还是建议使用异常法来停止线程,因为在catch块中还可以将异常向上抛,使线程停止事件得到传播。
版权声明:本文为博主原创文章,未经博主允许不得转载。