使用interrupt方法中断线程
2017-09-03 23:54
513 查看
Thread类的interrupt方法是用来中断一个线程的。
从输出结果可以看出,被中断的线程并没有停止执行。
输出结果:
上面的示例虽然停止了线程,但是如果for语句后面还有语句,这些语句还是会继续。看下面的例子:
使用Run.java执行的结果是:
如何解决语句继续运行的问题?
看一下更新后的代码:
输出结果:
[align=left]
[/align]
当前线程是main线性,该线程不处于中断状态。再看下面的例子
输出结果:
在这个例子中,中断的当前线程是main,所有第一结果为true。第二个结果为false是因为,调用了interrupted方法会清楚当前线程的中断状态。
接着看isInterrupted()方法。它判断的是调用它的对象所启动的线程是否处于中断状态,而不是判断当前线程。
运行结果:
interrupt()方法不像stop方法强行终止一个线程。它只是把要被停止的线程设置成中断状态。而此时要被中断的线程是可以继续执行的。
public class MyThread extends Thread { public void run(){ super.run(); for(int i=0; i<500000; i++){ System.out.println("i="+(i+1)); } } } public class Run { public static void main(String args[]){ Thread thread = new MyThread(); thread.start(); try { Thread.sleep(2000); thread.interrupt(); } catch (InterruptedException e) { e.printStackTrace(); } } }输出结果:
... i=499994 i=499995 i=499996 i=499997 i=499998 i=499999 i=500000
从输出结果可以看出,被中断的线程并没有停止执行。
被中断的线程可以通过isInterrupted()或者是interrupted()方法判断当前线程的中断状态是否标志为中断,然后终止线程。
public class MyThread extends Thread { public void run(){ super.run(); for(int i=0; i<500000; i++){ if(this.interrupted()) { System.out.println("线程已经终止, for循环不再执行"); break; } System.out.println("i="+(i+1)); } } } public class Run { public static void main(String args[]){ Thread thread = new MyThread(); thread.start(); try { Thread.sleep(2000); thread.interrupt(); } catch (InterruptedException e) { e.printStackTrace(); } } }
输出结果:
... i=202053 i=202054 i=202055 i=202056 线程已经终止, for循环不再执行。
上面的示例虽然停止了线程,但是如果for语句后面还有语句,这些语句还是会继续。看下面的例子:
public class MyThread extends Thread { public void run(){ super.run(); for(int i=0; i<500000; i++){ if(this.interrupted()) { System.out.println("线程已经终止, for循环不再执行"); break; } System.out.println("i="+(i+1)); } System.out.println("这是for循环外面的语句,也会被执行"); } }
使用Run.java执行的结果是:
... i=180136 i=180137 i=180138 i=180139 线程已经终止, for循环不再执行 这是for循环外面的语句,也会被执行
如何解决语句继续运行的问题?
看一下更新后的代码:
public class MyThread extends Thread { public void run(){ super.run(); try { for(int i=0; i<500000; i++){ if(this.interrupted()) { System.out.println("线程已经终止, for循环不再执行"); throw new InterruptedException(); } System.out.println("i="+(i+1)); } System.out.println("这是for循环外面的语句,也会被执行"); } catch (InterruptedException e) { System.out.println("进入MyThread.java类中的catch了。。。"); e.printStackTrace(); } System.out.println("我不会被输出"); } }
输出结果:
... i=203798 i=203799 i=203800 线程已经终止, for循环不再执行 进入MyThread.java类中的catch了。。。 java.lang.InterruptedException at thread.MyThread.run(MyThread.java:13)通过异常的方式来结束线程。
[align=left]
[/align]
isInterrupted()和interrupted()方法的区别
interrupted()方法是静态方法,它判断的是当前线程是否中断public class MyThread extends Thread { public void run(){ super.run(); for(int i=0; i<500000; i++){ i++;// System.out.println("i="+(i+1)); } } } public class Run { public static void main(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
当前线程是main线性,该线程不处于中断状态。再看下面的例子
public class Run2 { public static void main(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
在这个例子中,中断的当前线程是main,所有第一结果为true。第二个结果为false是因为,调用了interrupted方法会清楚当前线程的中断状态。
接着看isInterrupted()方法。它判断的是调用它的对象所启动的线程是否处于中断状态,而不是判断当前线程。
public class Run4 { public static void main(String[] args) { Thread thread = new MyThread(); thread.start(); Thread.currentThread().interrupt(); System.out.println("stop 1??" + thread.isInterrupted()); System.out.println("stop 2??" + thread.isInterrupted()); } }
运行结果:
stop 1??false stop 2??false这个例子中断的线程是当前线程main,可以看到输出结果为false。
public class Run4 { public static void main(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??true从这个例子可以看出isInterrupted()方法判断的是调用它的对象所启动的线程是否处于中断状态。也可以看出这个方法不会清除被中断线程的中断状态。
相关文章推荐
- 使用interrupt方法中断线程
- 线程中断方法interrupt() 与 cancel()
- 使用interrupt()方法和isInterrupted()方法实现了线程
- java--- 使用interrupte中断线程的真正用途
- java 线程的中断interrupt方法
- Java多线程-通过线程的中断来深入学习interrupt方法,Volatile关键字
- Java线程--interrupt join yield setDaemon常用方法的使用
- 多线程的使用——中断线程详解(Interrupt)
- Java学习笔记78. 线程的中断入门 —— interrupt方法
- 线程 中断 Thread.interrupt()使用
- 第二篇 多线程的使用——中断线程详解(Interrupt)
- 第二篇 多线程的使用——中断线程详解(Interrupt)
- 关于java线程的interrupt方法的使用总结
- Java之多线程interrupt中断线程的三种方法
- thinkinjava21.4.4----java中interrupt和interrupted方法的使用和线程退出的时机
- Java多线程之中断线程(Interrupt)的使用详解
- java线程基础巩固---Thread中断Interrupt方法学习&采用优雅的方式结束线程生命周期
- 线程中断之interrupt和stop方法
- 线程中断 interrupt() 与 线程终止方法
- Java多线程之中断线程(Interrupt)的使用详解