当前位置: 首页 > news >正文

建设网站论坛泰州网页制作

建设网站论坛,泰州网页制作,临平做网站电话,常州网站营销推广2019独角兽企业重金招聘Python工程师标准 Java多线程系列--“基础篇”09之 interrupt()和线程终止方式 概要 本章#xff0c;会对线程的interrupt()中断和终止方式进行介绍。涉及到的内容包括#xff1a;1. interrupt()说明2. 终止线程的方式 2.1 终止处于“阻… 2019独角兽企业重金招聘Python工程师标准 Java多线程系列--“基础篇”09之 interrupt()和线程终止方式 概要 本章会对线程的interrupt()中断和终止方式进行介绍。涉及到的内容包括1. interrupt()说明2. 终止线程的方式  2.1 终止处于“阻塞状态”的线程  2.2 终止处于“运行状态”的线程3. 终止线程的示例4. interrupted() 和 isInterrupted()的区别 转载请注明出处http://www.cnblogs.com/skywang12345/p/3479949.html 1. interrupt()说明 在介绍终止线程的方式之前有必要先对interrupt()进行了解。 关于interrupt()java的djk文档描述如下http://docs.oracle.com/javase/7/docs/api/ interrupt()的作用是中断本线程。本线程中断自己是被允许的其它线程调用本线程的interrupt()方法时会通过checkAccess()检查权限。这有可能抛出SecurityException异常。如果本线程是处于阻塞状态调用线程的wait(), wait(long)或wait(long, int)会让它进入等待(阻塞)状态或者调用线程的join(), join(long), join(long, int), sleep(long), sleep(long, int)也会让它进入阻塞状态。若线程在阻塞状态时调用了它的interrupt()方法那么它的“中断状态”会被清除并且会收到一个InterruptedException异常。例如线程通过wait()进入阻塞状态此时通过interrupt()中断该线程调用interrupt()会立即将线程的中断标记设为“true”但是由于线程处于阻塞状态所以该“中断标记”会立即被清除为“false”同时会产生一个InterruptedException的异常。如果线程被阻塞在一个Selector选择器中那么通过interrupt()中断它时线程的中断标记会被设置为true并且它会立即从选择操作中返回。如果不属于前面所说的情况那么通过interrupt()中断线程时它的中断标记会被设置为“true”。中断一个“已终止的线程”不会产生任何操作。 2. 终止线程的方式 Thread中的stop()和suspend()方法由于固有的不安全性已经建议不再使用 下面我先分别讨论线程在“阻塞状态”和“运行状态”的终止方式然后再总结出一个通用的方式。 2.1 终止处于“阻塞状态”的线程 通常我们通过“中断”方式终止处于“阻塞状态”的线程。 当线程由于被调用了sleep(), wait(), join()等方法而进入阻塞状态若此时调用线程的interrupt()将线程的中断标记设为true。由于处于阻塞状态中断标记会被清除同时产生一个InterruptedException异常。将InterruptedException放在适当的位置就能终止线程形式如下 Override public void run() {try {while (true) {// 执行任务...}} catch (InterruptedException ie) { // 由于产生InterruptedException异常退出while(true)循环线程终止} } 说明在while(true)中不断的执行任务当线程处于阻塞状态时调用线程的interrupt()产生InterruptedException中断。中断的捕获在while(true)之外这样就退出了while(true)循环 注意对InterruptedException的捕获务一般放在while(true)循环体的外面这样在产生异常时就退出了while(true)循环。否则InterruptedException在while(true)循环体之内就需要额外的添加退出处理。形式如下 Override public void run() {while (true) {try {// 执行任务...} catch (InterruptedException ie) { // InterruptedException在while(true)循环体内。// 当线程产生了InterruptedException异常时while(true)仍能继续运行需要手动退出break;}} } 说明上面的InterruptedException异常的捕获在whle(true)之内。当产生InterruptedException异常时被catch处理之外仍然在while(true)循环体内要退出while(true)循环体需要额外的执行退出while(true)的操作。 2.2 终止处于“运行状态”的线程 通常我们通过“标记”方式终止处于“运行状态”的线程。其中包括“中断标记”和“额外添加标记”。(01) 通过“中断标记”终止线程。 形式如下 Override public void run() {while (!isInterrupted()) {// 执行任务...} } 说明isInterrupted()是判断线程的中断标记是不是为true。当线程处于运行状态并且我们需要终止它时可以调用线程的interrupt()方法使用线程的中断标记为true即isInterrupted()会返回true。此时就会退出while循环。注意interrupt()并不会终止处于“运行状态”的线程它会将线程的中断标记设为true。 (02) 通过“额外添加标记”。 形式如下 private volatile boolean flag true; protected void stopTask() {flag false; }Override public void run() {while (flag) {// 执行任务...} } 说明线程中有一个flag标记它的默认值是true并且我们提供stopTask()来设置flag标记。当我们需要终止该线程时调用该线程的stopTask()方法就可以让线程退出while循环。 注意将flag定义为volatile类型是为了保证flag的可见性。即其它线程通过stopTask()修改了flag之后本线程能看到修改后的flag的值。 综合线程处于“阻塞状态”和“运行状态”的终止方式比较通用的终止线程的形式如下 Override public void run() {try {// 1. isInterrupted()保证只要中断标记为true就终止线程。while (!isInterrupted()) {// 执行任务...}} catch (InterruptedException ie) { // 2. InterruptedException异常保证当InterruptedException异常产生时线程被终止。} } 3. 终止线程的示例 interrupt()常常被用来终止“阻塞状态”线程。参考下面示例 // Demo1.java的源码 class MyThread extends Thread {public MyThread(String name) {super(name);}Overridepublic void run() {try { int i0;while (!isInterrupted()) {Thread.sleep(100); // 休眠100msi;System.out.println(Thread.currentThread().getName() (this.getState()) loop i); }} catch (InterruptedException e) { System.out.println(Thread.currentThread().getName() (this.getState()) catch InterruptedException.); }} }public class Demo1 {public static void main(String[] args) { try { Thread t1 new MyThread(t1); // 新建“线程t1”System.out.println(t1.getName() (t1.getState()) is new.); t1.start(); // 启动“线程t1”System.out.println(t1.getName() (t1.getState()) is started.); // 主线程休眠300ms然后主线程给t1发“中断”指令。Thread.sleep(300);t1.interrupt();System.out.println(t1.getName() (t1.getState()) is interrupted.);// 主线程休眠300ms然后查看t1的状态。Thread.sleep(300);System.out.println(t1.getName() (t1.getState()) is interrupted now.);} catch (InterruptedException e) { e.printStackTrace();}} } t1 (NEW) is new. t1 (RUNNABLE) is started. t1 (RUNNABLE) loop 1 t1 (RUNNABLE) loop 2 t1 (TIMED_WAITING) is interrupted. t1 (RUNNABLE) catch InterruptedException. t1 (TERMINATED) is interrupted now. 结果说明 (01) 主线程main中通过new MyThread(t1)创建线程t1之后通过t1.start()启动线程t1。 (02) t1启动之后会不断的检查它的中断标记如果中断标记为“false”则休眠100ms。 (03) t1休眠之后会切换到主线程main主线程再次运行时会执行t1.interrupt()中断线程t1。t1收到中断指令之后会将t1的中断标记设置“false”而且会抛出InterruptedException异常。在t1的run()方法中是在循环体while之外捕获的异常因此循环被终止。 我们对上面的结果进行小小的修改将run()方法中捕获InterruptedException异常的代码块移到while循环体内。 // Demo2.java的源码 class MyThread extends Thread {public MyThread(String name) {super(name);}Overridepublic void run() {int i0;while (!isInterrupted()) {try {Thread.sleep(100); // 休眠100ms} catch (InterruptedException ie) { System.out.println(Thread.currentThread().getName() (this.getState()) catch InterruptedException.); }i;System.out.println(Thread.currentThread().getName() (this.getState()) loop i); }} }public class Demo2 {public static void main(String[] args) { try { Thread t1 new MyThread(t1); // 新建“线程t1”System.out.println(t1.getName() (t1.getState()) is new.); t1.start(); // 启动“线程t1”System.out.println(t1.getName() (t1.getState()) is started.); // 主线程休眠300ms然后主线程给t1发“中断”指令。Thread.sleep(300);t1.interrupt();System.out.println(t1.getName() (t1.getState()) is interrupted.);// 主线程休眠300ms然后查看t1的状态。Thread.sleep(300);System.out.println(t1.getName() (t1.getState()) is interrupted now.);} catch (InterruptedException e) { e.printStackTrace();}} } t1 (NEW) is new. t1 (RUNNABLE) is started. t1 (RUNNABLE) loop 1 t1 (RUNNABLE) loop 2 t1 (TIMED_WAITING) is interrupted. t1 (RUNNABLE) catch InterruptedException. t1 (RUNNABLE) loop 3 t1 (RUNNABLE) loop 4 t1 (RUNNABLE) loop 5 t1 (TIMED_WAITING) is interrupted now. t1 (RUNNABLE) loop 6 t1 (RUNNABLE) loop 7 t1 (RUNNABLE) loop 8 t1 (RUNNABLE) loop 9 ... 结果说明 程序进入了死循环 为什么会这样呢这是因为t1在“等待(阻塞)状态”时被interrupt()中断此时会清除中断标记[即isInterrupted()会返回false]而且会抛出InterruptedException异常[该异常在while循环体内被捕获]。因此t1理所当然的会进入死循环了。 解决该问题需要我们在捕获异常时额外的进行退出while循环的处理。例如在MyThread的catch(InterruptedException)中添加break 或 return就能解决该问题。 下面是通过“额外添加标记”的方式终止“状态状态”的线程的示例 // Demo3.java的源码 class MyThread extends Thread {private volatile boolean flag true;public void stopTask() {flag false;}public MyThread(String name) {super(name);}Overridepublic void run() {synchronized(this) {try {int i0;while (flag) {Thread.sleep(100); // 休眠100msi;System.out.println(Thread.currentThread().getName() (this.getState()) loop i); }} catch (InterruptedException ie) { System.out.println(Thread.currentThread().getName() (this.getState()) catch InterruptedException.); }} } }public class Demo3 {public static void main(String[] args) { try { MyThread t1 new MyThread(t1); // 新建“线程t1”System.out.println(t1.getName() (t1.getState()) is new.); t1.start(); // 启动“线程t1”System.out.println(t1.getName() (t1.getState()) is started.); // 主线程休眠300ms然后主线程给t1发“中断”指令。Thread.sleep(300);t1.stopTask();System.out.println(t1.getName() (t1.getState()) is interrupted.);// 主线程休眠300ms然后查看t1的状态。Thread.sleep(300);System.out.println(t1.getName() (t1.getState()) is interrupted now.);} catch (InterruptedException e) { e.printStackTrace();}} } t1 (NEW) is new. t1 (RUNNABLE) is started. t1 (RUNNABLE) loop 1 t1 (RUNNABLE) loop 2 t1 (TIMED_WAITING) is interrupted. t1 (RUNNABLE) loop 3 t1 (TERMINATED) is interrupted now. 4. interrupted() 和 isInterrupted()的区别 最后谈谈 interrupted() 和 isInterrupted()。 interrupted() 和 isInterrupted()都能够用于检测对象的“中断标记”。 区别是interrupted()除了返回中断标记之外它还会清除中断标记(即将中断标记设为false)而isInterrupted()仅仅返回中断标记。         转载于:https://my.oschina.net/LucasZhu/blog/1529532
http://www.huolong8.cn/news/177805/

