线程的中断 interrunpt方法
2018-02-07 22:58
288 查看
什么是线程的中断 ?
当运行状态的线程 遇到了阻塞 会中断线程 停止程序.
当运行状态的线程 放弃了cpu的资源会中断线程 停止程序 进入休眠状态 .
当运行状态的线程 放弃了cpu的资
源 进入等待状态 会中断线程 停止程序.
当运行状态的线程 遇到了线程死锁 会中断线程 停止程序.
下面我们写一下休眠状态下的线程 设置休眠状态的时间 让程序中断
打印的结果
下面我们写一下休眠状态下的线程 用 intrrunpt清除 中断状态
答案如下图
interrunpt 会清除 中断
interrunpt方法弊端: 在不同的情况下, 使用interrunpt会遇到不同的问题 有时候遇到的问题是无法解决的 所以在要中断程序的时候最好是自己设置线程要沉睡的时间, 第一种就自定义线程沉睡时间的设置方法.
那什么情况下使用interrunpt方法呢?
只有遇到了线程冷冻状态时(线程等待)
线程冷冻: 是因为,多线程的时候,调用了Wite()方法 使调用Wite()的线程进入等待状态,没办法唤醒等待状态时,使用interrunpt方法可以强行唤醒调用Wite()的线程.
wite()方法必须使用锁对象去调用 锁对象是指对象监控器的名字.
下面我们写一个等待状态下的线程使用Wite();方强制唤醒等待线程
打印结果
当运行状态的线程 遇到了阻塞 会中断线程 停止程序.
当运行状态的线程 放弃了cpu的资源会中断线程 停止程序 进入休眠状态 .
当运行状态的线程 放弃了cpu的资
源 进入等待状态 会中断线程 停止程序.
当运行状态的线程 遇到了线程死锁 会中断线程 停止程序.
下面我们写一下休眠状态下的线程 设置休眠状态的时间 让程序中断
public class OnThe { public static void main(String[] args) { StopRunnable1 stopRunnable = new StopRunnable1(); Thread s1Thread = new Thread(stopRunnable); Date date = new Date(); DateFormat dateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss"); String format = dateFormat.format(date); System.out.println(format); s1Thread.start(); try { s1Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } //中断程序 stopRunnable.isOver = true; System.out.println("已经中断"); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("主线程结束"); } } class StopRunnable1 implements Runnable{ public boolean isOver = false; @Override public void run() { while (!isOver) { //休眠一秒钟 (写个死循环 让循环执行1秒结束) long time = System.currentTimeMillis(); while (System.currentTimeMillis()-time <1000) {} Date date = new Date(); DateFormat dateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss"); String format = dateFormat.format(date); System.out.println(Thread.currentThread().getName()+format); } } }
打印的结果
下面我们写一下休眠状态下的线程 用 intrrunpt清除 中断状态
public class Test{ public static void main (String[]agrs){ SleepRunnable sra = new SleepRunnable(); Thread xc1 = new Thread(sra); Date date = new Date(); DateFormat dateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss"); String format = dateFormat.format(date); System.out.println(format); xc1.start(); //让主线程沉睡1000毫秒也就是一秒钟 try{ Thread.sleep(1000); }catch (InterruptedExceptio e){ e.printStackTrace(); System.out.println("主线程已经沉睡"); } //让子线程调用 interrupt 方法中断程序 xc1.interrupt(); try{ xc1.sleep(1000); }catch (InterruptedExceptio e){ e.printStackTrace(); System.out.println("子线程已经沉睡"); } } } class SleepRunnable implements Runnable{ @Overread public void run(){ //用死循环测试休眠的线程还会不会运行 while(true){ try{ Thread.sleep(1000); }catch (InterruptedExceptio e){ e.printStackTrace(); } System.out.println("--------"); Date date = new Date(); DateFormat dateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss"); String format = dateFormat.format(date); System.out.println(Thread.currentThread().getName()+format); } } }
答案如下图
interrunpt
interrunpt 方法的作用interrunpt 会清除 中断
interrunpt方法弊端: 在不同的情况下, 使用interrunpt会遇到不同的问题 有时候遇到的问题是无法解决的 所以在要中断程序的时候最好是自己设置线程要沉睡的时间, 第一种就自定义线程沉睡时间的设置方法.
那什么情况下使用interrunpt方法呢?
只有遇到了线程冷冻状态时(线程等待)
线程冷冻: 是因为,多线程的时候,调用了Wite()方法 使调用Wite()的线程进入等待状态,没办法唤醒等待状态时,使用interrunpt方法可以强行唤醒调用Wite()的线程.
wite()方法必须使用锁对象去调用 锁对象是指对象监控器的名字.
下面我们写一个等待状态下的线程使用Wite();方强制唤醒等待线程
public class Test { public static void main(String[] args) { InterRuptRunable t1 = new InterRuptRunable(); InterRuptRunable t2 = new InterRuptRunable(); Date date = new Date(); DateFormat dateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss"); String format = dateFormat.format(date); System.out.println(format); t1.start(); t2.start(); for (int i = 0; i < 50; i++) { if (i == 25) { /* 会遇到 中断状态 * t1.isOver = true; */ /* * 清除中断状态 */ t1.interrupt(); t2.interrupt(); break; } System.out.println(i+"--------"); } System.out.println("主线程结束"); } } class InterRuptRunable extends Thread{ public boolean isOver = false; @Override public synchronized void run() { while (true) { try { /* * 线程1进来 带着锁 遇到wait方法 * 放弃了cpu的执行权. 但是 锁会还回去. * 线程2进来了 又遇到了等待 * 所以也再循环内等待 不会出循环执行 * 相等于线程1 和线程 2 都在那里等待 * 线程进入冷冻状态(中断状态) * 可以解决冷冻状态 (解决中断状态) * 可以调用interrupt方法 清除该状态 */ wait(); //线程等待 放弃了 cpu的执行资源 } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } Date date = new Date(); DateFormat dateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss"); String format = dateFormat.format(date); System.out.println(Thread.currentThread().getName()+format); } } }
打印结果
2018年02月07日 22:24:34 0-------- 1-------- 2-------- 3-------- 4-------- 5-------- 6-------- 7-------- 8-------- 9-------- 10-------- 11-------- 12-------- 13-------- 14-------- 15-------- 16-------- 17-------- 18-------- 19-------- 20-------- 21-------- 22-------- 23-------- 24-------- 主线程结束 java.lang.InterruptedException at java.lang.Object.wait(Native Method) at java.lang.Object.wait(Object.java:502) at com.lanou.yan.InterRuptRunable.run(Demo02.java:72) Thread-02018年02月07日 22:24:34 java.lang.InterruptedException at java.lang.Object.wait(Native Method) at java.lang.Object.wait(Object.java:502) at com.lanou.yan.InterRuptRunable.run(Demo02.java:72) Thread-12018年02月07日 22:24:34
相关文章推荐
- 线程中断方法interrupt() 与 cancel()
- java 线程的中断interrupt方法
- Thread线程中断相关方法
- Response.End方法导致线程中断
- 关于线程中断的3个重要方法总结
- Java多线程-通过线程的中断来深入学习interrupt方法,Volatile关键字
- 让每一步都跳出,这样不破坏整体流程,可以对结果随心所欲的控制(线程中断也可采用这种方法)
- Java中断线程的方法
- java线程中断方法
- Java学习笔记78. 线程的中断入门 —— interrupt方法
- java线程基础巩固---Thread中断Interrupt方法学习&采用优雅的方式结束线程生命周期
- 终止线程的2种方法。标记法和中断
- 线程中断之interrupt和stop方法
- 使用interrupt方法中断线程
- Android中断线程的处理方法
- Java中断线程的方法
- 有感于线程中断的一个方法(原创)
- Java之多线程interrupt中断线程的三种方法
- 线程中断机制,精确控制方法执行
- 关键字this、super、final、static、synchronized及 中断线程的方法