打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Java并发——线程中断学习

1. 使用interrupt中断线程

当一个线程运行时,另一个线程可以调用对应的Thread对象的interrupt方法来中断它,该方法只是在目标线程中设置一个标志,表示它已经被中断,并立即返回。这里需要注意的是,如果只是单纯的调用interrupt方法,线程并没有实际被中断,会继续往下执行。如下代码所示:

public class SleepInterrupt implements Runnable { @Override public void run { try { System.out.println('子线程开始执行'); Thread.sleep(20000); System.out.println('子线程继续执行'); } catch (InterruptedException e) { System.out.println('子线程遇到中断异常'); //处理完中断异常后,返回到run方法入口 //如果没有return,线程不会实际被中断,它会继续打印下面的信息 return; } System.out.println('子线程执行结束'); } public static void main(String args) { SleepInterrupt si = new SleepInterrupt; Thread t = new Thread(si); t.start; //主线程休眠2秒,从而确保刚才启动的线程有机会执行一段时间 try { Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace; } System.out.println('主线程对子线程执行中断操作'); //中断线程t t.interrupt; System.out.println('主线程结束执行'); } }

主线程启动新线程后,自身休眠2秒钟,允许新线程获得运行时间。新线程打印信息子线程开始执行后,继而休眠20秒钟,大约2秒钟后,main线程通知新线程中断,那么新线程的20秒的休眠将被打断并抛出InterruptException异常,执行跳转到catch块,打印出子线程遇到中断异常信息后结束。

请注意:由于不确定的线程规划,上图运行结果的后两行可能顺序相反,这取决于主线程和新线程哪个先消亡。但前两行信息的顺序必定如上图所示。

另外,如果将catch块中的return语句注释掉,则线程在抛出异常后会继续往下执行,而不会被中断,从而会打印出子线程执行结束信息。

2. 待决中断

在上面的例子中,sleep方法的实现检查到休眠线程被中断,它会相当友好地终止线程,并抛出InterruptedException异常。另外一种情况,如果线程在调用sleep方法前被中断,那么该中断称为待决中断,它会在刚调用sleep方法时,立即抛出InterruptedException异常。

public class PendingInterrupt extends Object { public static void main(String args) { //在main线程中中断当前线程(即main线程) Thread.currentThread.interrupt; //获取当前时间 long startTime = System.currentTimeMillis; try { Thread.sleep(2000); System.out.println('没有被中断'); } catch (InterruptedException e) { System.out.println('被中断'); } //计算中间代码执行的时间 System.out.println('执行耗时=' + (System.currentTimeMillis - startTime)); } }

这种模式下,main线程中断它自身。除了将中断标志(它是Thread的内部标志)设置为true外,没有其他任何影响。线程被中断了,但main线程仍然运行main线程继续监视实时时钟并进入try块,一旦调用sleep方法就会注意到待决中断的存在,并抛出InterruptException。于是执行跳转到catch块,并打印出线程被中断的信息。最后,计算并打印出时间差。最终输出的时间差距应该远小于2000

3. 使用isInterrupted方法判断中断状态

可以在Thread对象上调用isInterrupted方法来检查任何线程的中断状态

这里需要注意:线程一旦被中断isInterrupted方法便会返回true,而一旦sleep方法抛出异常,它将清空中断标志,此时isInterrupted方法将返回false。下面的代码演示了isInterrupted方法的使用:

public class InterruptCheck{ public static void main(String args) { Thread t = Thread.currentThread; System.out.println('Point A: t.isInterrupted=' + t.isInterrupted); //待决中断,中断自身 t.interrupt; System.out.println('Point B: t.isInterrupted=' + t.isInterrupted); System.out.println('Point C: t.isInterrupted=' + t.isInterrupted); try { Thread.sleep(2000); System.out.println('was NOT interrupted'); } catch (InterruptedException e) { System.out.println('was interrupted'); } //跑出异常后,会清除中断标志,这里会返回false System.out.println('Point D: t.isInterrupted=' + t.isInterrupted); } }

4. 使用Thread.interrupted方法判断中断状态

可以使用Thread.interrupted方法来检查当前线程的中断状态(并隐式重置为false)。又由于它是静态方法,因此不能在特定的线程上使用,而只能报告调用它的线程的中断状态,如果线程被中断,而且中断状态尚不清楚,那么这个方法返回trueisInterrupted不同,它将自动重置中断状态为false,第二次调用Thread.interrupted方法,总是返回false,除非中断了线程

如下代码演示了Thread.interrupted方法的使用:

public class InterruptReset{ public static void main(String args) { System.out.println( 'Point X: Thread.interrupted=' + Thread.interrupted); Thread.currentThread.interrupt; System.out.println( 'Point Y: Thread.interrupted=' + Thread.interrupted); System.out.println( 'Point Z: Thread.interrupted=' + Thread.interrupted); } }
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
线程中断
线程中断以及线程中断引发的那些问题,你值得了解
JAVA线程的interrupt
趣谈并发(1):全面认识 Thread
终止线程的三种方法
JAVA并发任务中止的isinterrupted,interupted()
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服