相关文章:

  • 网站建设 三合一适合大学生做的网站有哪些
  • 有哪些企业可以做招聘的网站有哪些广州网站建设信科便宜
  • 为公司设计一个网站做网站的缺点
  • 花卉网站源码东莞建设银行网点查询
  • 移动网站排名怎么做wordpress首页全屏广告
  • 金山手机网站建设网站建设 参照 标准规范
  • 杭州专业的网站制作成功案例wordpress 分隔符 sp
  • 做招聘网站代理商需要多少钱建设项目环境影响评价登记表网站
  • 分类信息网站发布标题旅游手机网站开发
  • 商城网站风格网站外包要花多少钱
  • 北京微网站设计制作服务铁岭做网站一般多少钱
  • 国内网站建设公司小程序怎样制作
  • 软件公司网站模板下载如何在局域网中做网站
  • 新乡网站建设设计公司哪家好智慧团建系统官方网站
  • 如何在好医生网站做二类学分网站首页怎么做ps
  • 免费只做网站库尔勒网站商城建设
  • 单位网站建设实施方案做微商做什么网站比较好
  • 推荐常州微信网站建设属于门户网站的平台有
  • 个人建立网站怎么赚钱深圳高端网站建设创新
  • 微信推广网站建设网站开发工具论文
  • 网站备案咨询网站上线倒计时页面
  • 装修公司网站源码phpwordpress博客必备插件
  • 在网站上做漂浮网站突然没收录
  • jsp网站开发详解下载网站建设需要ui吗
  • dw做网站时怎么在图片上加字化妆品网站制作需要
  • 多语种网站开发郑州网站优化哪家专业
  • winxp下做网站无锡手机网站建设服务
  • 个人摄影网站模版有什么网站建设比较好的公司
  • 石家庄营销型网站建设公司html字体代码大全
  • 合肥建设银行招聘网站重庆装修网